Objects and Classes
Class Methods
Class methods belong to the class itself and are called without creating an object first.
Class Methods
class_methods.rb
class Grade
def self.passing?(score)
score >= 70
end
def self.label(score)
passing?(score) ? "pass" : "review"
end
end
score =
puts "score=#{score}"
puts "status=#{Grade.label(score)}"
class Grade
def self.passing?(score)
score >= 70
end
def self.label(score)
passing?(score) ? "pass" : "review"
end
end
score =
puts "score=#{score}"
puts "status=#{Grade.label(score)}"
class Grade
def self.passing?(score)
score >= 70
end
def self.label(score)
passing?(score) ? "pass" : "review"
end
end
score =
puts "score=#{score}"
puts "status=#{Grade.label(score)}"
class method
A method defined as `self.method_name` is called on the class rather than on an instance.