0
0
Rubyprogramming~10 mins

Parameters with default values 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 with a default parameter value.

Ruby
def greet(name = [1])
  puts "Hello, #{name}!"
end
Drag options to blanks, or click blank then click option'
A"name"
BGuest
Cname
D"Guest"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the default string value.
Using a variable name instead of a string literal.
2fill in blank
medium

Complete the code to call the method with and without the default parameter.

Ruby
def greet(name = "Guest")
  puts "Hello, #{name}!"
end

greet([1])
greet
Drag options to blanks, or click blank then click option'
AAlice
B"Alice"
Cname
D"name"
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the method with a name without quotes.
Confusing the method call syntax.
3fill in blank
hard

Fix the error in the method definition with a default parameter.

Ruby
def add_numbers(a, b = [1])
  a + b
end
Drag options to blanks, or click blank then click option'
Anil
B"0"
C0
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "0" instead of number 0 as default.
Using a variable name as default value.
4fill in blank
hard

Fill both blanks to create a method with two parameters, one with a default value.

Ruby
def order(item, quantity = [1])
  puts "Order: #{quantity} #{item}(s)"
end

order("apple", [2])
Drag options to blanks, or click blank then click option'
A1
B"apple"
C5
Dquantity
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number for quantity.
Confusing parameter names with values.
5fill in blank
hard

Fill all three blanks to define a method with default parameters and call it correctly.

Ruby
def book_flight(destination = [1], seats = [2])
  puts "Booking #{seats} seats to #{destination}."
end

book_flight([3])
Drag options to blanks, or click blank then click option'
A"Paris"
B2
C"Tokyo"
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of parameters.
Using numbers as strings or vice versa.