views:

154

answers:

3

this is the problem:

inside a lib i need to print MONTHNAMES to string

if i try

Date::MONTHNAMES.inspect

result is

=> "[nil, \"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"]"

that's good but i don't need the first element, so

month_names = Date::MONTHNAMES
month_names.shift
month_names.inspect

but

ActionView::TemplateError (can't modify frozen array)

there is any workaround? thanks

+1  A: 
Date::MONTHNAMES.slice(1,12).inspect

gives you all month (without the first nil).

andre-r
+1  A: 

As showed in the error message Date::MONTHNAMES is a frozen object so you can not modify it(shift will modify it by ,well, shifting out the first element). You can achieve what you want by:

puts Date::MONTHNAMES[1..-1].inspect

pierr
A: 

Although the slice/array indexing solution is probably better here, you can always dup a frozen array and work on the copy:

month_names = Date::MONTHNAMES.dup
month_names.shift
month_names.inspect

should give you what you want.

Kyle