Tag: 元编程

Ruby.Metaprogramming。 class_eval

我的代码中似乎有一个错误。 但是我无法找到它。 class Class def attr_accessor_with_history(attr_name) attr_name = attr_name.to_s attr_reader attr_name attr_writer attr_name attr_reader attr_name + “_history” class_eval %Q{ @#{attr_name}_history=[1,2,3] } end end class Foo attr_accessor_with_history :bar end f = Foo.new f.bar = 1 f.bar = 2 puts f.bar_history.to_s 我希望它返回一个数组[1,2,3] 。 但是,它不返回任何东西。

Ruby元编程在线教程

我刚开始学习Ruby并且对基础知识有了很好的了解。 我一直听说Ruby做得很好的一件很酷的事情就是元编程,但我读过的这些教程都没有涵盖这个。 搜索谷歌我似乎只能找到付费的ruby元编程屏幕演员。 那么,我在哪里可以在线找到一个好的Ruby元编程教程?

Ruby – 使用class_eval定义方法

我正在做SaaS Stanford课程,试图完成这项任务的第5部分 我很难掌握这个概念,这就是我试图做的事情: class Class def attr_accessor_with_history(attr_name) attr_name = attr_name.to_s attr_reader attr_name attr_reader attr_name + ‘_history’ class_eval %Q'{def #{attr_name}(a);#{attr_name}_history.push(a) ; end;}’ end end 我可能做错了各种各样的事情,阅读关于元编程的The Book of Ruby章节,我仍然没有得到它,有人能帮助我理解这一点吗?

定义自定义Ruby运算符

问题是:我可以在Ruby中定义自己的自定义运算符,除了“ 运算符表达式 ”中的自定义运算符吗? 例如: 1 %! 2 1 %! 2

是否有类似于inheritance的类#的钩子只在Ruby类定义之后触发?

在class Foo语句之后#inherited调用#inherited 。 我想要的东西只能在关闭类声明的end语句之后运行。 这里有一些代码来举例说明我的需求: class Class def inherited m puts “In #inherited for #{m}” end end class Foo puts “In Foo” end puts “I really wanted to have #inherited tiggered here.” ### Output: # In #inherited for Foo # In Foo # I really wanted to have #inherited tiggered here. 这样的事情存在吗? 可以创建吗? 我完全没有运气吗?

如何反转ruby的include函数

我将在代码中解释我正在寻找的内容,因为这可能是最简洁的: module Mixin def method puts “Foo” end end class Whatever include Mixin end w = Whatever.new w.method => “Foo” # some magic here w2 = Whatever.new w.method => NoMethodError 我曾尝试使用remove_const取消定义Mixin模块,但这似乎对Whatever没有任何影响。 我假设#include只是将模块的引用添加到类的方法解析链中 – 但是这种行为与此不一致。 任何人都可以告诉我幕后实际做了什么,以及如何扭转这种情况?

如何获取通过attr_reader或attr_accessor定义的属性

假设我有一个A类 class A attr_accessor :x, :y def initialize(x,y) @x, @y = x, y end end 如何在不知道它们的确切命名的情况下获取x和y属性。 例如 a = A.new(5,10) a.attributes # => [5, 10]

Ruby – 打印变量名称,然后打印其值

编写允许我在Ruby中编写此代码的函数(或DSLish)的最佳方法是什么。 我如何构造函数write_pair? username = “tyndall” write_pair username # where write_pair username outputs username: tyndall 有可能吗? 寻找最简单的方法来做到这一点。

Ruby:为什么要调用to_ary?

我正在学习Ruby中的元编程,我只是尝试通过method_missing和define_method定义缺失的方法。 我有一些意想不到的行为,我想知道是否有人可以解释这一点。 这是我的class级: class X def method_missing(m, *args, &block) puts “method #{m} not found. Defining it.” self.class.send :define_method, m do puts “hi from method #{m}” end puts “defined method #{m}” end end 现在,这段代码: x = X.new x.some_method puts x.some_method puts puts x 产生输出: method some_method not found. Defining it. defined method some_method hi from method […]

如何在Ruby中动态改变inheritance

我想动态地为Ruby中的类指定父类。 考虑以下代码: class Agent def self.hook_up(calling_class, desired_parent_class) # Do some magic here end end class Parent def bar puts “bar” end end class Child def foo puts “foo” end Agent.hook_up(self, Parent) end Child.new.bar Parent和Child类定义都没有指定父类,因此它们都从Objectinheritance。 我的第一个问题是:我需要在Agent.hook_up中做什么才能使Parent成为Child的超类(例如, Child对象可以inheritance’bar’方法)。 我的第二个问题是:我是否需要将第一个参数传递给Agent.hook_up ,或者是否有某种方式hook_up方法可以通过编程方式确定调用它的类?