项目作者: shreeve

项目描述 :
Ruby gem to access bindings of calling methods
高级语言: Ruby
项目地址: git://github.com/shreeve/bindings.git
创建时间: 2015-06-14T00:27:31Z
项目社区:https://github.com/shreeve/bindings

开源协议:MIT License

下载


bindings

bindings is a Ruby gem that allows the bindings of calling methods to be accessed. Using this gem, you can easily access variables from calling methods, which makes it very easy to implement templating systems or other utilities that need similar access.

Usage

To access variables or other attributes of calling methods, just call Binding.of_caller(n) where n is a number that represents how many callers back you are requesting.

Example

  1. outer = 40
  2. class A
  3. def a
  4. a = 30
  5. B.new.b
  6. end
  7. end
  8. class B
  9. def b
  10. b = 20
  11. C.new.c
  12. end
  13. end
  14. class C
  15. def c
  16. c = 10
  17. puts Binding.of_caller(0).eval('local_variables') # c
  18. puts Binding.of_caller(1).eval('local_variables') # b
  19. puts Binding.of_caller(2).eval('local_variables') # a
  20. puts Binding.of_caller(3).eval('local_variables') # outer
  21. puts Binding.of_caller(999).eval('local_variables') rescue puts($!)
  22. end
  23. end
  24. A.new.a

Result

  1. c
  2. b
  3. a
  4. outer
  5. no such frame

License

This software is licensed under terms of the MIT License.