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.
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’
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:
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]
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:
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:
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:
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: