0
0
Rubyprogramming~10 mins

Hash as named parameters pattern 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 hash as named parameters.

Ruby
def greet(options = {})
  puts "Hello, #{options[:name]}!"
end
Drag options to blanks, or click blank then click option'
A[
B(
C.
D{
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets to access hash keys.
Trying to access hash keys without brackets or dot notation.
2fill in blank
medium

Complete the code to call the greet method with a name parameter.

Ruby
greet([1]: "Alice"})
Drag options to blanks, or click blank then click option'
Aname
Bname:
C"name"
D:name
Attempts:
3 left
💡 Hint
Common Mistakes
Using :name as a symbol key inside the hash literal incorrectly.
Using quotes around the key name.
3fill in blank
hard

Fix the error in the method definition to correctly accept named parameters as a hash.

Ruby
def greet([1])
  puts "Hello, #{options[:name]}!"
end
Drag options to blanks, or click blank then click option'
Aoptions = {}
Bname:
Cname
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to destructure named parameters without default hash.
Using a single symbol parameter instead of a hash.
4fill in blank
hard

Complete the code to create a hash from named parameters and access a value.

Ruby
def info([1] = {})
  age = params[:age]
  puts "Age is #{age}"
end
Drag options to blanks, or click blank then click option'
Aparams
B.
C[
Dparams = {}
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation to access hash keys instead of square brackets.
Not providing a default empty hash for the parameter.
5fill in blank
hard

Fill all three blanks to define a method with named parameters and print a greeting.

Ruby
def greet_user([1] = {})
  name = [2][:name]
  puts "Welcome, [3]!"
end
Drag options to blanks, or click blank then click option'
Aparams
Cname
Doptions
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not accessing the hash key correctly.