eigenclass

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

北慕城南 提交于 2019-11-29 14:04:53
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 Automobile" end class << self def banner "I am a singleton method of Automobile" end end end class

How does Inheritance work in Ruby?

不羁岁月 提交于 2019-11-29 13:02:35
According to Dave Thomas in his talk about the Ruby Object Model , there are no "class methods" in Ruby. There is only difference between whether the receiver of the method is a "class object" or an "instance object". class Dave def InstaceMethod ### will be stored in the current class (Dave) puts "Hi" end class << self ### Creates an eigenclass, if not created before def say_hello puts "Hello" end end end By default, ancestors method doesn't show the metaclass: class Dave class << self def metaclass ### A way to show the hidden eigenclass class << self; self; end end end end p Dave.ancestors

Difference between 'self.method_name' and 'class << self' in Ruby

怎甘沉沦 提交于 2019-11-28 22:06:25
I was trying to limit the instantiation of a class to just a single one(without using singleton) but i couldn't. I tried with class variables (@@) but without luck. I googled it and came across this: class A @count = 0 class << self attr_accessor :count end def initialize val @a = val self.class.count += 1 end end a=A.new 42 b=A.new 43 I searched for the 'class << self ' explanation hoping to find a better(or just a more simple and clean) but againt, no luck. Finally, after some tests i concluded that 'class << self ' is just a block wrapper where you can define class methods. So, is this the

How does Inheritance work in Ruby?

本小妞迷上赌 提交于 2019-11-28 07:03:42
问题 According to Dave Thomas in his talk about the Ruby Object Model, there are no "class methods" in Ruby. There is only difference between whether the receiver of the method is a "class object" or an "instance object". class Dave def InstaceMethod ### will be stored in the current class (Dave) puts "Hi" end class << self ### Creates an eigenclass, if not created before def say_hello puts "Hello" end end end By default, ancestors method doesn't show the metaclass: class Dave class << self def

Difference between 'self.method_name' and 'class << self' in Ruby

笑着哭i 提交于 2019-11-27 14:12:44
问题 I was trying to limit the instantiation of a class to just a single one(without using singleton) but i couldn't. I tried with class variables (@@) but without luck. I googled it and came across this: class A @count = 0 class << self attr_accessor :count end def initialize val @a = val self.class.count += 1 end end a=A.new 42 b=A.new 43 I searched for the 'class << self ' explanation hoping to find a better(or just a more simple and clean) but againt, no luck. Finally, after some tests i

Why isn't the eigenclass equivalent to self.class, when it looks so similar?

醉酒当歌 提交于 2019-11-26 21:15:39
I've missed the memo somewhere, and I hope you'll explain this to me. Why is the eigenclass of an object different from self.class ? class Foo def initialize(symbol) eigenclass = class << self self end eigenclass.class_eval do attr_accessor symbol end end end My train of logic that equates the eigenclass with class.self is rather simple: class << self is a way of declaring class methods, rather than instance methods. It's a shortcut to def Foo.bar . So within the reference to the class object, returning self should be identical to self.class . This is because class << self would set self to

Why isn&#39;t the eigenclass equivalent to self.class, when it looks so similar?

吃可爱长大的小学妹 提交于 2019-11-26 07:54:38
问题 I\'ve missed the memo somewhere, and I hope you\'ll explain this to me. Why is the eigenclass of an object different from self.class ? class Foo def initialize(symbol) eigenclass = class << self self end eigenclass.class_eval do attr_accessor symbol end end end My train of logic that equates the eigenclass with class.self is rather simple: class << self is a way of declaring class methods, rather than instance methods. It\'s a shortcut to def Foo.bar . So within the reference to the class

class << self idiom in Ruby

烂漫一生 提交于 2019-11-26 00:18:44
问题 What does class << self do in Ruby? 回答1: First, the class << foo syntax opens up foo 's singleton class (eigenclass). This allows you to specialise the behaviour of methods called on that specific object. a = 'foo' class << a def inspect '"bar"' end end a.inspect # => "bar" a = 'foo' # new object, new singleton class a.inspect # => "foo" Now, to answer the question: class << self opens up self 's singleton class, so that methods can be redefined for the current self object (which inside a