生命周期回调在ruby中的实例和类方法

时间:2015-12-19 20:13:59

标签: ruby metaprogramming

number_of_docs=7
db.collection('preguntas').find({},{_id:1}).toArray(function(err, arr) {
count=arr.length
idsram=[]
rans=[]
while(number_of_docs!=0){
    var R = Math.floor(Math.random() * count);
    if (rans.indexOf(R) > -1) {
     continue
      } else {           
               ans.push(R)
               idsram.push(arr[R]._id)
               number_of_docs--
                }
    }
db.collection('preguntas').find({}).toArray(function(err1, doc1) {
                if (err1) { console.log(err1); return;  }
               res.send(doc1)
            });
        });

这是RubyMonk的Ruby的Object Lifecycle Callbacks部分中的一个示例。我不明白它应该如何工作或者它是什么意思。 module Gym def self.included(class_or_module) class_or_module.send(:include, InstanceMethods) class_or_module.extend(ClassMethods) end module ClassMethods def build end end module InstanceMethods def open end def book_for_practice end def close end end end 应该只记录self.included中的两个模块如何使用,对吧?为什么Gym会被发送/延长?为什么它不会被保存在某些记录生命周期的数组中,就像导致这个数据的例子一样,例如

class_or_module

2 个答案:

答案 0 :(得分:4)

它不仅仅是文档。 self.included是一个回调方法,只要模块被包含在任何其他模块或类中,就会调用它。

在该示例中,send通过extend,类或模块方法包含实例方法。

Ruby documentation中了解更多信息。

答案 1 :(得分:1)

让我逐一回答你的问题。

1:self.included should just document how the two modules within Gym get used ?

module A
  def instance_methods_1
    p 'hello instance_methods_1'
  end
  def instance_methods_2
    p 'hello instance_methods_2'
  end

  module KlassMethods
    def klass_methods_1
      p 'Hello!! klass_methods_1'
    end
    def klass_methods_2
      p 'Hello!! klass_methods_2'
    end
  end
end

class B
  include A  # instead of writing two piece of code like this we could wrap in one using `self.included` method Hook
  extend A::KlassMethods
end

B.new.instance_methods_1
B.new.instance_methods_2
B.klass_methods_1
B.klass_methods_2

使用self.included

的方法钩子的同一程序的另一个版本
module A
  # this is special method one of the methods hook in ruby.
  def self.included(base)
    base.extend(KlassMethods)
  end
  def instance_methods_1
    p 'hello instance_methods_1'
  end
  def instance_methods_2
    p 'hello instance_methods_2'
  end

  module KlassMethods
    def klass_methods_1
      p 'Hello!! klass_methods_1'
    end
    def klass_methods_2
      p 'Hello!! klass_methods_2'
    end
  end
end

class B
  include A
end

B.new.instance_methods_1
B.new.instance_methods_2
B.klass_methods_1
B.klass_methods_2

2: self.included

  1. 了解有关included hook
  2. 的更多信息
  3. Ruby Hook
  4. <强> 3: why does class_or_module then get sent/extended ?

    module AddAdditionalProperty
      def self.included(base)
        base.extend(ClassMethods)
      end
    
      module ClassMethods
        def add_additional_property
          p 'ClassMethods::add_additional_property'
        end
      end
    
    end
    
    module ActiveRecord
      class Base
        def test
          p 'Base test Method'
        end
      end
    end
    
    ActiveRecord::Base.send(:include, AddAdditionalProperty)
    ActiveRecord::Base.add_additional_property
    
    ## Another version of same Program
    
    module AddAdditionalProperty
      def self.included(base)
        base.extend(ClassMethods)
      end
    
      module ClassMethods
        def add_additional_property
          p 'ClassMethods::add_additional_property'
        end
      end
    
    end
    
    module ActiveRecord
      class Base
        include AddAdditionalProperty
        def test
          p 'Base test Method'
        end
      end
    end
    
    ActiveRecord::Base.add_additional_property
    

    希望这个答案可以帮助你!!!