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

**URL:** <https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514>\
**Category:** Ruby\
**Created:** [November 17, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514 "2007-11-17T20:51:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [November 17, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514/1 "2007-11-17T20:51:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [November 17, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514/2 "2007-11-17T20:57:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [November 17, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514/3 "2007-11-17T21:06:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [November 17, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514/4 "2007-11-17T21:07:17Z")

</div>

From: “Pokkai D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 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

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [November 17, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514/5 "2007-11-17T21:34:58Z")

</div>

thanks both of you…

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [November 17, 2007, 9:32pm UTC](https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514/6 "2007-11-17T21:32:59Z")

</div>

Bill K. wrote:

> From: “Pokkai D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> > 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

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [November 18, 2007, 5:40am UTC](https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514/7 "2007-11-18T05:40:51Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [November 18, 2007, 5:54am UTC](https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514/8 "2007-11-18T05:54:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [November 18, 2007, 6:25am UTC](https://www.ruby-forum.com/t/make-a-hash-object-to-string-object-as-look-like-a-hash/120514/9 "2007-11-18T06:25:40Z")

</div>

On Nov 17, 9:41 pm, Pokkai D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.”)
