eigenclass

State-dependent behaviour of Python objects using Ruby-like eigenclasses with mixins

白昼怎懂夜的黑 提交于 2020-01-14 14:06:09
问题 I've been looking for a natural way to implement state-dependent behaviour (state machines) in Python objects. The goal was for objects to have a small number of states, or "mutually orthogonal" aspects of state, which would determine their concrete behaviour at each moment. In other words, the method returned by x.foo should be determined by the current state of x , and if x changes its state, the implementation of some or all of its methods should change accordingly. (I think some call it

Class, Module, their eigenclasses, and method lookup

人走茶凉 提交于 2020-01-13 10:43:58
问题 Let's open class Module and add a method to it: class Module def foo puts "phew" end end I can call this method by doing this, Class.foo which is understandable because class of Class is Class , whose superclass is Module . so it can call instance methods defined in Module . Now, the method bar below is defined on Module 's eigenclass: class Module def self.bar puts "bar" end end but now Class.bar also works. Can someone explain me how Class can access methods in Module 's eigenclass? I think

Class, Module, their eigenclasses, and method lookup

邮差的信 提交于 2020-01-13 10:43:09
问题 Let's open class Module and add a method to it: class Module def foo puts "phew" end end I can call this method by doing this, Class.foo which is understandable because class of Class is Class , whose superclass is Module . so it can call instance methods defined in Module . Now, the method bar below is defined on Module 's eigenclass: class Module def self.bar puts "bar" end end but now Class.bar also works. Can someone explain me how Class can access methods in Module 's eigenclass? I think

Class, Module, their eigenclasses, and method lookup

末鹿安然 提交于 2020-01-13 10:43:07
问题 Let's open class Module and add a method to it: class Module def foo puts "phew" end end I can call this method by doing this, Class.foo which is understandable because class of Class is Class , whose superclass is Module . so it can call instance methods defined in Module . Now, the method bar below is defined on Module 's eigenclass: class Module def self.bar puts "bar" end end but now Class.bar also works. Can someone explain me how Class can access methods in Module 's eigenclass? I think

Ruby Class Methods vs. Methods in Eigenclasses

假如想象 提交于 2019-12-21 07:11:53
问题 Are class methods and methods in the eigenclass (or metaclass) of that class just two ways to define one thing? Otherwise, what are the differences? class X # class method def self.a "a" end # eigenclass method class << self def b "b" end end end Do X.a and X.b behave differently in any way? I recognize that I can overwrite or alias class methods by opening the eigenclass: irb(main):031:0> class X; def self.a; "a"; end; end => nil irb(main):032:0> class X; class << self; alias_method :b, :a;

What's the difference between a class and the singleton of that class in Ruby?

只愿长相守 提交于 2019-12-20 10:14:27
问题 Okay, so I'm trying to do some metaprogramming in Ruby and I'm getting a bit confused. According to several articles I've read (like this one), in order to dynamically add class methods to Ruby classes, you have to use the class's singleton class: class Klass end class << Klass self.define_method(:foo) { return "foo" } end Why is this, and how is that different from this? class Klass self.define_method(:foo) { return "foo" } end (Sorry if this question contains any false assumptions. Like I

Ruby Class Methods vs. Methods in Eigenclasses

自古美人都是妖i 提交于 2019-12-04 01:23:37
Are class methods and methods in the eigenclass (or metaclass) of that class just two ways to define one thing? Otherwise, what are the differences? class X # class method def self.a "a" end # eigenclass method class << self def b "b" end end end Do X.a and X.b behave differently in any way? I recognize that I can overwrite or alias class methods by opening the eigenclass: irb(main):031:0> class X; def self.a; "a"; end; end => nil irb(main):032:0> class X; class << self; alias_method :b, :a; end; end => #<Class:X> irb(main):033:0> X.a => "a" irb(main):034:0> X.b => "a" irb(main):035:0> class X

What's the difference between a class and the singleton of that class in Ruby?

梦想与她 提交于 2019-12-02 21:30:37
Okay, so I'm trying to do some metaprogramming in Ruby and I'm getting a bit confused. According to several articles I've read (like this one ), in order to dynamically add class methods to Ruby classes, you have to use the class's singleton class: class Klass end class << Klass self.define_method(:foo) { return "foo" } end Why is this, and how is that different from this? class Klass self.define_method(:foo) { return "foo" } end (Sorry if this question contains any false assumptions. Like I said, I'm a bit confused.) To answer your question directly: Module#define_method creates an instance

Why a module's singleton method is not visible in downstream eigenclasses where it gets mixed?

橙三吉。 提交于 2019-12-01 15:32:45
问题 I understand the regular method lookup path i.e. class, superclass/module, all the way up to BasicObject . I thought it was true for singleton version of the chain also but doesn't seem the case when you mixin a module in the meta-chain. I'd appreciate if someone can explain why in the following example Automobile module's banner method is called instead of its singleton version when I have included this module in Vehicle's eigenclass. module Automobile def banner "I am a regular method of

Ruby looks for class variable in the Object instead of specific class

*爱你&永不变心* 提交于 2019-11-30 19:10:21
问题 This part works: class Example1 @@var1= "var1 in the Example1" def get_var1 @@var1 end end example1 = Example1.new example1.get_var1 # => "var1 in the Example1" but if I try eigenclass: def example1.get_var1 @@var1 end example1.get_var1 # NameError: uninitialized class variable @@var1 in Object # from (pry):128:in `get_var1' Ruby looks @@var1 in the Object instead of the Example . I have tested this code in the Ruby 1.9.3 and 2.0 with the same result. Why does it happening? The second thing,