使用open类将实例方法添加到ruby中的Object类?

时间:2017-10-22 19:43:58

标签: ruby metaprogramming block

我有一个类coords,它执行其他对象的某些实例的代码块:

Operator

另一方面,我有类Criature

class Operator

    def operation(&block)
        #execution of a block in some object
    end
end

我需要用开放类来对类class Criature operational def level_up @level+=1 end end 进行monkeypatch。

所以,我需要在Object中定义方法operational,因为每次Object的实例执行Operator时,(例如)块:operation将在其类主体中使用{ c.level_up() }的每个实例执行。

我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

  

执行某些对象的某些实例的代码块:

如果我完全理解,使用方法

    operational

你想在Criature等某些类的主体中执行,你想标记他们的实例,说"嘿,在执行operation时给我打个电话。&# 34;

来自The Pickaxe

  

ObjectSpace.each_object(

     

在此Ruby进程中为每个活动的非临时对象调用块一次。如果指定了class_or_mod,则只调用那些匹配(或者是class_or_mod的子类)的类或模块的块。

使用ObjectSpace,可以迭代某个类的对象。这让我觉得你想要给某些实例的签名,这个标志可以用超类完成。

ObjectSpace.each_object(Operational)

将收集具有此"签名"。

的所有类实例

档案tc.rb

class Operational

end

class Criature < Operational
    def initialize(p_name)
        @name  = p_name
        @level = 0
    end

    def level_up
        @level+=1
        puts "level is now #{@level}"
    end
end

class XYZ < Operational
    def initialize(p_name)
        @name = p_name
    end

end

档案to.rb

require_relative 'tc'

class Operator

    def operation(&block)
        #execution of a block in some object
        Special.currently_operational.each do | obj |
            puts "operation on #{obj.inspect}"
            block.call(obj) if obj.respond_to? 'level_up'
        end
    end
end

class Special
    def self.currently_operational
        operationals = []

        ObjectSpace.each_object(Operational) do | object |
            operationals << object
        end

        puts "#{operationals.size} objects are flagged as operational"
        operationals
    end
end

Criature.new('c1')
Criature.new('c2')
Criature.new('c3')

block = Proc.new{ | x | x.level_up }
puts 'first round'
Operator.new.operation(&block)

# later ...
XYZ.new('x1')
XYZ.new('x2')

puts 'second round'
Operator.new.operation(&block)

执行:

$ ruby -w to.rb 
first round
3 objects are flagged as operational
operation on #<Criature:0x007fc45683d2f0 @name="c3", @level=0>
level is now 1
operation on #<Criature:0x007fc45683d368 @name="c2", @level=0>
level is now 1
operation on #<Criature:0x007fc45683d3b8 @name="c1", @level=0>
level is now 1
second round
5 objects are flagged as operational
operation on #<XYZ:0x007fc45683cad0 @name="x2">
operation on #<XYZ:0x007fc45683cb48 @name="x1">
operation on #<Criature:0x007fc45683d2f0 @name="c3", @level=1>
level is now 2
operation on #<Criature:0x007fc45683d368 @name="c2", @level=1>
level is now 2
operation on #<Criature:0x007fc45683d3b8 @name="c1", @level=1>
level is now 2