From: David MacQuigg Date: 2004-02-24T04:14:52+09:00 Subject: Re: Need examples comparing Ruby to Python On Mon, 23 Feb 2004 15:57:39 GMT, gabriele renzi wrote: >in the end, the quote should be >"Ruby does not have immutable objects like tuples. Using ! will >modify an object in-place, but you can make any object immutable with >freeze()" Good point. I've changed the statement in the wiki. >but maybe, you should undeline that ruby has had oo+iterators since >the beginning, while python added them later. This reflects in the >'feeling' of the language, I believe I'm trying to avoid differences that are no longer true, and stay neutral with regard to matters of personal preference, like the "feel" of the language. This should get us down to just a few examples where Ruby has an advantage. There probably aren't any simple examples left, since Python has added a lot in the last few years. Still, I think there may be a difference with examples that are a little more complex. I've been playing around with a modification of the "jukebox" example. If we have a problem requiring several string operations in sequence, we run into some missing methods in Python, and have to resort to either mappings or list comprehensions, which are not as simple and readable as a sequence of methods applied to the starting object. Here is what I would like to say in Python, but can't because line.split() produces a list, and the subsequent methods don't apply to lists. I'll bet this would work in Ruby, but I don't know enough Ruby to say for sure. file, length, name, title = line.split('|').strip.split.join() The goal is to produce a list of strings with whitespace and newlines stripped off the ends, and whitespace squeezed out of the middle of each string. The equivalent statement in Python, using OOP style and list comprehensions is: file, length, name, title = [' '.join(t.split()) for t in [t.strip() for t in line.split('|')]] which to me looks even worse than a pure functional style: file, length, name, title = map(join, map(split, map(strip, split(line, '|')))) ( Sorry about the formatting here. I can't get this all on one line in the newsreader. ) This *may* be our first example showing an advantage for Ruby beyond just a preference on style. While there are those who *prefer* the functional style, I think there is an advantage to a style in which the operations are typed in the same order that you have to visualize intermediate results. With a sequence of operations in FP style, I find myself miscounting parentheses, and having to repeatedly go back to the beginning of the line to insert the next operation. If someone can show me a nice way to do this in Ruby, I'll include it in our language comparison page, and challenge the Python programmers to match it in conciseness and clarity. -- Dave