From: Duong Quang Ha Date: 2012-06-24T11:23:59+09:00 Subject: Re: Stripping symbol character from hash turned to array Hello, On Sun, Jun 24, 2012 at 8:38 AM, Emeka Patrick wrote: > Hi, new to programming and Ruby and trying to work through some problems > and understand the language. Right now I'm doing work with hashes and > ran into a question. > > Took this Hash > > people = { >  :fred => 23, >  :joan => 18, >  :pete => 54 > } > > and turned it to an Array via  "people_array = people.to_a" > > now I have > > people_array =  [[:fred, 23], [:joan, 18], [:pete, 54]] > In this array, *:fred* is a symbol and *:* only is notation for a symbol when you puts it. > If I want to keep it as a nested Array how can I go about stripping the > ":" from what used to be the key. Please help and if possible, explain. > > Thanks, > > Emeka > > PS > I've tried gsub, but I get undefined method for Symbol > > 1.9.3p194 :140 > people_array.each do |k, v| > 1.9.3p194 :141 >     k.gsub!(':', '') > 1.9.3p194 :142?>   end > NoMethodError: undefined method `gsub!' for :fred:Symbol >  from (irb):141:in `block in irb_binding' >  from (irb):140:in `each' You can do as follow: > people_array.each do | k, v | > puts k.to_s > end It this code snippet, I convert each symbol to string, so the *:* disappear (just for demo) You can try the following code fragment to more clearly: 1.9.2p320 :016 > person = :fred => :fred 1.9.2p320 :017 > person.class => Symbol 1.9.2p320 :018 > person.to_s => "fred" 1.9.2p320 :019 > person.to_s.class => String 1.9.2p320 :020 > person.to_s.intern => :fred 1.9.2p320 :021 > person.to_s.intern.class => Symbol Best regards, -- Duong Quang Ha (Hà Quang Dương in Vietnamese) blog.haqduong.com