- Define a method that takes in required and optional arguments
- Use
puts
to output a string - Define a return value
We've demonstrated the basics of defining methods, printing output, and returning values. Let's combine the concepts we've shown you to create a "Meal Choice" in our Ruby program.
We're attending a wedding and the newlyweds-to-be want to know what types of food they should provide at the banquet. Define a method named meal_choice
that has three parameters:
- Two required parameters for vegetables. Ex:
veg1
andveg2
- And an optional parameter for
protein
with a default value ofmeat
You should write your code in the file lib/meal_choice.rb
.
Whether you're omnivorous, vegetarian, or vegan, you're going to be eating a nutritious meal!
- First, in the body of your method, have the program
puts
"What a nutritious meal!" - Next, let's also have it
puts
"A plate of #{protein} with #{veg1} and #{veg2}." so that you can also see what you've ordered.
Top Tip: Remember, that puts
has a return value of nil
, so we need to
give our method an explicit return value.
The hosts are going the meal information to keep track of what everyone is eating, so make sure the return value includes the three food items you've chosen by returning: "A plate of #{protein} with #{veg1} and #{veg2}."
Note: An explicit or implicit return can be used.
You're all set! You've successfully written a series of Ruby methods utilizing all of the various Ruby basics we've discuss thus far. Now, we'll teach you about scope, and how information can be passed around to different methods.