[newbie] Hash to String ?

J

Josselin

I need to produce this kind of result

l = { "January" => "1"}, {"February => "2"}, { "March" => "3"}, .... }

so I wrote

h = (Array.new(13) {|i| Hash[Date::MONTHNAMES, i.to_s]}).slice(1,12)
which results in :
[{"January"=>"1"}, {"February"=>"2"}, {"March"=>"3"}, {"April"=>"4"},
{"May"=>"5"}, {"June"=>"6"}, {"July"=>"7"}, {"August"=>"8"},
{"September"=>"9"}, {"October"=>"10"}, {"November"=>"11"},
{"December"=>"12"}]

but I cannot write : l = { h } it's wrong... Hash cannot be
converted like that..
what should I write instead ?

thanks

joss
 
J

Josselin

I need to produce this kind of result

l = { "January" => "1"}, {"February => "2"}, { "March" => "3"}, .... }

so I wrote

h = (Array.new(13) {|i| Hash[Date::MONTHNAMES, i.to_s]}).slice(1,12)
which results in :
[{"January"=>"1"}, {"February"=>"2"}, {"March"=>"3"}, {"April"=>"4"},
{"May"=>"5"}, {"June"=>"6"}, {"July"=>"7"}, {"August"=>"8"},
{"September"=>"9"}, {"October"=>"10"}, {"November"=>"11"},
{"December"=>"12"}]

but I cannot write : l = { h } it's wrong... Hash cannot be
converted like that..
what should I write instead ?

thanks

joss


sorry for this silly question..
soem homework... and got the solution :

def dropdown_month(default)
months = []
1.step(12, 1) do |i|
l = Date::MONTHNAMES
m = i.to_s
months << [ l, m ]
end
return options_for_select(months, default)
end

gives me the correct structure

joss
 
J

Jan Svitok

I need to produce this kind of result

l = { "January" => "1"}, {"February => "2"}, { "March" => "3"}, .... }

so I wrote

h = (Array.new(13) {|i| Hash[Date::MONTHNAMES, i.to_s]}).slice(1,12)
which results in :
[{"January"=>"1"}, {"February"=>"2"}, {"March"=>"3"}, {"April"=>"4"},
{"May"=>"5"}, {"June"=>"6"}, {"July"=>"7"}, {"August"=>"8"},
{"September"=>"9"}, {"October"=>"10"}, {"November"=>"11"},
{"December"=>"12"}]

but I cannot write : l = { h } it's wrong... Hash cannot be
converted like that..
what should I write instead ?


What do you want to do exactly? Where the String from the title?
If you want to have an Array of Hashes, that's what you've got. If you
want to have
a Hash of Hashes, then you need to provide another set of keys {x => {
y=> z}, ...}
If you want to have just one Hash, then try

h = Hash[*(1..12).map {|i| [Date::MONTHNAMES, i.to_s]}.flatten)]
 
J

Josselin

I need to produce this kind of result

l = { "January" => "1"}, {"February => "2"}, { "March" => "3"}, .... }

so I wrote

h = (Array.new(13) {|i| Hash[Date::MONTHNAMES, i.to_s]}).slice(1,12)
which results in :
[{"January"=>"1"}, {"February"=>"2"}, {"March"=>"3"}, {"April"=>"4"},
{"May"=>"5"}, {"June"=>"6"}, {"July"=>"7"}, {"August"=>"8"},
{"September"=>"9"}, {"October"=>"10"}, {"November"=>"11"},
{"December"=>"12"}]

but I cannot write : l = { h } it's wrong... Hash cannot be
converted like that..
what should I write instead ?


What do you want to do exactly? Where the String from the title?
If you want to have an Array of Hashes, that's what you've got. If you
want to have
a Hash of Hashes, then you need to provide another set of keys {x => {
y=> z}, ...}
If you want to have just one Hash, then try

h = Hash[*(1..12).map {|i| [Date::MONTHNAMES, i.to_s]}.flatten)]


thanks.. (as mentionned in a second post I found a solution)
I try to rewrite these 6 lines into one line :

months = []
1.step(12, 1) do |i|
l = Date::MONTHNAMES
m = i.to_s
months << [ l, m ]
end

which produces :
[["January", "1"], ["February", "2"], ["March", "3"], ["April", "4"],
["May", "5"], ["June", "6"], ["July", "7"], ["August", "8"],
["September", "9"], ["October", "10"], ["November", "11"], ["December",
"12"]]

I need the array months as a parameter for 'options_to_select()'
(Rails function)
 
J

Jeremy Wells

Josselin said:
I need to produce this kind of result

l = { "January" => "1"}, {"February => "2"}, { "March" => "3"}, .... }

so I wrote

h = (Array.new(13) {|i| Hash[Date::MONTHNAMES, i.to_s]}).slice(1,12)
which results in :
[{"January"=>"1"}, {"February"=>"2"}, {"March"=>"3"}, {"April"=>"4"},
{"May"=>"5"}, {"June"=>"6"}, {"July"=>"7"}, {"August"=>"8"},
{"September"=>"9"}, {"October"=>"10"}, {"November"=>"11"},
{"December"=>"12"}]

but I cannot write : l = { h } it's wrong... Hash cannot be
converted like that..
what should I write instead ?

months = Date::MONTHNAMES.compact.collect{|m| [m,
Date::MONTHNAMES.index(m)]}
 
J

Josselin

months = Date::MONTHNAMES.compact.collect{|m| [m, Date::MONTHNAMES.index(m)]}

thanks , that's what I was looking for...
doesn't come at first hand for a newbie minded..
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,008
Messages
2,570,270
Members
46,872
Latest member
Stephendes

Latest Threads

Top