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

Discussion in 'Ruby' started by Pokkai Dokkai, Nov 17, 2007.

  1. 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/.
     
    Pokkai Dokkai, Nov 17, 2007
    #1
    1. Advertising

  2. 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/.
     
    Pokkai Dokkai, Nov 17, 2007
    #2
    1. Advertising

  3. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Nov 17, 2007
    #3
  4. Pokkai Dokkai

    Bill Kelly Guest

    From: "Pokkai Dokkai" <>
    >
    > 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
     
    Bill Kelly, Nov 17, 2007
    #4
  5. Bill Kelly wrote:
    > From: "Pokkai Dokkai" <>
    >>
    >> 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/.
     
    Pokkai Dokkai, Nov 17, 2007
    #5
  6. Pokkai Dokkai, Nov 17, 2007
    #6
  7. 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(8)>,
    :a=>"aaa"}

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


    any idea ?
    --
    Posted via http://www.ruby-forum.com/.
     
    Pokkai Dokkai, Nov 18, 2007
    #7
  8. > 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
     
    Devi Web Development, Nov 18, 2007
    #8
  9. Pokkai Dokkai

    Phrogz Guest

    On Nov 17, 9:41 pm, Pokkai Dokkai <> 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.")
     
    Phrogz, Nov 18, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    0
    Views:
    417
  2. Don Sykes
    Replies:
    3
    Views:
    697
    Christophe Vanfleteren
    Feb 25, 2004
  3. Replies:
    0
    Views:
    508
  4. Els
    Replies:
    9
    Views:
    504
    Neredbojias
    Jan 8, 2009
  5. active
    Replies:
    4
    Views:
    300
    active
    Apr 3, 2007
Loading...

Share This Page