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

P

Pokkai Dokkai

user_name@host:~$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?
 
P

Pokkai Dokkai

sorry my correct code is here

user_name@host:~$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?
 
S

Sebastian Hungerecker

Pokkai said:
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
 
B

Bill Kelly

From: "Pokkai Dokkai said:
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
 
P

Pokkai Dokkai

Sebastian said:
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 ?
 
D

Devi Web Development

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 Keeney
 
P

Phrogz

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top