A detailed analysis of instance_eval methods in Ruby and its comparison with class_eval _ruby topics

Source: Internet
Author: User
Tags instance method

Instance_eval method

This basicobject#instance_eval is somewhat similar to the Bind method in JS, when bind is passed this into the object, and Instance_eval is the block of code (context probes Probe) Passed in to the specified object, one for the object and one for the executing body. In this way, you can access the variables in the caller object in the code block in Instance_eval.

Sample code

Class MyClass
  def initialize
    @v = 1 end of
obj = myclass.new

obj.instance_eval do
  self  #=> #<myclass:0x33333 @v=1>
  @v   #=> 1 
end

v = 2
obj.instance_eval {@v = v}< c15/>obj.instance_eval {@v}  # => 2

In addition, the Instance_eval method also has a twin brother: Instance_exec method. The latter is more flexible than the former, allowing incoming arguments to the code block.

Sample code

Class C
  def initialize
    @x = 1
  end
class D
  def twisted_method
    @y = 2
    # C.new.instance_eval {"@x: #{@x}, @y>: #{y}"}
    c.new.instance_exec (@y) {|y| ' @x: #{@x}, @y: #{y} ' end
#D. New.twisted_method  # => "@x:1, @y:"
d.new.twisted_ Method  # => "@x:1, @y:2"

Because the caller is the current self after the call to Instance_eval, the scope is replaced in Class C, and the previous scope does not take effect. At this point, if you want to access the previous @y variables, you need to package the parameters @y together with Instance_eval escape, but because the instance_eval can not carry parameters, so use their siblings instance_exec method.


The difference between Instance_eval and Class_eval
# # #instance_eval
The first information that can be obtained from the name is that the Instance_eval caller receiver must be an instance instance, and within Instance_eval block, self is the receiver instance itself.

Obj_instance.instance_eval do
 self # => obj_instance
 # Current class => obj_instance ' s singleton class< C23/>end
<!--more-->

According to this definition, if Instance_eval is invoked on an instance, a single state function of the instance can be defined in it Singleton_method

Class A
end

A = a.new
a.instance_eval does
 self # => a
 # current class => a ' s singleton class
 def Method1 puts ' this are a singleton method of the instance a ' end-a.method1 #=> This is
a Singl Eton method of Instance a

B = a.new
b.method1
#=>nomethoderror:undefined method ' method1 ' for #<a:0 X10043ff70>

Similarly, because class classes are an instance of class classes themselves, Instance_eval can also be used on classes, where you can define the class's Singleton_method, which is the class function of the class.

In other words, you can use Instance_eval to define class methods, which are easier to confuse and need to figure out.

Class A
End

A.instance_eval does
 self # => a
 # current class => a ' s singleton class
 def Metho D1
  puts ' this are a singleton method of Class A ' end-
a.method1 #=> This is
a singleton meth OD of class A
Class_eval

# # #class_eval

To see Class_eval, the first thing you can get from the name is that the Class_eval caller receiver must be a class, and within Class_eval block, self is the receiver class itself.

Class A
end

A.class_eval does
 self # => a
 # current class => a


According to this definition, if Class_eval is invoked on a class, then an instance function of the class can be defined in it Instance_method

Class A
end

a = a.new
a.method1
#=> nomethoderror:undefined method ' method1 ' for #<a:0  x10043ff70>

a.class_eval do
 self # => a
 # current class => a
 def method1
  puts ' it is a instance method of Class A ' end-
a.method1 #=> This are a instance method of
Class A

In other words, you can use Class_eval to define an instance function instance method, which is also more confusing and needs to be figured out.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.