On Saturday 27 June 2009 10:26:51 pm Daniel DeLorme wrote:

> Nothing = Object.new
> def name(value = Nothing)
>    @name = value unless value == Nothing
>    @name
> end

Ok, I guess you have to be trying, but I can still break this:

class Everything
  def == other
    true
  end
end

name Everything.new

And the way to fix it:

Nothing = Object.new
def name(value = Nothing)
  @name = value unless Nothing == value
  @name
end

Out of sheer curiosity, I did benchmark this, and the Nothing method is 
slightly faster. Probably worth it if anyone wants to put this in a library -- 
otherwise, I'll go with the more readable (to me):

def name(*args)
  @name = args.first unless args.empty?
  @name
end