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.

No comments: