Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Convert month string to number
Adgar Marks
7/26/2008 11:03:00 PM
Hi all,
I use linux OS. I would like to convert a file with the folloing format:
-rw-r--r-- 1 student student 11289 26. Jul 22:41 listing.txt
into FileName, Year, Month, Day
In order to do this, i would like to convert the month string from "Jul"
in to the number 7.
I wrote the following code:
def ConvertMonthToNumber(MonthStr)
case MonthStr
when "Jan" then 1
when "Feb" then 2
when "Mar" then 3
when "Apr" then 4
when "May" then 5
when "Jun" then 6
when "Jul" then 7
when "Aug" then 8
when "Sep" then 9
when "Okt" then 10
when "Nov" then 11
when "Dec" then 12
end
end
when i run this code, i get the following error:
SortList.rb:7: formal argument cannot be a constant
def ConvertMonthToNumber(MonthStr)
^
My questions are:
1. Is there already a function that does in Ruby?
2. How to correct this bug?
Thanks
--
Posted via
http://www.ruby-...
.
1 Answer
David A. Black
7/26/2008 11:08:00 PM
0
Hi --
On Sun, 27 Jul 2008, Adgar Marks wrote:
> Hi all,
>
> I use linux OS. I would like to convert a file with the folloing format:
> -rw-r--r-- 1 student student 11289 26. Jul 22:41 listing.txt
>
> into FileName, Year, Month, Day
>
> In order to do this, i would like to convert the month string from "Jul"
> in to the number 7.
>
> I wrote the following code:
>
> def ConvertMonthToNumber(MonthStr)
> case MonthStr
> when "Jan" then 1
> when "Feb" then 2
> when "Mar" then 3
> when "Apr" then 4
> when "May" then 5
> when "Jun" then 6
> when "Jul" then 7
> when "Aug" then 8
> when "Sep" then 9
> when "Okt" then 10
> when "Nov" then 11
> when "Dec" then 12
> end
> end
>
> when i run this code, i get the following error:
> SortList.rb:7: formal argument cannot be a constant
> def ConvertMonthToNumber(MonthStr)
> ^
>
> My questions are:
> 1. Is there already a function that does in Ruby?
> 2. How to correct this bug?
1. require 'date'
Date::ABBR_MONTHNAMES.index("Jun") # 6
2. Use a variable instead of a constant as your parameter. Variables
start with lowercase letters or _ (underscore).
David
--
Rails training from David A. Black and Ruby Power and Light:
* Advancing With Rails August 18-21 Edison, NJ
* Co-taught by D.A. Black and Erik Kastner
See
http://www.r...
for details and updates!
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Convert month string to number
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password