# Hash to String?

**URL:** <https://www.ruby-forum.com/t/hash-to-string/70191>\
**Category:** Ruby\
**Created:** [September 27, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/hash-to-string/70191 "2006-09-27T13:00:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 27, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/hash-to-string/70191/1 "2006-09-27T13:00:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 27, 2006, 1:34pm UTC](https://www.ruby-forum.com/t/hash-to-string/70191/2 "2006-09-27T13:34:39Z")

</div>

On 2006-09-27 12:58:48 +0200, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> {“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[i]
    m = i.to_s
    months << [l, m]
  end
  return options_for_select(months, default)
end

```

gives me the correct structure

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 27, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/hash-to-string/70191/3 "2006-09-27T14:50:49Z")

</div>

On 2006-09-27 13:35:35 +0200, “Jan S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > {“May”=\>“5”}, {“June”=\>“6”}, {“July”=\>“7”}, {“August”=\>“8”},  
> > 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], 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[i]
    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)

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 27, 2006, 1:36pm UTC](https://www.ruby-forum.com/t/hash-to-string/70191/4 "2006-09-27T13:36:40Z")

</div>

On 9/27/06, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> {“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], i.to\_s]}.flatten)]

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 27, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/hash-to-string/70191/5 "2006-09-27T16:15:03Z")

</div>

Josselin wrote:

> {“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)]}

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 28, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/hash-to-string/70191/6 "2006-09-28T13:41:42Z")

</div>

On 2006-09-27 16:13:05 +0200, Jeremy W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

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