Modules in rails lib directory

Go To StackoverFlow.com

0

module MyModule
    def self.my_method
        @instance_variable ||= "some string"
    end
end

Consider another module OtherModule that includes MyModule. When code in OtherModule calls my_method multiple times will @instance_variable be instantiated multiple times? Or just once?

2012-04-04 21:31
by SundayMonday


1

updated answer:

  1. if the delegate module call the method, you will get an error.
  2. if the original module call the method, the class instance variable only instanced 1 time.
  3. if you want to define "class methods" in an module, you can't define self.xx then simply extend/include it. you should either "extend" it, or "include it to the eigen class", e.g.

    module SomeModule
      def self.aim_to_be_class_method ;    end;  
      def aim_to_be_instance_method ;    end;
    end
    
    # class methods:  []
    # instance methods: ["aim_to_be_instance_method"]
    class SomeClassIncludingTheModule
      include SomeModule  
    end
    
    # class methods:  ["aim_to_be_instance_method"]
    # instance methods: []
    class SomeClassExtendingTheModule
      extend SomeModule   
    end
    
    # class methods:  ["aim_to_be_instance_method"]
    # instance methods: []
    class SomeClassMadeEigenClassIncludingModule
      class << self
        include SomeModule
      end
    end
    

your code is an example of "class instance variables". according to the book << metaprogramming ruby>>, page 127, you can consider "class instance variables" as Java's static fields. so, I think most of the case, it should run once.

for more details, please write an unit test and see what happens? I will update my answer after a short while with my own unit test code written.

UPDATED: my unit test and result:

# all of the code is placed in a file: class_instance_variable_test.rb

# define a class so that we see its initialize process
class Apple
  def initialize
    puts "Apple initialized~"
  end 
end

# define an original_module that calls Apple.new
module OriginalModule
  def self.original_method
    @var ||= Apple.new
  end 
end

# define another module to call the original_module
module DelegateModule
  include OriginalModule
end

# now the test begins
require 'test/unit'
class ClassInstanceTest < Test::Unit::TestCase

  # output of this test case: 
  # NoMethodError: undefined method `original_method' for DelegateModule:Module      
  def test_if_delegate_module_could_call_original_method_by_including_original_module
    DelegateModule.original_method
  end

  # output of this test case: 
  # ----- calling from original_method, 3 times called, how many times initialized?
  # Apple initialized~
  # ------ ends
  def test_if_we_could_call_original_method_from_original_module
    puts "----- calling from original_method, 3 times called, how many times initialized? "
    OriginalModule.original_method
    OriginalModule.original_method
    OriginalModule.original_method
    puts "------ ends "
  end

end
2012-04-04 22:56
by XXX
Ads