1. Links
-
http://www.rubycentral.com/faq/rubyfaq-2.html 2.1 How Does Ruby Compare With Python?
2. Notes
References:
-
How to get a reference to method foo of object x
x.foo # Python x.methods(:foo) # Ruby
-
How to get a reference a Python "property"?
Iterators:
-
# Python: # provides ONE __iter__ interface: # (although there are many less-pretty ways around this...) class Foo: def __iter__(self): ... for x in iter(Foo()): # or: for x in iter([1, 2, 3, 4]): # what if we want a reverse iterator? # what if we want to traverse a tree? ... # Ruby: # provides any number of iterator interfaces through its # implicit block passing mechanism: ... -
Why don't these languages support Iterators that are more similar to those in Design Patterns?
Closures:
-
A closure has: code that can be executed and a context ...?
-
What is the special name for sorting that Perl programmers use?
Accessors (see also proxies):
-
Python "property" v. accessor?
-
...
Proxy:
-
# Python: proxying of attributes and methods? # Ruby: just proxy the methods
Python decorators:
-
# Python def onexit(f): import atexit atexit.register(f) return f @onexit def func(): ... # Ruby ???
3. Summarized pros/cons
| Pro Ruby | Rebuttle from Python |
| Easy meta programming | Adding methods to a class programmatically requires a lot more typing (thus it is discouraging) compared to Ruby |
| Ingrained uniform access, including metaprogamming helpers | Python provides the "property" method, but has the same problem as with its inconvenient metaprogramming; Could be possible to write your own class that overrides __getattr__ and __setattr__ to provide some level of blanket uniform access |
| Can easily attach new methods to existing classes, including builtin classes like String and Float | Two options: (1) write a function that lives outside the class but operates on its objects, (2) subclass or write a proxy |
| Ruby gems (CTAN for Ruby) makes getting new Ruby modules easy | Although Python doesn't have such a system, one could argue that Debian and Gentoo mitigate this problem |
| Ruby has some really awesome modules/apps, like for time+date storage and string processing, Ruby on Rails, ... | Python also has some neat modules like PyX |
| Real closures make it easy to transfer data between a callback function (iterator) and a context (the scope which defined the function) | Python simulates closures by copying the context, but sometimes this is not as elegant when they need to return data back into the defining scope; An additional level of indirection (via a class, list, dict, ...) is necessary in such situations. |
| When debugging, hard to match messages with corresponding receiving objects |
| Pro Python | Rebuttle from Ruby |
| List comprehensions | Usually Ruby's iterators (which are *everywhere*) can get you the same end result. Sometimes they work out better than list comprehensions, and sometimes they don't |
| Modula-3 style modules make it very easy to create LARGE projects with virtually ZERO worry about stepping on the toes of other modules | ... |
| Simple list slicing: [1:] means everything except the first item, [:-2] means everything except the last two items | Ruby has two ways of instantiating ranges (which are similar to xrange in Python): (1) 0..2 expands to [0, 1, 2], and (2) 0...2 expands to [0, 1]; [1..-1] means everything except the first item, [0...-2] means everything except the last two items |
| Con Ruby | ...and in Python? |
| Cannot overload some operators, such as and, or, ... | Same deal (I think) |
To be figured out:
-
When using Language/Ruby/method_missing, how do we deal with iteretors? (passing in blocks, or dealing with methods that have yield statements)
Additional language desires:
-
A large base of operator symbols (syntatic sugars), or *maybe* (this might get out of control though...) the ability to define your own new operators
-
Take operators from other language, such as (from Matlab/Octave):
./ .* ' .'
-
Establish useful conventions for multidimensional arrays (e.g. matrices) (perhaps similar to Matlab/Octave)
-
Built-in expression builder; When an expression is typed, it can (1) compute a result or (2) return an expression tree which the user/programmer is free to manipulate and transform