Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a method that accepts a keyword argument named name.
Ruby
def greet([1]:) puts "Hello, #{name}!" end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a positional argument instead of a keyword argument.
Naming the keyword argument differently than used inside the method.
✗ Incorrect
The method parameter must be named
name as a keyword argument to match the usage inside the method.2fill in blank
mediumComplete the code to call the greet method with the keyword argument name set to "Alice".
Ruby
greet([1]: "Alice")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the argument without the keyword.
Using a wrong keyword name.
✗ Incorrect
The method expects a keyword argument named
name, so we must pass name: "Alice".3fill in blank
hardFix the error in the method definition to provide a default value for the keyword argument name.
Ruby
def greet([1]: "Guest") puts "Hello, #{name}!" end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
name= or name = instead of name.Omitting the colon after the keyword argument name.
✗ Incorrect
Keyword arguments use a colon after the name, and default values are assigned after the colon.
4fill in blank
hardFill both blanks to define a method that accepts two keyword arguments, name and age, with default values.
Ruby
def info([1]: "Unknown", [2]: 0) puts "Name: #{name}, Age: #{age}" end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the colon after the keyword argument names.
Using equals sign instead of colon for keyword argument names.
✗ Incorrect
Keyword arguments must have a colon after the name. Both
name and age are correct syntax.5fill in blank
hardFill all three blanks to call the info method with keyword arguments name set to "Bob" and age set to 25, and print the result.
Ruby
info([1]: "Bob", [2]: [3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keyword names.
Passing age as a string instead of a number.
✗ Incorrect
The method expects keyword arguments
name and age. We pass name: "Bob" and age: 25.