Compartir a través de


10 Unique Ruby Features

The 10, but 3 (implicit return values) and 8 (only nil+false are False) are negative features.

FYI here's the 10 and how they compare with Python:

  1. Objects everywhere! -- Python supports OO but not solely, and has strong reflection features regardless of objects or not
  2. Blocks -- Python's function objects and 'with' keyword and list comprehension provide almost the same net result.
  3. Implicit return value in methods -- Python doesn't do this (thankfully)
  4. In Ruby everything is open! -- Python's comparable (but see below)
  5. Missing unary operators in Ruby -- Python's comparable
  6. Ruby supports parallel assignment -- Python's comparable
  7. In Ruby strings are mutable -- Python strings are immutable, but operations are performed on them (unlike Java and StringBuffer/StringBuilder) so comparable in almost all ways (Ruby wins on a few exotic cases, Python wins on some of the common cases - slight edge to Python).
  8. True and false in Ruby -- Python has sane evaluations for True and False
  9. Native support for ranges and regular expressions -- Python's regular expressions are as functional as Ruby but lack some of the notational convenience. Python has nothing like Ruby's Ranges (a cool feature, though rarely used in my experience)
  10. Method indicators -- Python doesn't do this

On #4, Ruby provides access control via the usual public/protected/private keywords, but it's a lie as you can trivially access protected and private symbols (even unknowingly). Python eschews such false protections and follows the convention things-with-a-leading-underscore-are-not-'meant'-for-public-consumption. All in all, I prefer Python's approach on this one. Ruby's solution sounds good at first (especially if you have C++ or Java experience), but in practice the Python solution works and 'feels' better.

For those looking to tally up a score card:

  1. Tie
  2. Slight edge to Ruby
  3. Python
  4. Tie
  5. Tie
  6. Tie
  7. Tie. Ruby's mutable-and-immutable-string-methods are an unnecessary complexity in the common cases, but are slightly advantageous in some exotic scenarios. Overall, they're comparable.
  8. Python
  9. Slight edge to Ruby. Ranges are handy but not overly significant, and Ruby's regex notation is more convenient for simple comparisons but falls into the ugly-Perl-mess beyond that (x=~/\s*(\w+)\s*/ ; puts $1). If you use Ruby's non-Perl-isms (e.g. Match object) then it's no different from Python.
  10. Slight edge to Ruby. I personally like the ? and ! suffixes to indicate boolean and mutable methods, but in the end they're just convention; good symbol names matter more. Of course if I get to choose, I'd take both :-)

Python and Ruby are different, but have a lot of common properties.

And in the end, the library and frameworks are at least as important (or more so) than the language.