Thursday, 15 May 2014

Ruby 1.8.7 convert hash to string -



Ruby 1.8.7 convert hash to string -

i wasn't working ruby 1.8.7 , surprised that:

{:k => 30}.to_s #=> "k30"

is there ready utilize prepare convert hash string ruby 1.8.7 create like:

{:k => 30}.to_s #=> "{:k=>30}"

hash.to_s has indeed been changed 1.8.7 1.9.3.

in 1.8.7, (ref: http://ruby-doc.org/core-1.8.7/hash.html#method-i-to_s):

converts hsh string converting hash array of [ key, value ] pairs , converting array string using array#join default separator.

in 1.9.3, (ref: http://www.ruby-doc.org/core-1.9.3/hash.html#method-i-to_s)

alias : inspect

you monkey-patch hash class in 1.8.7 same locally following:

class hash alias :to_s :inspect end

before monkey-patching:

1.8.7 :001 > {:k => 30}.to_s => "k30" 1.8.7 :002 > {:k => 30}.inspect => "{:k=>30}"

monkey-patching & after:

1.8.7 :003 > class hash; alias :to_s :inspect; end => nil 1.8.7 :004 > {:k => 30}.to_s => "{:k=>30}"

ruby ruby-1.8.7

No comments:

Post a Comment