Skip to content
Browse files

updated docs and reordered some files

  • Loading branch information...
1 parent 7682b83 commit 2f79206d14d45d9b47e28f2305c6f1f755d42cf2 @banister committed Oct 18, 2011
Showing with 75 additions and 48 deletions.
  1. +33 −8 README.md
  2. +1 −1 Rakefile
  3. +41 −0 examples/example.rb
  4. +0 −39 ext/binding_of_caller/example.rb
View
41 README.md
@@ -3,25 +3,50 @@ binding_of_caller
(C) John Mair (banisterfiend) 2011
-FIXME: _tagline_
+_Retrieve the binding of a method's caller in MRI 1.9.2_
-FIXME: _description goes here_
+The `binding_of_caller` gem provides the `Binding#of_caller` method.
+
+Using `binding_of_caller` we can grab bindings from higher up the call
+stack and evaluate code in that context. Allows access to bindings arbitrarily far up the
+call stack, not limited to just the immediate caller.
+
+**Recommended for use only in debugging situations. Do not use this in production apps.**
+
+**Only works in MRI Ruby 1.9.2**
* Install the [gem](https://rubygems.org/gems/binding_of_caller): `gem install binding_of_caller`
-* Read the [documentation](http://rdoc.info/github/banister/binding_of_caller/master/file/README.markdown)
* See the [source code](http://github.com/banister/binding_of_caller)
-Example: Example description
+Example: Modifying a local inside the caller of a caller
--------
-Example preamble
+ def a
+ var = 10
+ b
+ puts var
+ end
+
+ def b
+ c
+ end
+
+ def c
+ binding.of_caller(2).eval('var = :hello')
+ end
+
+ a()
- puts "example code"
+ # OUTPUT
+ # => hello
Features and limitations
-------------------------
-Feature List Preamble
+* Only works with MRI 1.9.2
+* Broken in 1.9.3, support will hopefully be provided in the near
+* future.
+* Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a `Binding#of_caller` there.
Contact
-------
@@ -32,7 +57,7 @@ Problems or questions contact me at [github](http://github.com/banister)
License
-------
-(The MIT License)
+(The MIT License)
Copyright (c) 2011 (John Mair)
View
2 Rakefile
@@ -52,7 +52,7 @@ namespace :ruby do
end
end
-desc "build the biinaries"
+desc "build the binaries"
task :compile do
chdir "./ext/#{PROJECT_NAME}/" do
sh "ruby extconf.rb"
View
41 examples/example.rb
@@ -0,0 +1,41 @@
+unless Object.const_defined? :BindingOfCaller
+ $:.unshift File.expand_path '../../lib', __FILE__
+ require 'binding_of_caller'
+ require 'binding_of_caller/version'
+end
+
+outer = 10
+
+class Z
+ def z
+ u = 10
+ A.new.a
+ end
+end
+
+class A
+ def a
+ y = 10
+ B.new.b
+ end
+end
+
+class B
+ def b
+ x = 10
+ puts binding.of_caller(0).eval('local_variables')
+ puts binding.of_caller(1).eval('local_variables')
+ puts binding.of_caller(2).eval('local_variables')
+ puts binding.of_caller(3).eval('local_variables')
+ puts binding.of_caller(400).eval('local_variables')
+ end
+end
+
+Z.new.z
+
+# output:
+# => x
+# => y
+# => u
+# => outer
+# Exception
View
39 ext/binding_of_caller/example.rb
@@ -1,39 +0,0 @@
-require './binding_of_caller'
-
-outer = 10
-
-class Z
- def z
- u = 10
- A.new.a
- end
-end
-
-class A
- def a
- y = 10
- B.new.b
- end
-end
-
-class B
- def b
- x = 10
- puts binding_of_caller(0).eval('local_variables')
- puts binding_of_caller(1).eval('local_variables')
- puts binding_of_caller(2).eval('local_variables')
- puts binding_of_caller(3).eval('local_variables')
- puts binding_of_caller(400).eval('local_variables')
- end
-end
-
-def a; b; end; def b; binding_of_caller(10); end; a;
-
-# Z.new.z
-
-# output:
-# => x
-# => y
-# => u
-# => outer
-# Exception

0 comments on commit 2f79206

Please sign in to comment.
Something went wrong with that request. Please try again.