« OSCON 2007 Underway | Main | Building Scalable Internet Architectures »

July 23, 2007

Mike Fitzgerald: Learning Ruby

Sitting in the Monday morning session of OSCON 2007 listening to Mike Fitzgerald's Learning Ruby tutorial.

Mike recommends the following books:
Ruby in a Nutshell (Matz)
Programming Ruby (Dave Thomas)
Ruby Cookbook (Lucas Carlson, Leonard Richardson)
Learning Ruby (Mike Fitzgerald)
Mike also refers to the ruby-doc site (generated by rdoc) as a great place for documentation about classes and methods.

Mike gives kudos to Matz, the creator and founder of Ruby.

Matz wanted to create a language since high school. Ruby development started in 1993, was first released 1995 (same year as Java). Didn't get attention in the west until around 2000, 2003 really into mainstream because of Ruby on Rails. Ruby was named by the person who wrote irb, the Ruby runtime environment who's birthstone was ruby.

What is Ruby? Object oriented. Interpreted, not compiled. Written in C, allows have C extension. Takes ideas from Lisp, Perl, Smalltalk etc.

Numbers are objects, not primitives. Most operators are methods. No declarations or static types (dynamic typing). Ruby does "duck" typing, treats variables. Ruby doesn't have ++ or --, but does allow abbreviated assignment. Can use underscores in numbers, which get ignored by the interpreter (1_000_000). Ranges can be inclusive (1..10) or exclusive (1...10).

Installing Ruby is pretty easy in most environments, if it isn't there already.

Mike now goes into a series of basic commands, looking at simple syntax examples running in irb.

For writing scripts #!/usr/bin/env ruby. Within a script use the backticks (`) or use the system method to execute a system call from within the script.

This line of code is interesting:

3.times { print "Go!" }

The 3 is an object (number) that has methods.

Variables start with a lowercase letter or _. Instance variables are @name, class variables are @@name, global variables ar $name (Matz discourages use of globals). Variables can be assigned in parallel (x, y = 37, 63).

Mike gives some interesting examples of very entrenched Java programmers that have switched wholesale. Switching a large Java application to RoR took about 10% of the time.

Ruby uses symbols as placeholders for strings and objects, makes it more efficient because there is only one copy of the data.

Expression substitution is done inside brackets prepended with #:

name = "guy"
puts "Hello #{name}"
puts "First argument #{ ARGV[0] }"

Different syntax for using a printf (string formatting) function:

hi = "Hello, %s"
puts hi % "people"

Works this way because % is actually a method that substitues the argument into the string.

Method definition looks like:

def hello
puts "Hi"
end

Can create aliases for methods as well:

alias hi hello

The hello method is in the ojbect main.

Ruby (return "Hello") automatically provides a method return value. Unless explicitly specified, the value returned from a method is the last evaluated expression.

In Ruby you do not overload methods, but you can override them. Ruby allows variable number of arguments which make overloading unnecessary. There is no typing of the arguments, but you could verify that the arguments belong to the correct class.

The block is very powerful in Ruby:

["Hi","Mike"].each { |e| print e }

Mike goes into detail about conditionals in Ruby (if, then, else, elsif, unless, begin, end, break, while, until, case, when). Can use colon or blocks to separate. Ruby allows you to put the conditional at the end of the statement (like Perl). Very nice for readability. In an array context, the >> pushes object onto an array.

Creating classes in Ruby is fairly straightforward. Each class will have an instantiation method run when object.new is defined.

Modules hold method definitions. You cannot instantiate modules, but you can include modules (referred to as mixin). Methods are implemented in modules but not available until the module is included.

Posted by mike at July 23, 2007 8:35 AM