[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Need examples comparing Ruby to Python
From:       David MacQuigg <dmq () gain ! com>
Date:       2004-02-25 1:49:49
Message-ID: 5run30ddidul2ptaq8o6d64fqnu8lf4ed0 () 4ax ! com
[Download RAW message or body]

On Wed, 25 Feb 2004 05:06:02 +0900, Hal Fulton
<hal9000@hypermetrics.com> wrote:

>David MacQuigg wrote:
>
>> I'm not seeing any fundamental advantage of Ruby blocks over Python
>> functions.  If your block is more than one line, or needs a print
>> statement, just give it a name and let it be a function.  lambda's are
>> used only for very short blocks, where for example, you want to pass a
>> simple function in an argument list, but don't want to waste a line
>> giving that function a name.
>
>David,
>
>The *last* thing I want is to start a flame war, especially since my
>knowledge of Python is practically zero.

Hal,

You won't get any flames from me, especially since my knowledge of
Ruby is near zero. :>)

[...]
>Here is a simple example of a block that is both multi-line and has a
>print statement. (Granted it could be done in one line.)
>
>   words.each do |word|
>     consonants = word.gsub(/[aeiou]/,"")
>     puts consonants
>   end
>
>How would you do that with a named function, and why would the new way
>be better?
>
>If I do:
>
>   def func(word)
>     consonants = word.gsub(/[aeiou]/,"")
>     puts consonants
>   end
>
>   words.each {|word| func(word) }
>
>then I have still used a block, one that happens to call a function
>(with a name) that will only be used once.
>
>Is this better than the other way?

I would say "yes", but that is probably a personal bias due to my
better knowledge of Python.  In Python, I would say:
for word in words: func(word)
which seems like the most natural way to say it.

The original statement was a response to the complaint I've heard
numerous times that Python's lambda functions are no good ( one line
only, no print statements, etc.)  I wasn't being critical of Ruby
blocks, just saying that I see no reason to have them in Python.  99%
of the time, I just use a simple function.  For the few cases where I
want to squeeze these functions into a really tight space, I use
lambda functions.  The irony to me is that they wasted six characters
for the name 'lambda' and added it as a keyword to a language with
only 29 keywords.  I would rather see |x,y| instead of 'lambda x,y:'
in a tight space like an argument list.  I also like @name instead of
self.name, but that is a debate that has been going on for about ten
years on the Python lists.

Adding up all of the above "personal preference" pluses, they seem
about equal to the one big personal preference minus I see - the fact
that Ruby doesn't use indentation to establish structure.  When I
first started using Python, I thought that was strange, then I found
that I almost never made a mistake with indentation, and when I did,
the interpreter caught it right away.  The big plus is that I can now
read other peoples code and count on the indentation to mean
something.

-- Dave


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic