class methods ruby code example
Example 1: creating a class in ruby
class SampleHumanClass
attr_accessor :name, :age, :gender
@@all = []
def initialize(name, age, gender)
@name = name
@age = age
@gender = gender
@@all << self
end
def self.all
@@all
end
end
mike = SampleHumanClass.new("Mike", 32, "Male")
Example 2: class ruby
Classes in Ruby are first-class objects—each is an instance of class Class . When a new class is created, an object of type Class is initialized and assigned to a global constant ( Name in this case). Classes, modules, and objects are interrelated.