This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
The caller.rb script includes a few related methods.
|
||||
We will use the following code to demonstrate their use.
|
||||
|
||||
require 'facets/binding/caller'
|
||||
|
||||
a = 1
|
||||
b = 2
|
||||
x = "hello"
|
||||
|
||||
@bind = binding
|
||||
@line = __LINE__ # the line number must be updated if it moves
|
||||
@file = __FILE__ # why does it equal basename only?
|
||||
|
||||
## Binding#caller
|
||||
|
||||
@bind.caller
|
||||
|
||||
## Binding#callstack
|
||||
|
||||
@bind.callstack.assert.is_a?(Array)
|
||||
|
||||
## Binding#__LINE__
|
||||
|
||||
@bind.__LINE__.assert == @line - 1
|
||||
|
||||
## Binding#__FILE__
|
||||
|
||||
@bind.__FILE__.assert == @file
|
||||
|
||||
## Binding#__DIR__
|
||||
|
||||
@bind.__DIR__.assert == File.dirname(@file)
|
||||
|
||||
## Binding#__callee__
|
||||
|
||||
This only works for certain versions.
|
||||
|
||||
def alternate_callee
|
||||
binding
|
||||
end
|
||||
|
||||
unless RUBY_VERSION < "1.9"
|
||||
alternate_callee.__callee__.assert == :alternate_callee
|
||||
end
|
||||
|
||||
## Binding#__method__
|
||||
|
||||
This only works for certain versions.
|
||||
|
||||
def alternate_method
|
||||
binding
|
||||
end
|
||||
|
||||
unless RUBY_VERSION < "1.8.7"
|
||||
alternate_method.__method__.assert == :alternate_method
|
||||
end
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## Binding#defined?
|
||||
|
||||
require 'facets/binding/defined'
|
||||
|
||||
a = 1
|
||||
b = 2
|
||||
x = "hello"
|
||||
|
||||
binding.assert.defined?("x")
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
## Binding#[]
|
||||
|
||||
require 'facets/binding/op'
|
||||
|
||||
a = 1
|
||||
b = 2
|
||||
x = "hello"
|
||||
|
||||
binding["x"].assert == "hello"
|
||||
|
||||
## Binding#[]=
|
||||
|
||||
binding["x"] = "goodbye"
|
||||
|
||||
binding["x"].assert == "goodbye"
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
## Binding#self
|
||||
|
||||
require 'facets/binding/self'
|
||||
|
||||
binding.self == self
|
||||
|
||||
Reference in New Issue
Block a user