def sayhi(name, age)
puts ("hello " + name + " you are " + age.to_s)
end
sayhi("Mike", 42)
Back
puts vs. print
Front
Puts will print out what you specify with new line
Print will print output without a new line
Back
Comments
Front
# comment
OR
=begin
=end
Back
Setting Defaults in Methods
Front
If method has no value, then will default to assigned values.
def sayhi(name= "No name", age= 0)
puts ("hello " + name + " you are " + age.to_s)
end
sayhi
Back
IF | ELSE | ELSIF
Front
if x < y # Assumes x and y are defined
puts "x is less than y!"
elsif x > y
puts "x is greater than y!"
else
puts "x equals y!"
end
Back
Returns in method
Front
Back
Hash
Front
states = {
:California => "CA",
"New York" => "NY",
1 => "OR"
}
puts states[:California]
Back
Array
Front
friends = ['Kevin', 'Karen', "Oscar"]
puts friends[0, 2] # stops at Karen
puts friends[0] = "Dwight" # Replaces Kevin with Dwight