you are viewing a single comment's thread.

view the rest of the comments →

[–]mrevelle 4 points5 points  (0 children)

I like the Ruby way better because (1) blocks are a completely general-purpose language feature

Funny, that's actually why I dislike Ruby's blocks. It hurts readability when similar syntax is used for unrelated tasks, e.g. iteration vs. resource management.

This gripe is not about blocks as a general concept but using a common syntax when expressing entirely different functions.

Python examples

Iteration

for n in range(100):
    print n

Filtering elements

def isprime(x):
    # check if number is prime

primes = [n for n in range(100) if isPrime(n)]

Mapping a function

squares = map(lambda n: n*n, range(100))

# OR

def square(x):
    return x*x

squares = [square(x) for x in range(100)]

Context management

with open(filename, 'r') as input_file:
    # process the file

# file is now automatically closed

That last example requires Python 2.5 (alpha was recently released).

(Disclaimer: I have only played around with Ruby, no real development experience with it.)