methods Ruby

Estimated reading time: 1 minute

methods

methods is a function . it start with def keyword.

def method_name(parameters)

end
  • demo 1
def method_name
  ## method body goes here
end
  • demo 2 / parameter
def method_name(msg)
  puts msg
end
  • demo 3 / multiple parameters
def method_name(msg, body)
  puts "This is " + msg + "body is here" + body
end

return

def add_three(number)
  return number + 3
end

returned_value = add_three(4)
puts returned_value

source

ruby