简体   繁体   English

将块传递到Ruby的class_eval中的嵌套方法中?

[英]Passing blocks into nested method within class_eval in Ruby?

I want to be able to define a block, and later evaluate that block from within a dynamically generated module/class. 我希望能够定义一个块,然后在动态生成的模块/类中评估该块。 It seems like I could accomplish this somehow using eval and block.binding , but I haven't figured it out. 似乎我可以使用evalblock.binding来完成此block.binding ,但是我还没有弄清楚。

I have this as the base: 我以此为基础:

def define_module(name, &block)
  name = name.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  parts = name.split("::")
  parts.each_with_index do |part, index|
    sub_name = parts[0..index].join("::")
    eval("module #{sub_name}; end")
  end
  clazz = eval(name)
  clazz.class_eval(&block) if block_given?
  clazz
end

def add_module(name, &block)
  module_block = block
  define_module(name).class_eval <<-EOF
    def self.included(base)
      base.class_eval do
        # something like this, I'm stuck
        instance_eval(&#{module_block})
      end
    end
  EOF
end

And I want to use it like this: 我想这样使用它:

add_module("My::Library") do
  def a_method
    "added 'a_method'"
  end
end

class ::User
  include My::Library
end

user = ::User.new

assert_equal "added 'a_method'", user.a_method

Is there any way to do something like that? 有什么办法可以做这样的事情吗?

This works: 这有效:

def add_module(name, &block)
    define_module(name).class_eval do
        class << self; self; end.send(:define_method, :included) { |base|
            base.class_eval(&block)
        }
    end
end

add_module("My::Library") do
    def a_method
        "added 'a_method'"
    end
end

class ::User
    include My::Library
end

user = ::User.new
user.a_method #=> "added a_method"

EDIT: 编辑:

Why don't you just do this instead? 您为什么不这样做呢? Much simpler, and it's actually the job of a module: 简单得多,实际上是模块的工作

def add_module(name, &block)
    define_module(name).class_eval(&block)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM