Ruby 101: It's a script 5

Posted by jeff Wednesday, June 14, 2006 19:33:00 GMT

My brain has been on the code-compile-run cycle for a long time.

So long in fact, that when I first started learning Ruby I didn’t realize that Ruby is a scripting language. People told me, and I read words like “Ruby is a scripting language” many times, but I guess i just didn’t understand.

That is, I understood the words, but I didn’t understand the full meaning. I had used scripting languages before – grudgingly – like VBScript and a tiny bit of JavaScript, and I also knew that .asp pages were “script”.

But sometimes us code-compile-run monkeys need to be hit over the head. Consider this quick example. Here’s my file, test.rb:

class App

  puts "weird... i'm inside a class declaration right now" 

  def hello
    puts "ok, now this feels normal again." 
  end

end

Ok, to use C# developers, this looks like a class declaration. But in Ruby, it’s much more than that.

Supposed you run this in a command window:

C:\dev> ruby test.rb

What do you think will happen? It’s just a class declaration, right? The hello method is never actually called.

Wrong! Ruby script is executed one statement at a time on the fly. If you run this code, you’ll get this:
C:\dev> ruby test.rb
weird... i'm inside a class declaration right now
C:\dev>

Code inside a class definition but outside any def blocks are executed.

Back in the day, when I first saw has\_many :books in Rails, I thought it was overriding a base class method or something. Had no idea it was actually executing a method, right then and there, called has\_many and passing it the parameter :books.

When I finally figured it out, a light went on, a brief angel voice could be heard in the distance, and I realized of course… it’s a script.

Comments

Leave a response

  1. Packagethief   June 15, 2006 @ 05:03 PM

    I had a similar revelation once upon a time. If you haven't done so already, I'd highly recommend you pick up a copy of David Black's book, 'Ruby for Rails'. He has a number of examples not unlike the one you just gave, and really helps to demystify some lesser-known facts about Ruby. I've been programming in Ruby for over a year now, and lightbulbs still go on every day. Isn't Ruby great?

  2. Marc   June 16, 2006 @ 01:26 PM

    Nice post and thanks Packagethief for the book reco. One of these days, I'm going to get a bit more into Rails. I've dabbled a bit but haven't done anything real with it. Closest I came is some prototyping work that I did in CakePHP.

  3. Jeff   June 16, 2006 @ 02:24 PM

    David's book is really great, and that's why we've put a link to it right in our sidebar (near the bottom).

    I consider it a must read for anyone who seriously wants to take the next step in their Ruby programming.

    David is also a great contributor to the Ruby community in so many ways.

    So if anyone doesn't have it yet, click on our link and buy it today.

  4. kino   May 24, 2008 @ 01:10 AM

    Separated modes of consciousness, in the broadest Cartesian sense, need to be criticized with regard to their validity and range, before they can be used for the purposes of a radical grounding of the transcendental-phenomenological reduction, by reconciling with noematic descriptions; I set myself the all-embracing task of uncovering multiplicities of the Objective world by a freely actualizable return to the stream of multiplicities of the fundamental form of this universal synthesis.

  5. Ellroy   May 30, 2008 @ 11:18 PM

    In the study of time, the pure employment of metaphysics should only be used as a canon for the intelligible objects in space and time.

Comment


(won't be published)