Quantcast
Channel: Pragmatic Forums | Posts by Bharat Ruparel
Viewing all articles
Browse latest Browse all 25

Episode 2 - class

$
0
0

Thanks for the tip:

I tried it, and it prints:

Bharat

Next, to see how it differs when you defined a method in it and inlcude it in a class, I created the following test scenario:


module Bharat
    p self
    def method1
      p self
    end
end

class Class1
  include Bharat  
end

m1 = Class1.new.method1

This prints the following:

Bharat #<class1:0x27b0384>

Next, I tried the following code:


module Bharat
    p self
    def method1
      p self
    end
end

class Class1
  extend Bharat  
end

m1 = Class1.method1

This prints the following result:

Bharat
Class1

When I compare and contrast these results. I am thinking that a module is nothing but a class that cannot be instantiated. Also, a method in a module can be an instance method or a class method depending on whether the module is included in the class or extended. Fundamentally, it is just a grouping of similar behavior that can be shared across different classes. Is that a correct way of approaching it?

Bharat


Viewing all articles
Browse latest Browse all 25

Trending Articles