Tag: 元编程

如何使控制器中的实例变量可用于Rails中的视图

我已经做了一段时间的Rails并且刚刚开始深入研究Rails从中获得力量的Ruby元编程。 我真的无法想出这个,它让我发疯。 如何在控制器中的实例变量可用于Rails中的视图(与视图共享)? 我知道它背后有一些元编程魔术,但我无法理解。请提前感谢你的所有帮助。

在ruby中从自身获取实例变量名称

我有一个实例变量@foo ,我想写一些代码,以便我得到字符串’foo’ 任何提示?

回调模块内定义的类

Ruby已经有几个内置的回调 。 这种情况有回调吗? 有点像method_added,但对于模块内的类(或常量),而不是类中的实例方法。

以编程方式使用$&Global Variable的Alias方法

我正在尝试使用Ruby的特殊$& ( 返回上一个正则表达式匹配 )的方法。 我可以手动完成这个工作: original = String.instance_method(:sub) String.send(:define_method, :sub) do |*args, &block| puts “called” original.bind(self).call(*args, &block) end “foo”.sub(/f/) { $&.upcase } called # => “Foo” 但是,如果我尝试编写一个为我执行此操作的方法,它将失败: def programatic_alias(klass, method_name) original = klass.instance_method(method_name) klass.send(:define_method, method_name) do |*args, &block| puts “called” original.bind(self).call(*args, &block) end end programatic_alias(String, :sub) “foo”.sub(/f/) { $&.upcase } called NoMethodError: undefined method `upcase’ […]

如何在像instance_eval方法的块中更改self?

instance_eval方法在其块中更改self,例如: class D; end d = D.new d.instance_eval do puts self # print something like #, not ‘main’! end 如果我们定义一个方法selfelf(或任何其他方法(除了instance_eval)接受一个块),当print self时,我们将获得’main’,这与instance_eval方法不同.eg: [1].each do |e| puts self # print ‘main’ end 如何定义像instance_eval这样的方法(采用块)? 提前致谢。

收集不必要分配给任何变量/常量的对象

未分配给任何变量/常量的对象立即消失(在正常情况下)。 在下面,第三行中的ObjectSpace.each_object(String)不捕获字符串”foo” : strings = ObjectSpace.each_object(String).to_a “foo” ObjectSpace.each_object(String).to_a – strings # => [] 是否有可能捕获不必分配给任何变量/常量或任何变量/常量的一部分的对象? 我对捕获字符串特别感兴趣。 相关域可以是文件或块。 我希望如下: capture_all_strings do … “a” s = “b” @s = “c” @@s = “d” S = “e” %q{f} … end # => [“a”, “b”, “c”, “d”, “e”, “f”]

如何在Ruby中列出局部变量?

def method a = 3 b = 4 some_method_that_gives # [a, b] end

查找名称与字符串匹配的所有类

我想找到名称与字符串匹配的所有ruby类(忽略大小写)。 这意味着该字符串应该是类名的子字符串。 所以当你搜索’stri’时,你应该得到类String (以及其他)作为结果。 你知道一个方便而不是太低效的方法吗?

元编程:输出方法体作为文本

我在模块中动态定义一个方法,并且我想检查一旦该方法绑定到一个类实例,该方法的主体就是我所期待的。 有没有办法输出(作为文本)方法的主体? 模块controller_mixins.rb : module ControllerMixin instance_eval “def search_by_vendor (*args) \n” \ ” @#{self.class.name.sub(/Controller/, ”).tableize} = #{self.class.name.sub(/Controller/, ”)}.find_all_by_vendor_id(params[:vendor_id]) \n”\ “respond_to do |format| \n” \ ” format.html { render :template=>’/#{self.class.name.sub(/Controller/, ”).tableize}/index’, :layout=>’vendor_info’} \n” \ ” format.xml { render :xml => @#{self.class.name.sub(/Controller/, ”).tableize} } \n” \ “end \n”\ “end \n” end 上课与: class VendorOrdersController < ApplicationController # […]

动态创建方法中的Ruby动态参数

我有以下几种方法定义: method_name = :foo method_arguments = [:bar, :baz] method_mandatory_arguments = {:quux => true} method_body = ->{ quux ? bar + baz : bar – baz } 所以我想得到一个真正的方法。 但是define_method没有任何动态定义方法参数的可能性。 我知道另一种使用class_eval的方法,但我知道用class_eval定义方法比使用define_method慢得多。 我怎么能有效地存档这个? 我在rails控制台中做了一些基准测试: class Foo; end n = 100_000 Benchmark.bm do |x| x.report(‘define_method’) do n.times { |i| Foo.send(:define_method, “method1#{i}”, Proc.new { |a, b, c| a + b […]