R
Robert Klemme
Nobody seems to have mentioned it, I guess since this is the place for
'pure' rubyists, but Rails has the following methods:
So with this you can write simply
=A0 var =3D =A0hash[key].trydowncase)
That's pretty cool, but I'd prefer it return a proxy object for all non-n= il
objects that just relays the method call, and for nil a proxy that always
returns nil, ala:
hash[key].try.downcase
Like this?
require 'singleton'
class NilProxy
include Singleton
def method_missing(*a,&b) end
end
class Object
def try
self
end
end
class NilClass
def try
NilProxy.instance
end
end
["Foo", nil, "Bar"].each do |x|
p x.try.downcase
end
Kind regards
robert
--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/