Destructors in Ruby

Hi all,
I have following class:

class Foo
def initialize
@locked = false
end

def lock
	return if @locked
	# Set up exclusive-write lock
	@locked = true
end

def do_something
	raise RuntimeError, "Not locked" unless @locked
	# Do something if locked
end

def unlock
	return unless @locked
	# Free that lock
	@locked = false
end

end

Is there any way I can ensure that ‘unlock’ will be called at the end of
instance lifetime (before GC will purge it out of mem)? I need this
feature because I can’t leave the external resource locked and I can’t
trust to other code to release (unlock) it correctly.

Thanks in advance,
W.

···


Wejn <lists+rubytalk(at)box.cz>
(svamberk.net’s Linux section, fi.muni.cz student, linuxfan)

    Bored?  Want hours of entertainment?         <<<
      Just set the initdefault to 6!             <<<

Is there any way I can ensure that ‘unlock’ will be called at the end of
instance lifetime (before GC will purge it out of mem)? I need this
feature because I can’t leave the external resource locked and I can’t
trust to other code to release (unlock) it correctly.

See: http://www.rubygarden.org/ruby?DiscussionOnUsingFinalizers

Cheers,
Nat.

···

Dr. Nathaniel Pryce
B13media Ltd.
Studio 3a, Aberdeen Business Centre, 22/24 Highbury Grove, London, N5 2EA
http://www.b13media.com

what about using Mutex ?

Wejn wrote:

···

Hi all,
I have following class:

class Foo
def initialize
@locked = false
end

def lock
return if @locked
# Set up exclusive-write lock
@locked = true
end

def do_something
raise RuntimeError, “Not locked” unless @locked
# Do something if locked
end

def unlock
return unless @locked
# Free that lock
@locked = false
end
end

Is there any way I can ensure that ‘unlock’ will be called at the end of
instance lifetime (before GC will purge it out of mem)? I need this
feature because I can’t leave the external resource locked and I can’t
trust to other code to release (unlock) it correctly.

Thanks in advance,
W.

what about using Mutex ?

How can I solve it using mutex?
(I must call external program to perform that unlocking operation)

W.

···


Wejn <lists+rubytalk(at)box.cz>
(svamberk.net’s Linux section, fi.muni.cz student, linuxfan)

    Bored?  Want hours of entertainment?         <<<
      Just set the initdefault to 6!             <<<

“Nat Pryce” nat.pryce@b13media.com writes:

See: http://www.rubygarden.org/ruby?DiscussionOnUsingFinalizers

and http://www.rubygarden.org/ruby?GCAndMemoryManagement (near bottom)