ruby · level 1 · unit 7 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a slot where your method catches information.
Picture your method as a machine. Right now it does the same thing every time. Cut a slot in the front, and you can feed in something new on each go.
That slot is a parameter. It is a variable in the brackets of the def line, like name in def greet(name). Inside the method, use it like any other variable.
The value you drop in when you call is the . Call greet("Mia") and name holds "Mia". Call greet("Leo") and the same machine greets Leo instead.
def greet(name)
puts "Hello, #{name}!"
end
greet("Mia")
greet("Leo") For two slots, separate the parameters with commas, and the arguments too.
guess first
Two slots this time. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
def order(food, drink)
puts "I would like #{food} with #{drink}."
end
order("lemonade", "chips")
your turn
Your turn. Run the birthday card machine.
Type a name between each pair of quotes and an age after each comma. Then run it and watch one machine make two cards.
fix it
Rex is hungry, but this crashes. Can you fix it?
- Read the error. Which name has Ruby never heard of?
- The parameter on the
defline is called pet, but line 2 uses a different name. - Change animal to pet inside the curly brackets, then press run.
nailed it
your methods take orders now. :D
- A parameter is a slot in the brackets of the
defline. - An argument is the value you drop in when you call.
- Arguments fill parameters in order: first into first.
new words in your collection
- parameter
- argument
This lesson isn’t saved. A free account remembers every lesson you finish.