DEV Community

pooyaalamdari
pooyaalamdari

Posted on

method in module and class

module InterestBearing
    def calculate_interest
        puts "Placeholder! we are in <module> InterestBearing"
    end
end

class BankAccount
    include InterestBearing

    def calculate_interest
        puts "Placeholder! we are in <class> BankAccount"
        puts """ the method in <class> is 
                 overriding than method in 
                 <module>."""
    end
end

account = BankAccount.new 
account.calculate_interest
Enter fullscreen mode Exit fullscreen mode
module M
    def report
        puts "'report' method in module M"
    end
end

module N
    def report 
        puts "'report' method in module N"
    end
end

class C  
    include M 
    include N
end

c = C.new 
c.report
Enter fullscreen mode Exit fullscreen mode
module M
    def report
        puts "'report' method in module M"
    end
end

module N
    def report 
        puts "'report' method in module N"
    end
end

class C  
    include M 
    include N
    include M
end

c = C.new 
c.report # same result (N)
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)