Make a hash object to string object as look like a hash?

[email protected]:~$irb
irb(main):001:0> h={‘k1’=>‘v1’,‘k2’=>‘v2’,‘k3’=>‘v3’}
=> {“k1”=>“v1”, “k2”=>“v2”, “k3”=>“v3”} ------->i want as a string
object
irb(main):002:0> k
irb(main):004:0> puts h
k1v1k2v2k3v3 ------->i don’t want
=> nil
irb(main):005:0> h

any idea to make a hash object to string object as look like a hash (
“{“k1”=>“v1”, “k2”=>“v2”, “k3”=>“v3”}”.class —>String )

above i shown what i want

is there any library in ruby to get this?

sorry my correct code is here

[email protected]:~$irb
irb(main):001:0> h={‘k1’=>‘v1’,‘k2’=>‘v2’,‘k3’=>‘v3’}
=> {“k1”=>“v1”, “k2”=>“v2”, “k3”=>“v3”} ---->i want as a string object
irb(main):004:0> h.to_s
k1v1k2v2k3v3 ---->i don’t want
irb(main):005:0>

is there any library in ruby to get this?

Pokkai D. wrote:

irb(main):001:0> h={‘k1’=>‘v1’,‘k2’=>‘v2’,‘k3’=>‘v3’}
=> {“k1”=>“v1”, “k2”=>“v2”, “k3”=>“v3”} ------->i want as a string

You want Hash#inspect

From: “Pokkai D.” [email protected]

irb(main):001:0> h={‘k1’=>‘v1’,‘k2’=>‘v2’,‘k3’=>‘v3’}
=> {“k1”=>“v1”, “k2”=>“v2”, “k3”=>“v3”} ---->i want as a string object
irb(main):004:0> h.to_s
k1v1k2v2k3v3 ---->i don’t want
irb(main):005:0>

is there any library in ruby to get this?

str = h.inspect

Regards,

Bill

thanks both of you…

Bill K. wrote:

From: “Pokkai D.” [email protected]

irb(main):001:0> h={‘k1’=>‘v1’,‘k2’=>‘v2’,‘k3’=>‘v3’}
=> {“k1”=>“v1”, “k2”=>“v2”, “k3”=>“v3”} ---->i want as a string object
irb(main):004:0> h.to_s
k1v1k2v2k3v3 ---->i don’t want
irb(main):005:0>

is there any library in ruby to get this?

str = h.inspect

Regards,

Bill

wow,thats i want

thank you bill

Sebastian H. wrote:

Pokkai D. wrote:

irb(main):001:0> h={‘k1’=>‘v1’,‘k2’=>‘v2’,‘k3’=>‘v3’}
=> {“k1”=>“v1”, “k2”=>“v2”, “k3”=>“v3”} ------->i want as a string

You want Hash#inspect

now i am facing new problem…

h={:a=>‘aaa’,:f=>BigDecimal.new(“10”)}
h.inspect -----> {:f=>#BigDecimal:b7d77a3c,‘0.1E2’,4(8),
:a=>“aaa”}

i want like this----->{:f=>10, :a=>“aaa”}

any idea ?

now i am facing new problem…

h={:a=>‘aaa’,:f=>BigDecimal.new(“10”)}
h.inspect -----> {:f=>#BigDecimal:b7d77a3c,‘0.1E2’,4(8),
:a=>“aaa”}

i want like this----->{:f=>10, :a=>“aaa”}

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)<<’}’

-Daniel Brumbaugh K.

On Nov 17, 9:41 pm, Pokkai D. [email protected] wrote:

now i am facing new problem…

h={:a=>‘aaa’,:f=>BigDecimal.new(“10”)}
h.inspect -----> {:f=>#BigDecimal:b7d77a3c,‘0.1E2’,4(8),
:a=>“aaa”}

i want like this----->{:f=>10, :a=>“aaa”}

irb(main):002:0> require ‘bigdecimal’
=> true

irb(main):003:0> h = { :a=>‘aaa’, :f=>BigDecimal.new(“10”) }
=> {:f=>#BigDecimal:34ab98,‘0.1E2’,4(8), :a=>“aaa”}

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.”)