0
0
Rubyprogramming~10 mins

Method declaration with def 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 declare a method named greet.

Ruby
def [1]
  puts "Hello!"
end
Drag options to blanks, or click blank then click option'
Ahello
Bsay_hello
Cgreet
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than 'greet'.
Forgetting to write the method name after 'def'.
2fill in blank
medium

Complete the code to declare a method named add that takes two parameters.

Ruby
def [1](a, b)
  a + b
end
Drag options to blanks, or click blank then click option'
Aadd
Bsum
Cplus
Dcombine
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than 'add'.
Leaving out the parameters.
3fill in blank
hard

Fix the error in the method declaration by completing the method name.

Ruby
def [1](name)
  puts "Hello, #{name}!"
end
Drag options to blanks, or click blank then click option'
Ahello
Bgreeting
CsayHello
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Using camelCase instead of snake_case.
Choosing a method name that does not match the greeting.
4fill in blank
hard

Fill both blanks to declare a method that returns the square of a number.

Ruby
def [1](num)
  num [2] 2
end
Drag options to blanks, or click blank then click option'
Asquare
B*
C**
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication (*) instead of exponentiation (**).
Choosing a method name that does not describe squaring.
5fill in blank
hard

Fill all three blanks to declare a method that greets a person with their name and age.

Ruby
def [1](name, age)
  puts "Hello, [2]! You are [3] years old."
end
Drag options to blanks, or click blank then click option'
Agreet_person
B#{name}
C#{age}
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use string interpolation syntax.
Using a method name that is too generic.