class-eval

Whats the difference between class_eval and class << className?

我是研究僧i 提交于 2019-12-10 11:16:43
问题 I am a Ruby starter. I found both of these are quite similar (in output), but i couldn't understand the difference in the below context. For example, I have a class class Say def self.hello puts "hello" end end and can be extended like this class << Say def hi puts "hi" end end and also like this Say.class_eval do def self.bye puts "bye" end end When should I use << and when class_eval ? 回答1: class_eval doesn't really have anything to do with class << className . A.class_eval do ... end is

Whats the difference between class_eval and class << className?

北战南征 提交于 2019-12-06 07:38:43
I am a Ruby starter. I found both of these are quite similar (in output), but i couldn't understand the difference in the below context. For example, I have a class class Say def self.hello puts "hello" end end and can be extended like this class << Say def hi puts "hi" end end and also like this Say.class_eval do def self.bye puts "bye" end end When should I use << and when class_eval ? class_eval doesn't really have anything to do with class << className . A.class_eval do ... end is equivalent to class A ... end with a few differences. class_eval uses a block (or a string, but ignoring that

What's the variable scope within `class_eval` string?

风流意气都作罢 提交于 2019-12-03 05:06:29
问题 I am using class_eval to write code to be executed under the context of current class. In the following code, I want to add a counter for changes of attribute values. class Class def attr_count(attr_name) attr_name = attr_name.to_s attr_reader attr_name # create the attribute's getter class_eval %Q{ @count = 0 def #{attr_name}= (attr_name) @attr_name = attr_name @count += 1 end def #{attr_name} @attr_name end } end end class Foo attr_count :bar end f = Foo.new f.bar = 1 My understanding of

What's the variable scope within `class_eval` string?

社会主义新天地 提交于 2019-12-02 18:20:42
I am using class_eval to write code to be executed under the context of current class. In the following code, I want to add a counter for changes of attribute values. class Class def attr_count(attr_name) attr_name = attr_name.to_s attr_reader attr_name # create the attribute's getter class_eval %Q{ @count = 0 def #{attr_name}= (attr_name) @attr_name = attr_name @count += 1 end def #{attr_name} @attr_name end } end end class Foo attr_count :bar end f = Foo.new f.bar = 1 My understanding of class_eval is that it evaluates the block in the context of the runtime class - in my case, under class

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

Ruby - Using class_eval to define methods

◇◆丶佛笑我妖孽 提交于 2019-11-29 20:38:19
I'm doing the SaaS Stanford class, trying to do Part 5 of this assignment I'm having a really hard time grasping this concept, this is what I've attempted to do: 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 I'm probably doing all sorts of things wrong, read The Book Of Ruby chapter on metaprogramming and I still don't get it, can someone help me comprehend this? This was fun!!! class Class def attr_accessor_with_history

Ruby - Using class_eval to define methods

烈酒焚心 提交于 2019-11-28 16:20:47
问题 I'm doing the SaaS Stanford class, trying to do Part 5 of this assignment I'm having a really hard time grasping this concept, this is what I've attempted to do: 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 I'm probably doing all sorts of things wrong, read The Book Of Ruby chapter on metaprogramming and I still don't get