• Stars
    star
    655
  • Rank 66,171 (Top 2 %)
  • Language
    Ruby
  • License
    Other
  • Created over 12 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Retrieve the binding of a method's caller in MRI 1.9.2+

Build Status Gem Version

binding_of_caller

(C) John Mair (banisterfiend) 2012

Retrieve the binding of a method's caller in MRI (>= 2.0.0) and RBX (Rubinius)

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.

Works in MRI Ruby (>= 2.0) and RBX (Rubinius)

Example: Modifying a local inside the caller of a caller

def a
  var = 10
  b
  puts var
end

def b
  c
end

def c
  binding.of_caller(2).eval('var = :hello')
end

a()

# OUTPUT
# => hello

Spinoff project

This project is a spinoff from the Pry REPL project.

Features and limitations

  • Works in MRI (>= 2.0.0) and RBX (Rubinius)
  • For MRI 1.9.x, use version "~> 0.8" of the gem, which included support for MRI before 2.0.
  • Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a Binding#of_caller there.
  • There is experimental support for jruby 1.7.x, but it only works in interpreted mode (i.e. use the option -Djruby.compile.mode=OFF or append compile.mode=OFF to your .jrubyrc)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contact

Problems or questions contact me at github

License

(The MIT License)

Copyright (c) 2012 (John Mair)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

method_source

return the sourcecode for a method
Ruby
344
star
2

plymouth

Start Pry in the context of a failed test
Ruby
178
star
3

funkify

Haskell-style partial application and composition for Ruby methods
Ruby
153
star
4

texplay

image manipulation tool for ruby and gosu
C
66
star
5

devil

ruby bindings for devil cross platform image loading library
Ruby
60
star
6

debug_inspector

A Ruby wrapper for the MRI 2.0 debug_inspector API
Ruby
58
star
7

free

force immediate garbage collection of an object
C
36
star
8

remix

Ruby modules re-mixed and remastered
Ruby
32
star
9

include_complete

Fixing the limitations in traditional Module#include
Ruby
19
star
10

pry-stack_explorer

MOVED TO https://github.com/pry/pry-stack_explorer
Ruby
18
star
11

prepend

prepends modules in front of a class; so method lookup starts with the module
C
16
star
12

selene

ruby spaceship game
Ruby
13
star
13

tremolo

Worms-like game for Ruby
Ruby
10
star
14

method_reload

fine grained code reloading
Ruby
10
star
15

state-ology

Clean and fast Object state transitions in Ruby using the Mixology C extension
Ruby
9
star
16

object2module

Convert Classes and Objects to Modules so they can be extended/included.
Ruby
9
star
17

pry-doc

Provide MRI Core documentation and source code for the Pry REPL
Ruby
9
star
18

mixology19

A version of the Mixology C extension that is 1.9.1 and 1.8.6 compatible
Ruby
8
star
19

pry-exception_explorer

MOVED TO https://github.com/pry/pry-exception_explorer
Ruby
7
star
20

gen_eval

a strange new breed of instance_eval
C
6
star
21

tweak

enhance classes with temporary functionality
Ruby
4
star
22

compat

Managing Ruby 1.9 and 1.8 C extension compatibility in one header file
C
3
star
23

custom_boolean

a hack to have if/else_if/else conditions with user-defined truthiness
Ruby
3
star
24

dup_eval

A souped up version of instance_eval in the vein of mix_eval
Ruby
3
star
25

strange_evals

a hodgepodge of alternatives to instance_eval
Ruby
3
star
26

pry-note

Ease refactoring and exploration by attaching notes to methods and classes in Pry"
Ruby
3
star
27

local_eval

instance_eval without changing self
Ruby
3
star
28

mult

a few utility functions written in C
C
2
star
29

basis

personal system setup for new *nix boxes
Shell
2
star
30

change_class2

change class of an object
Ruby
2
star
31

window-rotate-for-emacs

rotate the buffers for the active windows in emacs
2
star
32

pry_time

pry error console
Ruby
1
star
33

RailsExceptionExamples

Pry's @banisterfiend wanted a sample rails application to use for developmentโ€ฆ This is it with documentation.
Ruby
1
star
34

raise_shim

A shim for rb_raise
C
1
star
35

rumi

magic
C++
1
star
36

exercise

learning objc
Objective-C
1
star
37

baseline

organize project benchmarks into contexts
Ruby
1
star