Here's a silly 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
Lab: write a multiply
method and use it to multiply 123 * 456
def rant s s.upcase.gsub(" ", "") + "!!!" end puts rant "i like pizza"
Lab: use "rant" to rant about something really important!!!
def initial_cap s s[0].upcase + s[1,s.length] end puts initial_cap("smith") puts initial_cap("deniro")
Lab: capitalize a few things
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"/
#