Last Updated: February 25, 2016
·
324
· prosperva

Why I Like Ruby

I have been playing with Ruby for sometime now and the satisfaction has been great. Coming from quite a number of programming languages, I am really amazed by the brevity in Ruby.

The problem

Say we are looking to determine if a word is a palindrome. Define palindrome, a palindrome is a word that is the same spelled in either direction (examples of those words: otto, alula, rada, racecar). So we can create a function to determine that:

In ruby, you can write it in a couple of ways:

Way #1:

def check_word (word)
  if (word == word.reverse)
    return true
  else
    return false
  end
end

Way #2 (Take out return keyword):

def check_word (word)
  if (word == word.reverse)
    true
  else
    false
  end
end

Way #3 , ruby evaluates the last statement

def check_word (word)
  word == word.reverse
end

Way #4 lets put everything on one line

def check_word (word) ; word == word.reverse; end;

The dynamics in Ruby are really amazing.

Enjoy!!!