Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a class method named greet.
Ruby
class Person def [1].greet puts "Hello!" end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'self' to define class methods.
Forgetting to prefix the method name with 'self.'
✗ Incorrect
In Ruby, class methods are defined by prefixing the method name with
self. inside the class.2fill in blank
mediumComplete the code to call the class method greet on the Person class.
Ruby
Person.[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the method on an instance instead of the class.
Using a wrong method name.
✗ Incorrect
Class methods are called on the class itself, so use
Person.greet to call the method.3fill in blank
hardFix the error in the class method definition by completing the blank.
Ruby
class Calculator def [1].add(a, b) a + b end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'self' in Ruby.
Omitting the prefix and defining an instance method instead.
✗ Incorrect
Class methods must be defined with
self. prefix, not 'this' or other keywords.4fill in blank
hardFill both blanks to define a class method description that returns a string.
Ruby
class Book def [1].[2] "A great read" end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'self' for the prefix.
Using wrong method names like 'info'.
✗ Incorrect
Class methods use
self prefix and the method name is description here.5fill in blank
hardFill all three blanks to define and call a class method info that returns a string.
Ruby
class Animal def [1].[2] "Friendly animal" end end puts Animal.[3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in definition and call.
Forgetting the
self prefix in the method definition.✗ Incorrect
The class method is defined with
self.info and called as Animal.info.