ruby - Why does Hash.try_convert not convert a two-dimensional array into a Hash? -
the docs following hash.try_convert(obj):
try convert obj hash, using #to_hash method. returns converted hash or nil if obj cannot converted reason.
however, when try with:
[[:a,"a"],[:b,"b"]]
i this:
hash.try_convert([[:a,2],[:b, 3]]) => nil
but if use #to_h method on array, consider correct behaviour:
[[:a,"a"],[:b,"b"]].to_h => {:a=>"a", :b=>"b"}
does mean understanding of docs incorrect (i.e. 2 approaches above should not considered analogous), else going on, or being dumb in way have not considered yet?
thanks arie shaw in comments drew attention precise way being dumb.
i've subsequently discovered related bug report:
the class hash has method try_convert, documented "try convert obj hash, using #to_hash method." surpisingly, array class doesn't have method to_hash, have method to_h converts hash. looks there 2 possible methods different names, same expected behaviour, breaks usage of hash#try_convert.
which rejected thus:
ruby has 2 conversion methods object, e.g. #to_s , #to_str, #to_i , #to_int, #to_h , #to_hash. former used explicit conversion, latter used implicit conversion (from object identical method signature e.g. proxy).
and provides me obvious answer - array has #to_h method not #to_hash method.
Comments
Post a Comment