[Courses] [Ruby] Lesson 0 homework

Rachel McConnell rachel at xtreme.com
Wed Nov 9 21:53:35 EST 2005


> First, introduce yourself.  Do you know any other programming
> languages?  What do you like/not like about them?  Why are you
> interested in Ruby?  What are some things you'd like to learn to do?

Hello All, and thanks Laurel for organizing this!

I've been programming in Java for about 5 years now, mostly webapps, 
meaning that I use html and javascript extensively as well.  I used to 
focus specifically on telephone apps, writing the UI in VoiceXML; but 
testing your app by calling it up on the phone sucks really hard!  I can 
get around in C# & ASP, and PHP, and get into trouble in Perl.  I have 
to say I like them all except PHP, which makes it far too easy to write 
bad code, and far too hard to write good code.  Good for beginners, though.

> 
> Second, find and run any ruby program.  It can be something you wrote,
> something you downloaded (hint, look on RubyForge), an example you
> copied out of a book, something a friend wrote for you, etc.
> 
> Finally, look at the code.  Find a section of about 5-20 lines of
> code, and try to figure out what it does.  If you figure it out (or
> just have a good guess), explain it to us.  If not, show us the code
> and we'll try to explain it.

I've got a very simple example, "cat" implemented in Ruby.  I more or 
less wrote it - I found parts from here & there and put them together. 
But I do understand what each line does, more or less, and it will run 
if you put it in a file by itself.  But remove the line numbers first, 
which I've added for reference!  Here goes:

----
  1  #! /usr/bin/ruby
  2
  3  fileName = ARGV[0] ? ARGV[0] : exit
  4
  5  begin
  6      IO.foreach(fileName) { | line |
  7          puts line
  8      }
  9  rescue
10      $stderr.puts $!
11  end
----

LINE 1: the shebang line (i assume it's still called that in Ruby!) 
Lets you run it without typing "ruby" first:

$ ./cat.rb filename.txt

of course, you can still run it through ruby:

$ ruby cat.rb filename.txt


LINE 3: Get the name of the file from the arguments list and assign it 
to the variable, fileName.  (This is a SIMPLE cat function that only 
looks at the first argument and ignores any others.)  If no argument is 
provided, just stop right there.  I used the ternary operator, see 
footnote [0] if you aren't familiar with it.


LINE 5: Starts a block of code.  I'm almost certain that begin/end (see 
line 11) are there as bookends to the rescue statement (line 9) to 
explicitly delimit a block of code that is likely to throw an exception. 
  If the rescue wasn't needed, the begin/end wouldn't be either.


LINE 6: This is the heart of cat.  IO is (I believe) a builtin Ruby 
object that handles file input & output.  Its foreach method takes a 
file name as argument, opens the file, and iterates through the lines in 
the file.

The bit with the pipe symbols after the open bracket is a mixin, and 
it's the thing I'm least familiar with here....  I think it defines the 
variable name for the object returned by the foreach method, for use 
within the scope of the code block that comes after it.  I think it is a 
little like the for each syntax, in Java 5:

List<Line> linesInFile = getLines();
for( Line line : linesInFile ) {
    // do stuff
}

but, obviously, shorter.  I do know that "line" isn't a Ruby reserved 
word here or anything, I tried "blarg" also and that worked just fine.

Anyone can talk more about this, please do!


LINE 7: puts is Ruby's print function.


LINE 8: ends the scope of the variable, line (or blarg)


LINE 9: rescue appears to be a Ruby catch statement.  IO stuff is always 
dangerous and error-prone and most languages offer some way of 
recovering from IO errors (no file, weird characters, permissions 
problems, and so forth).


LINE 10: what to do if there IS an error - print it out to standard 
error (which may or may not be the same place a plain puts writes to).


LINE 11: remember the begin statement in line 5?  we're done with that 
block now.


That's all, then.  I have to say that an in-depth analysis like that 
helped ME a ton even for such a short little scrap of code.  NEXT!

Rachel



[0] The ternary operator is like a shortened IF/ELSE statement.  It 
looks like this:

TEST ? TRUE_RESULT : FALSE_RESULT

As an IF statement, it would look like this:

if( TEST ) {
   TRUE_RESULT;
} else {
   FALSE_RESULT;
}

It's called "ternary" because it has three arguments; it's the only 
operator that takes more than two.


More information about the Courses mailing list