A reader method that returns the value of an instance variable and then resets it to nil
- by snl
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