Friday, December 19, 2008

15December2008

Agenda
regex
github
exceptions

Class started off with 3 book recomendations:
Unix Powertools
Pragmatic Thinking and Learning
Ruby Visual Quickstart Guide

Tom asked about compiling Ruby to an executable. Glenn mentioned several projects working on this:
Rubinius
Maglev

Glenn spoke briefly about Github. He also mentioned another versioning platform called Subversion and its predecessor CVS.

Regex - Regular Expressions

The homework for this week was to design a regular expression that will pick off a time. In US or European format and with and without the AM/PM designation.
12:35 AM
12:35
17:49

Christophe pointed us at Rubular which is a a Ruby regular expression editor/testing site. You can add a regular expression and aim it at a piece of text and see if it works.

First up was this expression
\d{1,2}:\d{2}\ (ampm)?

This expression matches 12:23 or 12:24 am, but won't match 12:23am, 12:23 AM and will match some crazy, impossible times like 28:23 am.

These next expressions I need to edit to get working:

(([012]?[1-9]1[1,2]):([0-5]\d)\s*(ampmAMPM)?)
d{1,2}:\d{2} (am\pm)?
([0]?[123456789][1}[12]):[00-59] (ampm)?
[0-5]\d\s*
(([0]?[123456789][1}[12]):[00-59] (ampm)?)([01\d2[0123]:[0-5]\d))
(0?[1-9]1[1,2]):([0-5]\d)\s*(ampm)
([01\d2[0123]:[0-5]\d)

Cristophe also spoke about how ruby and perl are greedy:
(I missed the details of this example)
<.*>
so you add a question mark
<.*?>


Christophe put an expression up that was similar to this
(0?[1-9]1[1,2]):([0-5]\d)\s*(AMPM)
and demonstrated that using parentheses Ruby will capture the matched parts of the expression into a series of global variables.
Given 12:35 AM
$&=12:35 AM
$1=12
$2=35
$3=AM

Exceptions

puts "Give me a number"
one=gets.chomp.to_fputs
"Give me another number"
two=gets.chomp.to_f
puts one/two
dont put important stuff here begin puts one/two puts "xx"
will not puts this if there is an exception

rescue =>e rescue ZeroDivionError=>e
puts "you can't divide by zero!"
end

rescue =>e take the object and put it into e
puts "an exception occured"
puts "its called #{e}"
end

ensure
puts "XX"else puts 'things are cool"
end

Future Topics

Take a feature and give a 20 minute talk:
IO JohnG Jan 13
Logging yw ?

Active Record some time after the holiday ~1/20
Might need to understand it a bit before the class. It is a bit more advanced that what we`ve been doing.

Monday, December 15, 2008

Class for December 16th

We are on for the 17th session for the Intro to Object Oriented Programming class. We're going to meet at the usual time, at 4:30, and in the usual place, the Rotary Conference room at the Central Square library.

The plan is to continue with Regular Expressions, go over the homework (which was to come up with a regex to match a time -- 12:12 am or 23:07) and to introduce exceptions, the object oriented way to catch errors.

Hope you can all make it. This will be the final session for 2008. After tomorrow night, we will resume on January 6th, 2009.

Glenn

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)

Monday, December 8, 2008

The Boston Ruby Group is meeting on December 9th

I will be going, at least for part of the meeting. It goes from 7 - 9, and it's at Sermo, a company located near Kendall Square.

Anyone interested in joining me?

Glenn

Ruby session #16 is on for tomorrow night

Hi All,

The Ruby class is on for tomorrow night, December 9th. We are planning to meet at the usual place, in the Rotary Conference Room of the Central Square Library (on Pearl Street off of Mass. Ave) at the usual time.

Christophe is planning to continue his introduction to Regular Expressions. He went over RegExp for about 15 minutes last week, so if you missed last week, I am sure that you won't have any trouble getting caught up during tomorrow's session.

The Boston-area Ruby group usually has a lecture-style meeting on the second Tuesday of each month. It is not clear to me that the meeting is on for tomorrow. I am trying to find out. If it is on, I will be going, and of course anyone who wants to join me is welcome.


Glenn

Friday, December 5, 2008

More notes from the December 2nd Session

Here are Christophe's notes from the session:

We went over a few basic things 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

And usual quantifiers
? : match zero or one
+ : match one or more
* : match zero or more
{n} ({12}) match n times
{n,m} match n to m times (inclusive)

Also one can defined new 'character class', with [ ] Eg. [aeiouy] matches ONE character but it has to be a lowercase vowel
[0-9a-z] matches ONE character that is any digit or lowercase letter (but cannot be an uppercase letter.
[0-9][a-z][A-Z] matches 1aX or 0xA but not 2ax etc.

A special thing with this character class [ ] thing is the caret (^) to introduce the negative:
[^aeiouy] means match one character as long as it is not a lowercase vowel

Exercice

To match

20-DEC-2008 5:59pm

We proposed:

\d\d-\w{3}-\d{4}\s\d:\d\d\w\w

Because \w means any alphanumeric, the expression above would also match
99-DEC-0001 5:59pm
Or
12-ABX-4512 5:59pm
(and also 20-DEC-2008 5:59ab)

We refined the time 5:59pm to match also 12:44pm or 10:51am \d{1,2}:\d\d\w\w

And then even
\d{1,2}:\d\d[am]p
(to match only am or pm...)

That was about it this time...

Tof'

Wednesday, December 3, 2008

Notes from Dec. 2nd Session

We started the class at 5:15 instead of the usual 4:30 because of a meeting at Millennium Pharmaceuticals (where most of the regular students and I work).

For the first half hour or so, we talked about the direction we want to take the Introduction to Object Oriented Programming with Ruby class. There are a number of possibilities.

We still have at least a half dozen basic aspects of the language to get through.

For example, these are the basic Future Topics as I see them:
  • Regular Expressions
  • Logging (Logger and log4r)
  • Testing (Test::Unit)
More advanced Furture Topics include:
  • ActiveRecord
  • Sockets
  • Testing (RSpec)
  • Rails
We talked about the need to write more programs as part of the class. To that end, I brought up an idea that I've been toying with for the last 6 months or so, and that is to write a statistical programming Domain Specific Language (DSL) in Ruby. (Statistical programming is the kind of programming that most of us in the Intro to Programming class do. I will write more on this in a future blog post.)

Everyone in the group seemed to think that writing more programs as part of the class -- more advanced programs than the simple homework assignments that I've been giving so far -- would be a great way to learn the language.

Christophe spent the last 15 or 20 minutes of the session introducing regular expressions to the group. Most of the group (other than Christophe, of course) has had only limited experience with RegExp, so this is basically new territory for us. The example Christophe presented was to extract a date from a string, with the date in the format DD-MMM-YYYY HH:MM.

More on the Regular Expression part of the Dec. 2nd session will follow in a future post.

Glenn

Welcome to RubyRx

Welcome to the RubyRx Blog.

I plan to use this blog to post updates and information regarding the Introduction to Object Oriented Programming with Ruby classes held in Central Square, Cambridge, MA.

Stay tuned for further updates.

Thanks!

Glenn Ritz