English 中文(简体)
你们如何用像宏观的计量方法推广Ruby模块?
原标题:How do you extend a Ruby module with macro-like metaprogramming methods?

考虑以下延伸(多年来若干铁路码头的分布):

module Extension
  def self.included(recipient)
    recipient.extend ClassMethods
    recipient.send :include, InstanceMethods
  end

  module ClassMethods
    def macro_method
      puts "Called macro_method within #{self.name}"
    end
  end

  module InstanceMethods
    def instance_method
      puts "Called instance_method within #{self.object_id}"
    end
  end
end

如果您愿意向每个阶层介绍,你可以做以下工作:

Object.send :include, Extension

现在,你可以界定任何类别,使用宏观方法:

class FooClass
  macro_method
end
#=> Called macro_method within FooClass

案例可以采用:

FooClass.new.instance_method
#=> Called instance_method within 2148182320

但是,即使<代码>Module.is_a?(目标),你也不能在模块中使用宏观方法。

module FooModule
  macro_method
end
#=> undefined local variable or method `macro_method  for FooModule:Module (NameError)

即便你将原来的<代码>Extension明确列入Module,与Module.send(包括,推广)

对于个别单元,你可以按手方式包括推广和发挥同样的作用:

module FooModule
  include Extension
  macro_method
end
#=> Called macro_method within FooModule

但是,你如何在所有鲁比模块中添加类似宏观的方法?

最佳回答

考虑以下的延伸(多年来若干铁路原始产品所推广的模式)

这是一种模式,不是“人口”。 是anti-pattern,cargo-culted,1337 PHP h4X0r 不知道Ruby。 令人欣慰的是,由于Yehuda Katz、Carl Lerche和其他人的话,许多(所有吗?)的这种反家长事件已经从3号铁路中消失。 Yehuda甚至使用你在最近关于清理铁路密码库的会谈中贴出的完全相同的代码,并写了,仅涉及一名反主。

如果您愿意向每个阶层介绍,你可以做以下工作:

Object.send :include, Extension

请将此增列为<代码>。 目的anyway, 所以不光是:

class Object
  def instance_method
    puts "Called instance_method within #{inspect}"
  end
end

但是,你如何在所有鲁比模块中添加类似宏观的方法?

简单:在<条码>上添加:

class Module
  def macro_method
    puts "Called macro_method within #{inspect}"
  end
end

所有公正的工作都是:

class FooClass
  macro_method
end
#=> Called macro_method within FooClass

FooClass.new.instance_method
#=> Called instance_method within #<FooClass:0x192abe0>

module FooModule
  macro_method
end
#=> Called macro_method within FooModule

这部法律只有10条,即第16条,这10条线路中,只有0条形形形形形形形形色色色或黑色,甚至非常复杂。

你的法典与矿体之间的唯一区别是,在你的法典中,在继承等级上显示的组合,因此,可以比较容易去辩论,因为你实际上在<条码>上增加了一些内容。 目标。 但这很容易确定:

module ObjectExtensions
  def instance_method
    puts "Called instance_method within #{inspect}"
  end
end

class Object
  include ObjectExtensions
end

module ModuleExtensions
  def macro_method
    puts "Called macro_method within #{inspect}"
  end
end

class Module
  include ModuleExtensions
end

现在我与你的法典有16条关系,但我想说,地雷比你更简单,特别是考虑到你不工作,你和我以及将近190000 StackOverflow用户都无法说明原因。

问题回答

暂无回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签