instance-eval

Ruby difference between send and instance_eval?

血红的双手。 提交于 2019-12-06 00:00:40
问题 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' 回答1: From the fine manual: send(symbol [, args...]) → obj send(string [, args...]) → obj Invokes the method identified by symbol , passing it any arguments specified. [...] When the method is identified by a

Ruby difference between send and instance_eval?

心不动则不痛 提交于 2019-12-04 04:50:18
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' From the fine manual : send(symbol [, args...]) → obj send(string [, args...]) → obj Invokes the method identified by symbol , passing it any arguments specified. [...] When the method is identified by a string, the string is converted to a symbol. and for instance_eval : instance_eval(string [, filename [,

Accessing Ruby Class Variables with class_eval and instance_eval

血红的双手。 提交于 2019-12-03 03:14:14
问题 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

Accessing Ruby Class Variables with class_eval and instance_eval

北慕城南 提交于 2019-12-02 16:43:35
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. I

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

荒凉一梦 提交于 2019-12-01 09:13:37
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

Instance_eval does not work with do/end block, only with {}-blocks [duplicate]

隐身守侯 提交于 2019-12-01 07:12:10
问题 This question already has answers here : Ruby block and unparenthesized arguments (1 answer) Ruby Block Syntax Error [duplicate] (1 answer) Closed 5 years ago . If I have a class: class KlassWithSecret def initialize @secret = 99 end end and run: puts KlassWithSecret.new.instance_eval { @secret } it prints 99, but if I run: puts KlassWithSecret.new.instance_eval do @secret end It returns an error: `instance_eval': wrong number of arguments (0 for 1..3) (ArgumentError) Why can't I use do/end

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

空扰寡人 提交于 2019-12-01 07:07:58
问题 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

Ruby instance_eval on a class with attr_accessor

这一生的挚爱 提交于 2019-12-01 04:29:11
问题 I understand the basic difference between instance_eval and class_eval . What I've discovered though when playing around is something strange involving attr_accessor . Here's an example: A = Class.new A.class_eval{ attr_accessor :x } a = A.new a.x = "x" a.x => "x" # ... expected A.instance_eval{ attr_accessor :y } A.y = "y" => NoMethodError: undefined method `y=' for A:Class a.y = "y" => "y" # WHATTT? How is it that: the instance_eval didn't at the accessor onto our A class (object) it then

Instance_eval block not supplied? [duplicate]

孤街醉人 提交于 2019-11-30 09:56:58
问题 This question already has answers here : Code block passed to each works with brackets but not with 'do'-'end' (ruby) (3 answers) Closed 3 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

Instance_eval block not supplied? [duplicate]

独自空忆成欢 提交于 2019-11-29 17:21:52
This question already has an answer here: Code block passed to each works with brackets but not with 'do'-'end' (ruby) 3 answers 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: Apparently I forgot