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