Tags: convert, dealing, hash, hashes, instead, key, keys, names, ofstrings, programming, ruby, rubyists, string, symbol, symbols, transform

transform hash key from string into symbol

On Programmer » Ruby

6,894 words with 7 Comments; publish: Thu, 01 May 2008 00:18:00 GMT; (20046.88, « »)

Hello Rubyists,

When dealing with hashes, I like using symbols for key names instead of

strings. Is there an easy way to convert hash keys from symbols into

strings?

E.g. I'd like to do something like this:

my_hash = YAML.load_file('my_file.yml')

=> { 'one' => 1, 'two' => 2, 'three' => 3 }

my_hash. super__hash_key_string_to_sym_transf

ormation #one line of

code?

..

my_hash

=> { :one => 1, :two => 2, :three => 3 }

Please display your ninja skills. : )

Thanks,

kodama

--

Posted via http://www.ruby-forum.com/.

All Comments

Leave a comment...

  • 7 Comments
    • class Hash

      def super__hash_key_string_to_sym_transf

      ormation

      self.each_key{|k| k = k.to_sym}

      self

      end

      end

      -or-

      my_hash.each_key{|k| k = k.to_sym}

      Either of these work for you?

      On Jan 1, 2008, at 3:26 PM, Old Echo wrote:

      > Hello Rubyists,

      > When dealing with hashes, I like using symbols for key names instead

      > of

      > strings. Is there an easy way to convert hash keys from symbols into

      > strings?

      > E.g. I'd like to do something like this:

      > my_hash = YAML.load_file('my_file.yml')

      > => { 'one' => 1, 'two' => 2, 'three' => 3 }

      > my_hash. super__hash_key_string_to_sym_transf

      ormation #one line of

      > code?

      > ...

      > my_hash

      > => { :one => 1, :two => 2, :three => 3 }

      > Please display your ninja skills. : )

      > Thanks,

      > kodama

      > --

      > Posted via http://www.ruby-forum.com/.

      >

      #1; Thu, 01 May 2008 00:20:00 GMT
    • Hi Steve,

      Thanks for your quick response, but I'm afraid that didn't do the trick.

      It looks like the block just completes without actually changing

      anything, or at least it doesn't transform what needs to be transformed.

      Do you have any other suggestions?

      Steve Ross wrote:

      > class Hash

      > def super__hash_key_string_to_sym_transf

      ormation

      > self.each_key{|k| k = k.to_sym}

      > self

      > end

      > end

      > -or-

      > my_hash.each_key{|k| k = k.to_sym}

      > Either of these work for you?

      Posted via http://www.ruby-forum.com/.

      #2; Thu, 01 May 2008 00:21:00 GMT
    • On Wed, Jan 02, 2008 at 08:26:31AM +0900, Old Echo wrote:

      > Hello Rubyists,

      > When dealing with hashes, I like using symbols for key names instead of

      > strings. Is there an easy way to convert hash keys from symbols into

      > strings?

      > E.g. I'd like to do something like this:

      > my_hash = YAML.load_file('my_file.yml')

      > => { 'one' => 1, 'two' => 2, 'three' => 3 }

      > my_hash. super__hash_key_string_to_sym_transf

      ormation #one line of

      > code?

      > ...

      > my_hash

      > => { :one => 1, :two => 2, :three => 3 }

      > Please display your ninja skills. : )

      class Hash

      def symbolize_keys

      replace(inject({}) { |h,(k,v)| h[k.to_sym] = v; h })

      end

      end

      > Thanks,

      > kodama

      --Greg

      #3; Thu, 01 May 2008 00:22:00 GMT
    • Sorry. Look at Greg's for the keen Ruby way to do it. There should be

      a symmetric stringify_keys for consistency as well.

      My hash works as such:

      class Hash

      def symbolize_keys

      t = self.dup

      self.clear

      t.each_pair{|k, v| self[k.to_sym] = v}

      self

      end

      end

      h = {'one' => 1, 'two' => 2}

      puts h.symbolize_keys.inspect

      {:one=>1, :two=>2}

      Again, Greg's is er.

      On Jan 1, 2008, at 3:53 PM, Old Echo wrote:

      > Hi Steve,

      > Thanks for your quick response, but I'm afraid that didn't do the

      > trick.

      > It looks like the block just completes without actually changing

      > anything, or at least it doesn't transform what needs to be

      > transformed.

      > Do you have any other suggestions?

      > Steve Ross wrote:

      > --

      > Posted via http://www.ruby-forum.com/.

      >

      #4; Thu, 01 May 2008 00:23:00 GMT
    • Thank you both for your answers. It's threads like these that make me so

      so so happy to be programming with Ruby. :)

      --

      Posted via http://www.ruby-forum.com/.

      #5; Thu, 01 May 2008 00:24:00 GMT
    • FYI, if you prefer, there is also 'facets' gem which has what you want. Yo=

      u can selectively require parts of it. I think it's facets/hash_keyize.

      http://facets.rubyforge.org/index.html

      -andre

      > Date: Wed, 2 Jan 2008 09:29:11 +0900

      > From: kodama.ruby.todaysummary.com.bluexpanse.net

      > Subject: Re: transform hash key from string into symbol

      > To: ruby-talk.ruby.todaysummary.com.ruby-lang.org

      >=20

      > Thank you both for your answers. It's threads like these that make me so

      > so so happy to be programming with Ruby. :)

      > --=20

      > Posted via http://www.ruby-forum.com/.

      >=20

      ________________________________________

      _________________________

      Share life as it happens with the new Windows Live.

      http://www.windowslive.com/share.ht...sharelife_1220=

      07=

      #6; Thu, 01 May 2008 00:25:00 GMT
    • On Jan 1, 9:28=A0pm, Andreas S <andrea....ruby.todaysummary.com.hotmail.com> wrote:

      > FYI, if you prefer, there is also 'facets' gem which has what you want. =

      =A0You can selectively require parts of it. I think it's facets/hash_keyize.

      =

      You're right, but that is the old library. Instead use Hash#rekey

      require 'facets/hash/rekey'

      {"a"=3D>1}.rekey #=3D> {:a=3D>1}

      {:a=3D>1}.rekey(&:to_s) #=3D> {"a"=3D>1}

      {"a"=3D>1}.rekey{|k| k.capitalize} #=3D> {"A"=3D>1}

      T.

      #7; Thu, 01 May 2008 00:26:00 GMT