Module: ExamplePrinter
- Defined in:
- lib/qualitysmith_extensions/kernel/example_printer.rb
Overview
This was written because the irb/xmp that was already available seemed to be needlessly complex and needlessly dependent upon “irb” stuff. This alternative is dirt simple, and it still works.
Instance Method Summary collapse
-
#put_statement(code, binding = nil, file = __FILE__, line = __LINE__) ⇒ Object
(also: #stp)
Prints the given statement (
code
– a string) before evaluating it. -
#xmp(code, binding = nil) ⇒ Object
Pretty much compatible with irb/xmp.
Instance Method Details
#put_statement(code, binding = nil, file = __FILE__, line = __LINE__) ⇒ Object Also known as: stp
Prints the given statement (code
– a string) before evaluating it.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/qualitysmith_extensions/kernel/example_printer.rb', line 22 def put_statement(code, binding = nil, file = __FILE__, line = __LINE__) # This didn't work. Still got this error: undefined local variable or method `x' for #<TheTest:0xb7dbc358> (NameError) # Binding.of_caller do |caller_binding| # puts caller_binding # puts code # eval code, caller_binding # end puts code eval code, binding, file, line end |
#xmp(code, binding = nil) ⇒ Object
Pretty much compatible with irb/xmp. But you have currently have to pass in the binding manually if you have any local variables/methods that xmp should have access to.
37 38 39 40 |
# File 'lib/qualitysmith_extensions/kernel/example_printer.rb', line 37 def xmp(code, binding = nil) result = put_statement(code, binding) puts "=> #{result}" end |