0
0
Rubyprogramming~10 mins

Keyword arguments in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aage
Bgreeting
Cmessage
Dname
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.
2fill in blank
medium

Complete 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'
Aname
Bage
Cgreeting
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the argument without the keyword.
Using a wrong keyword name.
3fill in blank
hard

Fix 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'
Aname =
Bname=
Cname
Dname:
Attempts:
3 left
💡 Hint
Common Mistakes
Using name= or name = instead of name.
Omitting the colon after the keyword argument name.
4fill in blank
hard

Fill 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'
Aname
Bage
Cname:
Dage:
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the colon after the keyword argument names.
Using equals sign instead of colon for keyword argument names.
5fill in blank
hard

Fill 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'
Aname
Bage
C25
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keyword names.
Passing age as a string instead of a number.