Thursday, December 11, 2008

Ruby Notes for Dec. 9th, 2008

Welcome new member: Rick Davis, one of our Colleagues from Millennium.
Glenn started with an introduction about Heroku, which is an online deployment system for Ruby on Rails apps. You can create a rails app via your browser, and then with an online editor, you begin to customize it.

Christophe then continued his lecture on regular expressions.Special "character classes":\d to detect a digit 0-9\w to detect alphanumeric 0-9, a-z, A-Z
\s to detect a whitespace (space, tab etc)
. to detect any character\D to detect anything but digit
\W to detect anything but alphanumeric
\S to detect anything but a whitespace
And usual quantifiers? : match zero or one+ : match one or more* : match zero or more{n,m} match n to m times (inclusive).
e.g. {11, 15} 11 to 15 times.
{, 5} 1 to 5 times.{3, } 3 or more times.Also one can defined new 'character class', with [ ]
Eg. [aeiouAEIOU] matches ONE character
Examples:
1. To match 617-679-7000
\d\d\d-\d\d\d -\d\d\d\d
Or
\d{3}-\d{2}-\d{4}
2. To match 26-DEC-2008 5:59pm\d{1,2}[\s-/]?\w{3}[\s-/]?\d{4}\s\d{1,2}:\d\d(ampm)?
[\s-/]? to detect a blank, tab, ‘-‘, ‘/’ or nothing.
(ampm)? To detect ‘am’, ‘pm’ or nothing (sp that the time part can math 12:44pm or 10:51am or 18:00).
To refine the accuracy of this expression, i.e. month should be jan, feb, ...; minute part can not be greater than 59; hour part can not be greater than 23. We will define month part as $1, hour part as $2, minute part as $3. ($ is similar as global variable in SAS).

We first use hash to define a format for month
Month = {‘Jan’ => 1
‘Feb’ => 2
.
.
.
‘Dec’ => 12}
line= ~/…/&& $2<24 && $3<60&&month.looking?$1

Also the hour part (any digits of 0-23) can be expressed as ([01]\d2[0123])

3. Get phone numbers
a = 617-679-7000
method 1: l=a.split(/[-\s/]/)=[‘617’, ‘679’, ‘6000’]
method 2 l=a.scan(/\d+/)=[‘617’, ‘679’, ‘6000’]

but for the following object.
a = my number is 617 953 7000 , yours is 785 365 1122 , oh?
Method 1 will result [‘my’, ‘number’, ‘is’, ‘617’, ‘953’, ‘7000’, ‘ ‘, ……]
Method 2will result [‘617’, ‘953’, ‘7000’, ‘785’, ‘365’, ‘1122’]

Assignment:
Write an expression for time only (or for time and second if you can)

No comments: