A reader method that returns the value of an instance variable and then resets it to nil
Posted
by snl
on Stack Overflow
See other posts from Stack Overflow
or by snl
Published on 2010-04-02T16:32:41Z
Indexed on
2010/04/02
17:03 UTC
Read the original article
Hit count: 146
ruby
Is there a shorter (and cleaner) way to write the following reader method:
class Foo
attr_writer :bar
def bar
return_value = @bar
self.bar = nil
return_value
end
end
Here's some output on the console to show what it does:
>> foo = Foo.new
=> #<Foo:0x1010e9cf8>
>> foo.bar
=> nil
>> foo.bar = "ok"
=> "ok"
>> foo.bar
=> "ok"
>> foo.bar
=> nil
© Stack Overflow or respective owner