Ruby Impressions

Thomas Gandy gandy at techie.com
Wed Jan 9 16:27:08 EST 2002


A while back there was a thread here comparing Python and Ruby.  It
inspired me to take a look at Ruby and I must say I like what I see
there.

Now I'm a recent Perl refugee and so a newbie in Python as well.  I
used to really like Perl a lot, but then I started doing OO Perl and
that convinced me that Perl was just too painful for OO.  A few months
back I checked out Python and it seemed to be an improvement from an
OO perspective.

I think that coming from Perl, though, that Ruby seems more
'comfortable' (I'm not gonna bring up the indentation issue ;-) and it
seems actually more Object Oriented.  (I never could understand why
you have to pass 'self' into methods in Python - you actually have to
to that in OO Perl as well).

There have been some complaints about Ruby's use of the characters
'$,@,@@' to indicate the scope of a variable and I must say that
initially I thought this a bit arbitrary, but now I think it actually
does help to see 'at-a-glance' what the scope of a variable is.  In
defining instance variables you're just replacing Python's self.var
with Ruby's @var. As I'm glancing through code I (and it's my opinion)
must say it's easier to see the '@''s than the 'self's (and there's a
bit less typing to do).  And it's not like Perl where you've got these
kinds of characters everywhere in the code so that they soon overwhelm
the eyes and become meaningless - In Ruby they're used with great
discretion so they retain the ability to draw the eye.

In Python we do:

class SomeClass:
   def __init__(self,value):
      self.data = value

In Ruby you do:

class SomeClass
   def initialize(value)
      @data = value
   end
end

Especially if the constructor or method is long, it's easy to see the
instance variables in a Ruby class. Sure,they need those 'end's in
Ruby, but I don't find them all that problematic. The fact that I can
get by without all those 'self's seems to make up for the 'end's.

I also like Ruby's operator overloading.  If you want to overload '+',
you define a '+' method instead of an __add__ method.

Other things I like about Ruby:
* classes and modules are always open (including the built-in ones)so
you can add methods to them at anytime or change methods which makes
for a lot of flexibility.
* built-in types like Hash and Array are extendable and you can
inherit from them.
* yield (iterators) - though this is now in Python 2.2 as well
*dRuby - very easy to use distributed objects

Some things I miss:
* docstrings (though there are probably ways of creating similar
things in Ruby)
* in (though you can define one in Ruby, but it'll be called with '.')

I don't miss multiple inheritance.  I tend to avoid it anyway and Ruby
does have module mixins which in many cases makes up for the lack of
MI.

Overall I like Ruby and I think I'll be using it more and more.  I'm
wondering if others here have tried Ruby and if so what their
impressions are.
(No language wars intended)

--gandy



More information about the Python-list mailing list