Answers for "@ in ruby classes"

3

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

#now we can create an instance of the class

mike = SampleHumanClass.new("Mike", 32, "Male")
Posted by: Guest on November-12-2019
0

ruby class variable

class Customer
   @@no_of_customers = 0
end
Posted by: Guest on May-09-2021

Browse Popular Code Answers by Language