Parameters with default values
📖 Scenario: You are creating a simple greeting system for a website. Sometimes users provide their name, and sometimes they don't. You want to write a method that can greet users by name if given, or greet them with a generic message if no name is provided.
🎯 Goal: Build a Ruby method called greet that takes one parameter name with a default value. The method should return a greeting message using the given name or a default greeting if no name is given.
📋 What You'll Learn
Create a method called
greet with one parameter name that has a default value of "Guest".Inside the method, return a string that says
"Hello, <name>!" where <name> is the parameter value.Call the
greet method twice: once without any argument, and once with the argument "Alice".Print the results of both method calls.
💡 Why This Matters
🌍 Real World
Default parameters are useful when you want to make your methods flexible and easy to use without always requiring all information.
💼 Career
Understanding default parameters helps you write cleaner, more maintainable code in Ruby, which is important for many programming jobs.
Progress0 / 4 steps