Archive for the 'Ruby' Category

Returning values from a function, Ruby style

June 13, 2009

A Ruby function automatically returns the value of the last expression in that function. It takes a little time to get used to this, if you are from PHP/Java background, like me. You’ll have to return values explicitly in those languages. Take this simple example.

def get_full_name(first_name, last_name)
  first_name + " " + last_name
end

It is equivalent to the following, but more elegant.

def get_full_name(first_name, last_name)
  full_name = first_name + " " + last_name
  return full_name
end