Introduction To Ruby
Methods

Programmer-defined methods in Ruby are the equivalent of functions and procedures in other high-level languages. There is a little more to it than that but we're trying to start Ruby quickly here.

Example 1

This example carries out a statement based on the value of n supplied to the method.

# Square method

def square(n)
  puts "#{n} x #{n} = #{n*n}"
end

# call the method
square(5)

gets

Example 2

This example uses the return statement to return a value that we can assign to variables in the main part of the program.

# Square method

def square(n)
  return n*n
end

# call the method
a = square(5)
puts a

gets