0
0
Rubyprogramming~5 mins

Method declaration with def in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What keyword is used to declare a method in Ruby?
The keyword def is used to declare a method in Ruby.
Click to reveal answer
beginner
How do you end a method declaration in Ruby?
You end a method declaration with the keyword end.
Click to reveal answer
beginner
What is the correct way to declare a method named greet that prints 'Hello'?
Use:<br>
def greet
  puts 'Hello'
end
Click to reveal answer
intermediate
Can Ruby methods return values? How?
Yes, Ruby methods return the value of the last executed expression automatically.
Click to reveal answer
intermediate
What happens if you call a method without parentheses in Ruby?
Ruby allows calling methods without parentheses if there are no arguments, making code look cleaner.
Click to reveal answer
Which keyword starts a method declaration in Ruby?
Adef
Bfunction
Cmethod
Ddeclare
How do you end a method declaration in Ruby?
Astop
Bend
Cfinish
Dclose
What will this Ruby method return?<br>
def add
  2 + 3
end
A5
B2 + 3
Cnil
DError
Which of these is a valid method call in Ruby?
Agreet()
Bgreet
CNone
DBoth A and B
What is the output of this code?<br>
def say_hi
  puts 'Hi!'
end
say_hi
Asay_hi
Bnil
CHi!
DError
Explain how to declare a simple method in Ruby using def and end.
Think about the start and end keywords and what goes in between.
You got /4 concepts.
    Describe how Ruby methods return values and how you can call them.
    Remember Ruby's automatic return and flexible syntax.
    You got /3 concepts.