- Chained comparisons (
3 < 5 < 7
=> True) ; Exception in Ruby
-
puts "foo" if condition
-
IdentationError
sucks! -
predicate (and bang) methods with
?
(and!
) (e.g. :defined?
) -
No need to call most methods with
()
-
Operator overloading and "special" methods name clearer in Ruby:
class Foo def +(n) end def [](el) end end foo + bar foo[:bar]
class Foo(): def __add__(self, n): # ... def __getitem__(self, el): # .... foo + bar foo[:bar]
-
"List comprehensions" (_can be simplier in Python in some cases)
a = (0..9).to_a alphabet = ("a".."z").to_a
l = [i for i in range(10)] alphabet = string.lowercase[26]
-
Nicer conversion methods (OO style):
1.to_f # => 1.0 [1, 2, 3].to_s # => "[1, 2, 3]"
float(1) # => 1.0 str([1, 2, 3]) # => "[1, 2, 3]"
-
Nicer blocks (+ lambdas/procs) e.g.:
proc = -> (e) { puts "Current element : #{e}" } [1, 2, 3, 4].each(&proc)
-
Ability to manipulate basic objects such as
String
class String def foo "Let me introduce myself : #{self}" end end "Foo".foo # => "Let me introduce ..."
-
Implicit return statements
def foo # Same as with a `return` statement "bar baz" end foo # => "bar baz"
def foo(): "bar baz" not foo() # => True def foo(): return "bar baz" foo() # => "bar baz"
-
File manipulation (no example, I want to keep this gist clean!)