Binding of caller.
require 'boc' class A def f p eval("x", Boc.value) end end Boc.enable A, :f x = 33 A.new.f # => 33
% gem install boc
Or from inside an unpacked .tgz download, rake install / rake uninstall.
Binding of caller: obtain a caller's binding.
MRI 1.9.2 or JRuby 1.6+ is required. Support for other Ruby platforms is a goal.
require 'boc/binding_of_caller' will define Binding.of_caller (not present by default).
require 'boc/binding_of_caller' class A def f Binding.of_caller do |bind| p eval("x", bind) end end end Boc.enable A, :f x = 33 A.new.f # => 33
This is not an actual compatibility layer since the call to enable is still necessary. Binding.of_caller is merely a convenience method for existing 1.8 code.
Feature Requests, Bug Reports: github.com/quix/boc/issues
Manual Download: github.com/quix/boc/archives/master
When Boc.enable(A, :f) is called it first makes an alias,
class A alias_method :f__impl, :f end
Following that, f is replaced by native method whose only tasks are to set Boc.value and then call f__impl.
After adapting the old continuation-based Binding.of_caller to 1.9.2 (quix.github.com/binding_of_caller), I found the result unsatisfying. There were syntax restrictions surrounding the use of it, and though workaroundable they raised practical problems.
While a caller's binding might be obtained by accessesing VM innards, this approach would be subject to future breakage. The implementation presented herein is a compromise. In exchange for restricting functionality (the additional requirement of enable), binding-of-caller may be implemented straightforwardly with only the public C API, meaning that it should work in future MRI releases.
James M. Lawrence < email@example.com >
Copyright (c) 2011 James M. Lawrence. All rights reserved. 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.