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
Prints the given statement (
code
– a string) before evaluating it.
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. Same as xmp only it doesn’t print the return value.
o = nil
xmp 'o = C.new', binding
# => o = C.new
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/qualitysmith_extensions/kernel/example_printer.rb', line 27 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
Prints the given statement (code
– a string) before evaluating it. Then prints its return value. 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.
43 44 45 46 |
# File 'lib/qualitysmith_extensions/kernel/example_printer.rb', line 43 def xmp(code, binding = nil) result = put_statement(code, binding) puts "=> #{result}" end |