10 unique ruby language features

Learning Ruby is easy if you already know a programming language. But there are some ruby language features which can cause headache for a programmer migrating from C++ or Java. In this post, I will look at 10 interesting language features that are unique to Ruby.

1. Objects everywhere! – In Ruby everything is an object including simple numeric values. Here is an example,

  5.times { print "Hello World" }

2. Blocks – Blocks is a powerful feature in Ruby which simplifies programming. Blocks are code blocks which can be passed as a parameter to a method. Using this feature it is easy to build code libraries which can delegate varying functionality to code blocks to be built later. Here is an example which prints out all elements in an array,

  sample_array.each { |element| print element }

3. Implicit return value in methods – Value of the last expression in a method becomes the return value of the method. In Ruby return keyword is optional. In the following example, 8 is the return value,

  def testMethod
    x = 4+4

4. In Ruby everything is open! – In Ruby you can easily extend classes and modules. This means that nothing in Ruby (including built in classes and modules) are closed!. Interestingly additional methods can be added to a class even at runtime. Here is an example in which we add a previous method to FixNum class (which is the data type for all integers),

  class Fixnum
    def previous
      return self-1

  6.previous  # prints 5

5. Missing unary operators in Ruby – Unary operators ++ and — are not supported in Ruby. Instead you can use += operator.

6. Ruby supports parallel assignment – It is possible to change multiple variables in a single assignment. The best example is the swapping of two variables as given below,

  a , b = b, a

7. In Ruby strings are mutable – In Ruby it is possible to change a string variable in place. Hence unlike Java, the same string literal when used multiple times will point to different object instances.

  print "hello".object_id
  print "hello".object_id   # prints a different value

  a = "hello"
  a[1] = "a"
  print a  # prints hallo

8. True and false in Ruby – In Ruby only nil and false evaluate to false. This means that everything else evaluates to true! Hence even the value 0 evaluate to true in ruby. Following code snippet will output “Hello World” on console,

if (0) then
  print "Hello World"

9. Native support for ranges and regular expressions – Ruby language has native support for regular expressions and ranges.  This is backed up with a rich set of API methods. Control structures such as a case statement natively supports ranges for value comparison.

10. Method indicators – In Ruby the last character of a method name can indicate its behaviour. If the method ends with a question mark it indicates that the return value is boolean. If the method ends with an exclamation, it indicates that the method can change the state of the object. In many cases a non exclamation version of the method is provided which modifies a copy of the object.

2 Responses to “10 unique ruby language features”

  1. Either you haven’t done any research at all, or you don’t quite know what “unique” means, but there isn’t a single unique feature mentioned in your list-of-10. See my response (http://hinternets.blogspot.com/2009/05/unique-just-like-everybody-else.html) for more details.

  2. Hi Joe,

    I think I should have mentioned that these 10 features are unique in the sense they are all available in the same language at the same time. Of course, any feature you take it will be available in some language out there :-)

Leave a Reply