a Python person's experience with Ruby

MonkeeSage MonkeeSage at gmail.com
Sat Dec 8 01:30:09 EST 2007


On Dec 7, 11:08 pm, Steve Howell <showel... at yahoo.com> wrote:
> Python is my favorite programming language.  I've used
> it as my primary language for about six years now,
> including four years of using it full-time in my day
> job.  Three months ago I decided to take a position
> with a team that does a lot of things very well, but
> they don't use Python.  We use Ruby instead.  I'd like
> to share my observations about Ruby, because I think
> they say important things about Python, which has been
> my frame of reference.
>
> First of all, I actually enjoy programming in Ruby.
> Although I'm still fairly early on the learning curve,
> I feel like I've achieved basic fluency, and it
> generally stays out of the way.
>
> (A quick disclaimer is that some of the observations I
> make about Ruby may simply reflect my ignorance about
> the language.  I'm still learning it.)
>
> The thing that I like least about Ruby is its
> "require" mechanism.  Basically, when you do "require"
> in Ruby, it sort of pollutes your namespace.  I much
> prefer Python's explicitness.
>
> Some surprising things that I like about Ruby:
>
>   1) It has the Perlish natural language syntax of
> "raise 'foo' if condition."  I never missed having
> that syntax in Python, but now that I have it in Ruby,
> I use it quite often.
>
>   2) On a general note, Ruby is enough like Python
> that it doesn't bend my brain.
>
>   3) I actually like being able to omit parentheses in
> method definitions and method calls.  In Ruby you can
> express "add(3,5,7)" as both "add(3,5,7)" and "add 3,
> 5, 7."  The latter syntax is obviously more error
> prone, but I don't think I've ever actually gotten bit
> by it, and the code appears more clean to me.
>
>   4) Ruby forces you to explicitly make attributes for
> instance variables.  At first I found this clumsy, but
> I've gotten used to it, and I actually kind of like it
> in certain circumstances.
>
> What I miss about Python:
>
>   1) I like the fact that Python's syntax for passing
> around methods is very natural. Ruby's syntax is much
> more clumsy.
>
>   2) I miss indentation.  I get bitten by kEnd in Ruby
> all the time.
>
>   3) I miss Python's maturity.  Just to give a small
> example, Python's interpreter gives more readable
> syntax error messages.
>
> Those are the things that jump out for me.  I'm
> curious to hear what other people have learned about
> Python after maybe going away from it for a while.
>
>       ____________________________________________________________________________________
> Be a better friend, newshound, and
> know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

Hello,

Standard disclaimer: This response is in the spirit of sharing
knowledge and experience, as I take your post to be, not in the spirit
of advocacy of any language. All opinions I express are simply that:
my opinion.

I use both python and ruby quite often. But I came from the other
direction, starting with ruby then learning python. Overall, I think
they are both great languages, and I believe the choice in whether to
use one or the other (or both!) is determined mainly by expedience and
preference. I'll try to give my thoughts on your points, which I had
as I was learning python a few years ago, along with my current
opinions:

Ruby:

0.) I found python's concept of namespaces as defined by imported
modules (i.e., files) to be strange at first. I used "from foo import
*" a lot starting out. The main thing that I had to get my mind around
was that in ruby a namespace applies to an object (even "main", i.e.,
toplevel, is an instance of class Object), and you can "re-open"
objects to extend their namespace (or mix in "modules", which in ruby
are like a container of unbound methods that you can "include" in
another object and thereby bind those methods to that object). So
rather than "import" a module with the namespace "string," as in
python, in ruby you'd require a file that re-opens class String and
extends it. They are two different approaches (and two different ideas
of namespace pollution), but once you get your mind around both, they
are very easy to use. I almost never use "from foo import *" now in
python.

1.) I missed the infix version of conditionals that ruby provides at
first, and I would often write things like "if foo: bar" in python if
the line was short. Now it just feels more natural to write it on two
lines in python most of the time, though in ruby I still use the other
notation if the line is short.

2.) I found the same was true. I could basically translate 90% of the
ruby idioms directly into python (minor syntax issues aside), and vise
versa.

3.) I never have liked omitting parens in ruby method calls (with the
exception of methods taking no arguments, but I didn't even do that
for a long time), though many/most rubyists do use that style. One
problem with it is that it leads to ambiguities, and the parser
complains in those cases (e.g., "puts add 1, 2, 3"). However, with
predicate methods it does make it read more like a natural language
(e.g., "if Jordan.likes? :cookies"). I still parens though. ;)

4.) Yeah, it's hard when learning ruby, especially if coming from
languages that distinguish between methods and attributes, to get used
to thinking of "a.a" and "a.a=" as method calls and defining accessors
for those methods (or using one of the attr_* keywords) in the class
body. The equivalent python idiom is something like:

class A:
  __a = "foo"
  def __init__(self):
    self.a = A.__a

Which roughly translates to this in ruby:

class A
  attr_accessor :a
  def initialize
    @a = "foo"
  end
end

Python:

1.) I also found python's style of method referencing to be a lot more
intuitive than using something like ruby's "m = method('foo');
m.call('bar')" to get a reference to foo() and call it. It's alot
cleaner to say "m = foo; m('bar')", imo. But this goes back to the
omitting parens thing, which makes it impossible to do it that way in
ruby. Though, ruby does allow some interesting things by having it
that way, e.g., letting you substitute a Proc object (e.g., a block or
lambda) for a method reference transparently.

2.) I also find layout to be a nice feature. And since I've recently
been using Haskell, it has only re-inforced that opinion. But when I
first started using python, I got bitten by IndentationError quite
often. And I kept wanting to type some closing symbol, heh. ;)

3.) I also found (and do find) python's maturity to be nice in many
ways.

Regards,
Jordan



More information about the Python-list mailing list