Stripping symbol character from hash turned to array

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

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:inblock in irb_binding’
from (irb):140:in `each’

Hello,

On Sun, Jun 24, 2012 at 8:38 AM, Emeka P. [email protected]
wrote:

}

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.

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,

On Sun, 24 Jun 2012 10:38:28 +0900
Emeka P. [email protected] wrote:

}

and turned it to an Array via “people_array = people.to_a”

now I have

people_array = [[:fred, 23], [:joan, 18], [:pete, 54]]

You can iterate through them all if you really need it:

people_array.to_a.map{ |a| a.map(&:to_s) }

But, I don’t see any point to do so, as for example if you want
stringify this Hash - all symbols will automatically become strings and
so on. Also if you’ll need to output this as a list:

people_array.to_a.each do |pair|
  name, age = pair
  puts "#{name} is #{age} years old"
end

*Disclaimer: the code above demonstrates work with symbols, not
iterating through Hash or Array


Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: [email protected]

*Origin: Happy Hacking!

On Sun, 24 Jun 2012 10:38:28 +0900
Emeka P. [email protected] wrote:

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.

As other responders already said, “:” is only a notation for Symbol. It
has nothing to do with being a key in a Hash. In fact you can use
Strings as keys for your Hash and then Hash#to_a will produce directly
the result you want:

{ “fred” => 23, “joan” => 18, “pete” => 54 }.to_a
=> [[“fred”, 23], [“joan”, 18], [“pete”, 54]]

Although if you want to use Symbols as Hash keys and then having
Strings in resulting nested Array, you can use Hash#map method to do
the transformation:

{ :fred => 23, :joan => 18, :pete => 54 }.map { |key,value| [key.to_s, value] }
=> [[“fred”, 23], [“joan”, 18], [“pete”, 54]]

Thanks for the help and the explanation behind everything, I think I
have a handle on it now!

Thanks again,

Emeka

Michal Karas wrote in post #1065824:

On Sun, 24 Jun 2012 10:38:28 +0900
Emeka P. [email protected] wrote:

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.

As other responders already said, “:” is only a notation for Symbol. It
has nothing to do with being a key in a Hash. In fact you can use
Strings as keys for your Hash and then Hash#to_a will produce directly
the result you want:

{ “fred” => 23, “joan” => 18, “pete” => 54 }.to_a
=> [[“fred”, 23], [“joan”, 18], [“pete”, 54]]

Although if you want to use Symbols as Hash keys and then having
Strings in resulting nested Array, you can use Hash#map method to do
the transformation:

{ :fred => 23, :joan => 18, :pete => 54 }.map { |key,value| [key.to_s, value] }
=> [[“fred”, 23], [“joan”, 18], [“pete”, 54]]