Wednesday, April 1, 2015

Ruby always holds the reference to a class as well as the self. But there is no way to retrieve it

[Translation] Three Contexts Implicit in Ruby | AkitaOnRails.com
Yehuda Katz wrote an article about self and metaclass. In this article he said Person.instance_eval associates the metaclass of Person to self for a Person class, but this is obviously wrong. Check out all related links
As I mentioned in an old article, although I must apologize for being written in Japanese, Ruby always has 3 implicit contexts: self, the so-called 'klass' and the constant set point. Yehuda how to lose belly fat fast is confusing self with 'klass'. self
self is the self you know. It is the standard receiver method how to lose belly fat fast invocation. There is always a self. 1 2 3 4 5 6 7 8 9 10 11 12 p self # shows "main" class Foo def bar (a = (p self)) end end foo = Foo .new foo.bar # shows "# how to lose belly fat fast <Foo: 0x471004> "class Foo class Baz <(p self; self) # show" Foo "end end
I called the concept of 'klass' in the old article, but do not know if it's the best name. It is standard on which class the method is defined. Now I would like to call it "standard setter".
Ruby always holds the reference to a class as well as the self. But there is no way to retrieve it directly. It's implied that self. If you define a method without providing a specific receptor, in other words, a method is set to the normal way of syntactic methods set, the default will defining the method as an instance how to lose belly fat fast method. Examples:
At the top level, is the Object class. So global functions are also instance methods in the Object class as you already know. 1 2 def hoge; Kernel end .instance_method (: hoge) # => # <UnboundMethod: Object # hoge>
The class syntax changes both self and defining the standard for the class that is now being set. 1 2 3 4 class T def hoge; end end .instance_method T (: hoge) # => # <UnboundMethod: T # hoge>
In a normal body method, self is the recipient of method invocation and the standard defining the class is syntactically out of it, now it is T. 1 2 3 4 5 6 7 8 9 class T def def hoge escape; how to lose belly fat fast end end end t = T .new t.hoge t.method (: Trail) # => # <Method: T # trail> T .instance_method (: Trail) # => # <UnboundMethod: T # trail>
What instance_eval do is: change the self to instance_eval how to lose belly fat fast receiver change the defining standard for eigenclass the receiver how to lose belly fat fast if the receiver does not have a eigenclass also creates one. executes the block as 1 2 3 4 5 6 7 = Object .new o.instance_eval of self p # => # <Object: 0x454f24> def hoge; end end o.method (: hoge) # => # <Method: # <Object: 0x454f24> .hoge> Object .instance_method (: hoge) # raises the NameError "undefined method` hoge 'for class' Object' "
Ruby 1.8 acts more lexical way, then you will end up having the opposite: 1 2 3 4 $ o .Method (: Trail) # raises to $ NameError the .Method (: piyo) # raises the NameError T .instance_method (: Trail) # => # <UnboundMethod: T # trail> T .instance_method (: piyo) # => # <UnboundMethod: T # piyo>
In Ruby 1.8, the standard defining the method body is lexically based on the definition of the outer class. Anyway, both as Ruby 1.8 1.9 instance_eval how to lose belly fat fast self changes to the receiver, the pattern defining at its eigenclass.
When you see an instance variable, it is a self instance variable. When you use a class variable, it is a self class class variable; or own self when self is a class.
We will discuss this concept of "cref" in another article. All links in this Article old former Article Article written a great complement Related Articles [Translation] Metaprogramming Ruby: is all about Self
Company Codeminer 42 Achieve Leap Screencasts (en-US) Starting with Git Starting with Vim Installing a Ruby Environment [Lecture] Understand Software Correct Way


No comments:

Post a Comment