[Courses] [Ruby] Lesson 0: Installing, References, and your first homework assignment

Laurel Fan laurel.fan at gmail.com
Sat Nov 12 10:50:35 EST 2005


On 11/8/05, Katie Bechtold <katie at hoteldetective.org> wrote:
> Hi, I'm Katie!

> My favorites
> among those are Python and Forth, both of which are flexible and
> elegant, and both of which feature an interactive mode where you can
> script "on the fly".

You should definitely check out irb then, if you haven't already.

> I found a Ruby application called rubyPod[2].  It's "a graphical
> frontend for managing a iPod on Linux."  Here's a snippet from its
> song.db:
>
> class Song
>   include Comparable
>
>   ...
>
>   def <=>(songb)
>     if @pathSong==songb.pathSong
>       0
>     else
>       #if the path is not the same then the songs cannot be equal
>       if @nameArtist.downcase<songb.nameArtist.downcase
>         -1
>       elsif @nameArtist.downcase>songb.nameArtist.downcase
>         1
>       else
>         if @nameAlbum.downcase<songb.nameAlbum.downcase
>           -1
>         elsif @nameAlbum.downcase>songb.nameAlbum.downcase
>           1
>         else
>           @numSong.to_i<=>songb.numSong.to_i
>         end
>       end
>     end
>   end
> end
>
> What we have here is a method inside the Song class.  This method is
> overloading Ruby's general comparison operator '<=>', which returns
> -1, 0, or 1 depending on whether its left-hand operand is less then,
> equal to, or greater than its right-hand operand.  Here, I think the
> current Song object serves as the left-hand operand, and another
> Song object denoted by 'songb' serves as the right-hand operand.
>
> The first comparison in this function definition tests whether a
> song is being compared to itself, to which it should be considered
> equal.  The next test compares the lowercase version of the artist's
> name (for case-insensitive sorting).  If those are equal, this
> method compares the lowercase version of the album name.  If those
> are also equal, it casts the songs' track numbers to integers and
> compares them using the <=> operator for integers.

Great explanation!

A few other interesting things about this code:  Comparable is a mixin
(for those experienced with other OO languages, mixins are kind of
like multiple inheritance but actually not at all like it).  Including
Comparable lets you get a whole bunch of comparison operators
(between?, ==, <, >=, etc.), simply by implementing the <=> function. 
Here's some documentation on Comparable:

http://ruby-doc.org/core/classes/Comparable.html

Enumerable is another very useful mixin.  We'll learn more about those
in Lesson 3, in the Modules chapter of the book.

Another thing you may notice is that there are no 'return' statements
anywhere.  Like perl and matlab (and probably other languages), if
there is no explicit return statement, the return value of the method
is the last statement evaluated.  We'll learn about methods in Lesson
1 (and then more about them in Lesson 2).

--
Laurel Fan
http://dreadnought.gorgorg.org


More information about the Courses mailing list