Hi --

On Tue, 30 Jun 2009, Bertram Scharpf wrote:

> Hi,
>
> Am Dienstag, 30. Jun 2009, 04:37:55 +0900 schrieb Mike Sassak:
>> On Mon, Jun 29, 2009 at 2:44 PM, Wijnand Wiersma <wijnand / videre.net> wrote:
>>   def bar(v=nil)
>>     v ? @bar = v : @bar
>>   end
>
> Sorry, but I cannot leave this as it is (untested):
>
>  def set_bar v = nil
>    @bar ||= v
>  end

That only sets @bar if @bar hasn't be set (to a true value) already.

class C
   attr_reader :bar
   def set_bar(v = nil)
     @bar ||= v
   end
end

c = C.new
c.set_bar(3)
puts c.bar     # 3

c.set_bar(4)
puts c.bar     # 3


David

-- 
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com