0
0
Rubyprogramming~10 mins

Class methods with self prefix 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 class method named greet.

Ruby
class Person
  def [1].greet
    puts "Hello!"
  end
end
Drag options to blanks, or click blank then click option'
Aclass
Bself
Cthis
Ddef
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'self' to define class methods.
Forgetting to prefix the method name with 'self.'
2fill in blank
medium

Complete 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'
Agreet
Bnew
Chello
Dsay
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the method on an instance instead of the class.
Using a wrong method name.
3fill in blank
hard

Fix 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'
Athis
Bclass
Cself
Ddef
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'self' in Ruby.
Omitting the prefix and defining an instance method instead.
4fill in blank
hard

Fill 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'
Aself
Bclass
Cdescription
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'self' for the prefix.
Using wrong method names like 'info'.
5fill in blank
hard

Fill 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'
Aself
Binfo
Ddescription
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in definition and call.
Forgetting the self prefix in the method definition.