• missing xbfish.com image

Tag Archives: Ruby methods

Instance & Class Method in Ruby

Consider the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Animal
 
    def self.haha #Class method
        puts "hello"
    end
 
    def haha #instance method
        puts "bark"
    end
 
end
 
dog = Animal.new()
dog.haha
 
Animal::haha #Calling class method

Result:

1
2
bark
hello