I would say this:
result=’{’
h.each_key do |key|
result<<key.inspect<<’=>’<<h[key].to_s<<’,’
end
result=result.slice(0,result.length-1)<<’}’
however, you seem to want quotation marks around the string, so you
could try this
result=’{’
h.each_key do |key|
result<<key.inspect<<’=>’
if /^</.match h[key].inspect
result<<h[key].to_s
else
result<<h[key].inspect
end
result=result.slice(0,result.length-1)<<’}’
irb(main):005:0> h[:f].to_i
=> 10
irb(main):006:0> class BigDecimal; alias_method :inspect, :to_i; end
=> BigDecimal
irb(main):007:0> h
=> {:f=>10, :a=>“aaa”}
#inspect methods of classes that aggregate other instances tend to
call the #inspect of all those instances; you can customize the
inspect method of each class to customize its output. (Here I’ve just
said “make it so that when I call .inspect on a BigDecimal instance,
instead call the to_i method.”)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.