Here's a not very useful function:
def add x, y
x + y
end
def
means "define a function"add
is the name of the functionx, y
are the parameters of the functionx + y
is the body of the function
You call a function by its name
def add x, y
x + y
end
add 2, 3 # returns 5
add 12, 30 # returns 42
def rant s
s.upcase.gsub(" ", "") + "!!!"
end
puts rant "i like pizza"
def initial_cap s
s[0] + s[1,s.length].downcase
end
puts initial_cap("McElaney")
this program prints Mcelaney
def titleize string
string.split(' ').map(&:capitalize).join(' ')
end
&:
means "send this message"map(&:capitalize)
means "send the message capitalize
to every item in the array"