Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
Use square brackets to access the hash key :name inside the options hash.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using :name as a symbol key inside the hash literal incorrectly.
Using quotes around the key name.
✗ Incorrect
When passing named parameters as a hash, use the key with a colon after it, like name: "Alice".
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to destructure named parameters without default hash.
Using a single symbol parameter instead of a hash.
✗ Incorrect
To accept named parameters as a hash, define the method with an optional hash parameter like options = {}.
4fill in blank
hardComplete 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'
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.
✗ Incorrect
Define the method parameter as params = {} and access the :age key with square brackets.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not accessing the hash key correctly.
✗ Incorrect
Define the parameter as options = {}, access options[:name], and print the variable name.