|
|
|
|
|
by Mikeb85
1684 days ago
|
|
In Python some standard operations are global functions and some are methods. Which are which isn't exactly clear. It's also kind of irrational and random but I hate seeing __init__ everywhere. For example, to find the length of an array in Python you use the function len(). In Ruby you call the method .length. As the article is about, loops in Python are for blah in blah. In Ruby they're blah.each and for loops are just sugar (not sure why they're there but you can avoid them). In Ruby pretty much everything is an object and you just call methods. Very few keywords, global functions, etc... Also, as the other poster said, if you call a[0] on an empty array, in Ruby you get nil, in Python it throws an error. Not sure which is better or considered more 'proper', but Ruby's behaviour is what I'd personally expect in a dynamic language. |
|
Typically, all of the former are also the latter because the global function just calls the corresponding method. E.g., len(x) works by calling x.__len__().
> As the article is about, loops in Python are for blah in blah. In Ruby they're
...for blah in blah, if you choose to use them at all, which has the same semantics as the python.
It relies on method calls to #each under the hood, and usually in Ruby you won't bother with for/in, but it is there still...