x.instance_eval 更改您的上下文,因此 self 的计算结果为 x。
这允许您做很多事情,包括定义实例变量和实例方法,但仅限于 x。
x = Object.new
y = Object.new
# define instance variables for x and y
x.instance_eval { @var = 1 }
y.instance_eval { @var = 2 }
# define an instance method for all Objects
class Object
def var
@var
end
end
x.var #=> 1
y.var #=> 2
Ruby 允许您在几个地方为对象定义实例方法。一般,
在一个类中定义它们,并且这些实例方法在所有实例之间共享
该类的(如上面的def var)。
但是,我们也可以为单个对象定义一个实例方法:
# here's one way to do it
def x.foo
"foo!"
end
# here's another
x.instance_eval do
# remember, in here self is x, so bar is attached to x.
def bar
"bar!"
end
end
即使x 和y 具有相同的类,它们也不共享这些方法,因为它们只是为x 定义的。
x.foo #=> "foo!"
x.bar #=> "bar!"
y.foo #=> raises NoMethodError
y.bar #=> raises NoMethodError
现在在 ruby 中,一切都是对象,甚至是类。类方法只是实例方法
对于那个类对象。
# we have two ways of creating a class:
class A
end
# the former is just syntatic sugar for the latter
B = Class.new
# we have do ways of defining class methods:
# the first two are the same as for any other object
def A.baz
"baz!"
end
A.instance_eval do
def frog
"frog!"
end
end
# the others are in the class context, which is slightly different
class A
def self.marco
"polo!"
end
# since A == self in here, this is the same as the last one.
def A.red_light
"green light!"
end
# unlike instance_eval, class context is special in that methods that
# aren't attached to a specific object are taken as instance methods for instances
# of the class
def example
"I'm an instance of A, not A itself"
end
end
# class_eval opens up the class context in the same way
A.class_eval do
def self.telegram
"not a land shark"
end
end
再次注意,所有这些方法都是A 特定的,B 无法访问其中任何一个:
A.baz #=> "baz!"
B.telegram #=> raises NoMethodError
要从这里带走的重要一点是
类方法只是类Class的对象的实例方法