ruby on rails - Hash does not contain 'try' method -
i noticing differences between hash object within ruby 1.8.7 , hash object within rails 3.0.10.
for example, within 1.8.7 irb
, get:
1.8.7 :001 > {}.try(:method) nomethoderror: undefned method `try' {}:hash (irb):1```
however, 3.0.10 rails console, get:
1.8.7 :003 > {}.try(:method_x) nomethoderror: undefined method `method_x' {}:hash (irb):3:in `try' (irb):3
this surprises me because under impression try
defined in object ancestor of hash , try
return nil instead of throwing nomethoderror.
what missing?
this surprises me because under impression
try
defined inobject
ancestor ofhash
,try
returnnil
instead of throwingnomethoderror
.what missing?
your impression of class try
defined in correct (object
). missing file defined in. it's defined in activesupport library, not in ruby core library.
so, need
require 'active_support/core_ext/object/try'
first.
Comments
Post a Comment