Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > make a hash object to string object as look like a hash ?

Reply
Thread Tools

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

 
 
Pokkai Dokkai
Guest
Posts: n/a
 
      11-17-2007
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?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Pokkai Dokkai
Guest
Posts: n/a
 
      11-17-2007
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?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      11-17-2007
Pokkai Dokkai 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


--
NP: Anathema - Wings Of God
Jabber: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Bill Kelly
Guest
Posts: n/a
 
      11-17-2007

From: "Pokkai Dokkai" <(E-Mail Removed)>
>
> 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



 
Reply With Quote
 
Pokkai Dokkai
Guest
Posts: n/a
 
      11-17-2007
Bill Kelly wrote:
> From: "Pokkai Dokkai" <(E-Mail Removed)>
>>
>> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Pokkai Dokkai
Guest
Posts: n/a
 
      11-17-2007
thanks both of you.......
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Pokkai Dokkai
Guest
Posts: n/a
 
      11-18-2007
Sebastian Hungerecker wrote:
> Pokkai Dokkai 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(>,
:a=>"aaa"}

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


any idea ?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Devi Web Development
Guest
Posts: n/a
 
      11-18-2007
> now i am facing new problem....
>
>
> h={:a=>'aaa',:f=>BigDecimal.new("10")}
> h.inspect -----> {:f=>#<BigDecimal:b7d77a3c,'0.1E2',4(>,
> :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

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      11-18-2007
On Nov 17, 9:41 pm, Pokkai Dokkai <(E-Mail Removed)> wrote:
> now i am facing new problem....
>
> h={:a=>'aaa',:f=>BigDecimal.new("10")}
> h.inspect -----> {:f=>#<BigDecimal:b7d77a3c,'0.1E2',4(>,
> :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(>, :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.")
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Can I make this look like a button and act like an a href ? active ASP .Net Web Controls 4 04-03-2007 01:50 PM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
Would like to make a cell look like a dropdown box in the edit mode Alex ASP .Net 0 04-25-2004 09:10 PM
Can't Make Swing JList look like AWT Choice Don Sykes Java 3 02-25-2004 08:32 PM



Advertisments