Ruby Impressions

Andrew Dalke dalke at dalkescientific.com
Sat Jan 12 17:10:49 EST 2002


Thomas Gandy:
>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.

I listed some of my concerns in that earlier thread you mentioned.
The google archive for that post is

http://groups.google.com/groups?selm=a0eqq9%24dio%241%40nntp9.atl.mindspring
.net&output=gplain

I mentioned that I do a lot of work with non-programmers, that is,
chemists, biologists, and physicists who want to get their research
done, and not necessarily how to program.  For those people, having
to remember all sorts of special symbols, like $, @, @@, is more
work.

I can think of three advantages to using a short character:
  - you can fit more on a line
  - shorter to type
  - it's easier to identify in a scan of the code

Regarding the first point, most code, even Python code, doesn't
take up all 80 characters, so the additional space savings isn't
worth it.  Especially since Python doesn't need all the 'end'
statements so really saves on vertical space, which is much more
important.

Regarding the second, my fingers type self. pretty quickly these
days.  And I'm not limited by my typing speed.

Regarding the third, I don't know.  For you it makes a difference.
I seem to see "self."s pretty quickly, but I haven't done any
sorts of tests on it.


>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.

One of the other points I made was that it's easier to tell people
that "__" function are "special" to Python while the lack of
syntactic distinction makes them harder to recognize in Ruby.  And
more likely to have a user-defined method name accidently use what
is a system-referenced name.

You mentioned scanning the code for special symbols.  It seems to
me that looking for __init__ is easier than looking for initialize,
and in general looking for "__"s gives an easy way to see all the
special methods that have been defined.

More lines of code on a page is very useful.  Filling those
lines up with 'end' statements isn't.

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

I like that as well.  I wonder how hard that would be to add to Python.
Err, thinking about it, it isn't quite that easy.  What's the syntax
to distinguish unary from binary operators, as -x vs. x-y?  And what
about the reverse operators, like __radd__?

That also becomes an alias for the existing mechanism, and I don't
like aliases.

>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.

Classes and modules in Python are open, but as I mentioned I think it's
a bad practice to modify them except in very restricted cases, like
debugging.  I don't like how in Ruby that practice is encouraged.  I
think it can lead to two different packages trying to add objects
with the same name but with different meanings.

(Some built-in objects cannot be modified.  Then again, it's possible
to have user-defined objects which cannot be modified, or only modified
with extreme difficulty.)

>* built-in types like Hash and Array are extendable and you can
>inherit from them.

Python 2.2 supported inheritance from built-in data types.  They
cannot be extended in place.  I consider this a really good thing.

>* yield (iterators) - though this is now in Python 2.2 as well

Uncle Tim pointed out the differences between these two 'yield's.

>*dRuby - very easy to use distributed objects

Depending on what you mean, there's dozens of ways to do this in
Python.  I've written my own on top of pickle and HTTP.  There's
CORBA, XML-RPC (std. in Python 2.2), SOAP, PYRO, PVM, and more.
And Guido did some research in distributed agents using Python.

I mentioned a few other concerns I had with Ruby in that references
usenet post.  What do you think of them?

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list