Complete the code to define a class method in Ruby.
class Car def self.[1] "This is a class method" end end
self. prefix.The method name info is used here as a class method example. It can be called on the class itself.
Complete the code to call the class method correctly.
class Dog def self.[1] "Bark!" end end puts Dog.[1]
The class method bark is defined and called on the class Dog.
Fix the error in the code by completing the blank.
class Cat def [1] "Meow" end end puts Cat.meow
To define a class method, prefix the method name with self.. Here, def self.meow defines a class method.
Fill in the blank to create a class method that accesses the class variable.
class Book @@count = 0 def self.[1] @@count end end
Class variables start with @@. The class method count accesses and returns it.
Fill both blanks to define a class method that increments a class variable and returns it.
class Counter @@total = 0 def self.[1] @@total [2] 1 end end
self. for class methods.=+ instead of +=.Class variables start with @@. Define the class method increment and use += to increment and return the new value.