天道酬勤,学无止境

instance-eval

send和instance_eval之间的Ruby区别?(Ruby difference between send and instance_eval?)

问题 我知道 send 使用带参数的字符串或符号,而 instance_eval 使用字符串或块,它们的区别在给定接收器的情况下可能很明显。 我的问题是下面示例的“幕后”区别是什么? 1234.send 'to_s' # '1234' 1234.instance_eval 'to_s' # '1234' 回答1 从精美的手册: 发送(符号 [,参数...])→ obj 发送(字符串 [,参数...])→ obj 调用由符号标识的方法,将指定的任何参数传递给它。 [...] 当方法由字符串标识时,字符串将转换为符号。 和 instance_eval: instance_eval(string [, 文件名 [, lineno]] ) → obj 实例评估{| | 块 } → 对象在接收器 ( obj ) 的上下文中计算包含 Ruby 源代码或给定块的字符串。 为了设置上下文,在代码执行时将变量self设置为obj ,使代码可以访问obj的实例变量。 因此, send执行一个方法,而instance_eval执行任意代码块(作为字符串或块),并将self设置为您正在调用instance_eval的对象。 在您的情况下,没有太大区别,因为您传递给instance_eval的字符串只是一个方法。 主要区别在于任何阅读您代码的人(包括六个月后的您)都会想知道您为什么使用instance

2021-07-13 03:44:14    分类:技术分享    ruby   send   instance-eval

使用 class_eval 和 instance_eval 访问 Ruby 类变量(Accessing Ruby Class Variables with class_eval and instance_eval)

问题 我有以下几点: class Test @@a = 10 def show_a() puts "a: #{@@a}" end class << self @@b = '40' def show_b puts "b: #{@@b}" end end end 为什么以下工作: Test.instance_eval{show_b} b: 40 => nil 但是我不能直接访问@@b ? Test.instance_eval{ @@b } NameError: uninitialized class variable @@b in Object 同样,以下工作 t = Test.new t.instance_eval{show_a} a: 10 => nil 但以下失败 t.instance_eval{ @@a } NameError: uninitialized class variable @@a in Object 我不明白为什么我不能直接从instance_eval块访问类变量。 回答1 我刚刚在 RubyKaigi 派对上向 Matz 提出了同样的问题。 我喝了半醉,但他完全清醒,所以你可以把这个当作确定的答案。 Anton 是对的——你不能通过 instance_eval() 访问类变量的原因是“只是因为”。 甚至 class_eval() 也有同样的问题(Matz

2021-06-23 07:47:02    分类:技术分享    ruby   class   variables   instance-eval

Ruby difference between send and instance_eval?

I know send takes string or symbol with arguments while instance_eval takes string or block, and their difference could be apparent given receivers. My question is what the 'under the hood' difference is for the example below? 1234.send 'to_s' # '1234' 1234.instance_eval 'to_s' # '1234'

2021-06-05 07:22:33    分类:问答    ruby   send   instance-eval

未提供 Instance_eval 块? [复制](Instance_eval block not supplied? [duplicate])

问题 这个问题在这里已经有了答案: 传递给每个函数的代码块使用方括号但不使用“do”-“end”(ruby) (3 个回答) 4年前关闭。 有谁知道是什么导致了这个错误? 我正在尝试制作一个基本的机架应用程序。 App.rb => class Cherry class << self def app &block Cherry::Application.new &block end end class Application def initialize &block instance_eval &block end def print_start_message puts "Starting server" end def call env [200, {"Content-type" => "text/plain"}, "Hello World"] end end end Config.ru => require 'app' run Cherry.app do print_start_message end 编辑:显然我忘了包括错误woops: /local/www/cherry/lib/app.rb:12:in 'instance_eval': block not supplied (ArgumentError) 回答1 解决它! 显然你需要在Cherry.app do

2021-06-03 22:43:30    分类:技术分享    ruby   rack   instance-eval

Ruby:常量查找如何在instance_eval / class_eval中工作?(Ruby: how does constant-lookup work in instance_eval/class_eval?)

问题 我正在通过Pickaxe 1.9进行工作,并且对instance / class_eval块中的常量查找感到有些困惑。 我正在使用1.9.2。 看起来Ruby在* _eval块中处理常量查找的方式与方法查找相同: 在receiver.singleton_class(加上mixins)中查找定义; 然后在receiver.singleton_class.superclass(加上mixins)中; 然后继续沿着特征链向上移动,直到到达#<Class:BasicObject> ; 其超类是Class; 然后在祖先链的其余部分(包括Object ,该Object存储您在顶级定义的所有常量),并在此过程中检查是否有mixins 这样对吗? 镐的讨论有点简短。 一些例子: class Foo CONST = 'Foo::CONST' class << self CONST = 'EigenFoo::CONST' end end Foo.instance_eval { CONST } # => 'EigenFoo::CONST' Foo.class_eval { CONST } # => 'EigenFoo::CONST', not 'Foo::CONST'! Foo.new.instance_eval { CONST } # => 'Foo::CONST' 在class_eval示例中

2021-06-01 13:41:01    分类:技术分享    ruby   constants   instance-eval

Accessing Ruby Class Variables with class_eval and instance_eval

I have the following: class Test @@a = 10 def show_a() puts "a: #{@@a}" end class << self @@b = '40' def show_b puts "b: #{@@b}" end end end Why does following work: Test.instance_eval{show_b} b: 40 => nil But I can't access @@b directly? Test.instance_eval{ @@b } NameError: uninitialized class variable @@b in Object Likewise, the following works t = Test.new t.instance_eval{show_a} a: 10 => nil but the following fails t.instance_eval{ @@a } NameError: uninitialized class variable @@a in Object I don't understand why I can't access the Class Variables directly from the instance_eval blocks.

2021-05-15 23:44:24    分类:问答    ruby   class   variables   instance-eval

Ruby: how does constant-lookup work in instance_eval/class_eval?

I'm working my way through Pickaxe 1.9, and I'm a bit confused by constant-lookup in instance/class_eval blocks. I'm using 1.9.2. It seems that Ruby handles constant-lookup in *_eval blocks the same way it does method-lookup: look for a definition in receiver.singleton_class (plus mixins); then in receiver.singleton_class.superclass (plus mixins); then continue up the eigenchain until you get to #<Class:BasicObject>; whose superclass is Class; and then up the rest of the ancestor chain (including Object, which stores all the constants you define at the top-level), checking for mixins along the

2021-05-02 23:38:05    分类:问答    ruby   constants   instance-eval

Instance_eval block not supplied? [duplicate]

This question already has answers here: Code block passed to each works with brackets but not with 'do'-'end' (ruby) (3 answers) Closed 4 years ago. Does anybody know what's causing this error? I'm trying to make a basic rack application. App.rb => class Cherry class << self def app &block Cherry::Application.new &block end end class Application def initialize &block instance_eval &block end def print_start_message puts "Starting server" end def call env [200, {"Content-type" => "text/plain"}, "Hello World"] end end end Config.ru => require 'app' run Cherry.app do print_start_message end EDIT

2021-04-21 16:05:54    分类:问答    ruby   rack   instance-eval