Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert month string to number

0 views
Skip to first unread message

Adgar Marks

unread,
Jul 26, 2008, 7:02:51 PM7/26/08
to
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-forum.com/.

David A. Black

unread,
Jul 26, 2008, 7:07:46 PM7/26/08
to
Hi --

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.rubypal.com for details and updates!

0 new messages