Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 需要关于绑定对象的帮助吗_Ruby_Binding_Ruby 2.0 - Fatal编程技术网

Ruby 需要关于绑定对象的帮助吗

Ruby 需要关于绑定对象的帮助吗,ruby,binding,ruby-2.0,Ruby,Binding,Ruby 2.0,从 类绑定的对象将执行上下文封装在代码中的某个特定位置,并保留该上下文以备将来使用。变量、方法、self的值以及在此上下文中可以访问的迭代器块都被保留。可以使用Kernel#Binding创建绑定对象,并且可用于Kernel#set _trace_func的回调 有谁能帮我通过上面每一条粗体线的单个例子来理解这个事实吗?方法: x = eval("foo", binding) rescue "foo undefined" puts x def foo; "foo"; end puts eva

类绑定的对象将执行上下文封装在代码中的某个特定位置,并保留该上下文以备将来使用。变量、方法、self的值以及在此上下文中可以访问的迭代器块都被保留。可以使用Kernel#Binding创建绑定对象,并且可用于Kernel#set _trace_func的回调


有谁能帮我通过上面每一条粗体线的单个例子来理解这个事实吗?

方法:

x = eval("foo", binding) rescue "foo undefined"
puts x

def foo; "foo"; end

puts eval("foo", binding)
puts eval("self", binding)
class Foo
  def eval_self
    eval("self", binding)
  end
end
puts Foo.new.eval_self
block = eval("yield", binding) rescue "no iterator block"
puts block

def block
  eval("yield", binding)
end
puts block { "iterator block" }
class Test
  def test
    a = 1
  end
end

set_trace_func proc { |event, file, line, id, binding, classname|
  puts eval("self", binding)
}
t = Test.new
t.test
这将产生:

foo undefined
foo
main
#<Foo:0x10c5a3018>
no iterator block
iterator block
main
Test
#<Test:0x10204cb28>
#<Test:0x10204cb28>
Test
main
#<Test:0x10204cb28>
#<Test:0x10204cb28>
#<Test:0x10204cb28>
main
main
main
自身的值:

x = eval("foo", binding) rescue "foo undefined"
puts x

def foo; "foo"; end

puts eval("foo", binding)
puts eval("self", binding)
class Foo
  def eval_self
    eval("self", binding)
  end
end
puts Foo.new.eval_self
block = eval("yield", binding) rescue "no iterator block"
puts block

def block
  eval("yield", binding)
end
puts block { "iterator block" }
class Test
  def test
    a = 1
  end
end

set_trace_func proc { |event, file, line, id, binding, classname|
  puts eval("self", binding)
}
t = Test.new
t.test
这将产生:

foo undefined
foo
main
#<Foo:0x10c5a3018>
no iterator block
iterator block
main
Test
#<Test:0x10204cb28>
#<Test:0x10204cb28>
Test
main
#<Test:0x10204cb28>
#<Test:0x10204cb28>
#<Test:0x10204cb28>
main
main
main
这将产生:

foo undefined
foo
main
#<Foo:0x10c5a3018>
no iterator block
iterator block
main
Test
#<Test:0x10204cb28>
#<Test:0x10204cb28>
Test
main
#<Test:0x10204cb28>
#<Test:0x10204cb28>
#<Test:0x10204cb28>
main
main
main
内核#设置跟踪功能:

x = eval("foo", binding) rescue "foo undefined"
puts x

def foo; "foo"; end

puts eval("foo", binding)
puts eval("self", binding)
class Foo
  def eval_self
    eval("self", binding)
  end
end
puts Foo.new.eval_self
block = eval("yield", binding) rescue "no iterator block"
puts block

def block
  eval("yield", binding)
end
puts block { "iterator block" }
class Test
  def test
    a = 1
  end
end

set_trace_func proc { |event, file, line, id, binding, classname|
  puts eval("self", binding)
}
t = Test.new
t.test
这将产生:

foo undefined
foo
main
#<Foo:0x10c5a3018>
no iterator block
iterator block
main
Test
#<Test:0x10204cb28>
#<Test:0x10204cb28>
Test
main
#<Test:0x10204cb28>
#<Test:0x10204cb28>
#<Test:0x10204cb28>
main
main
main
main
试验
#
#
试验
主要的
#
#
#
主要的
主要的
主要的

您可以在its.

中阅读更多关于
内核#set_trace_func
的信息,这正是我搜索的内容。请用一些小例子来解释这一点
Kernel#set_trace_func.