Module: ExamplePrinter
- Defined in:
- lib/quality_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, options = {}) ⇒ 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. Is it instead of xmp when the return value isn’t interesting or might even be distracting to the reader.
o = nil
put_statement 'o = C.new', binding
# Outputs:
# o = C.new
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/quality_extensions/kernel/example_printer.rb', line 30 def put_statement(code, binding = nil, file = __FILE__, line = __LINE__) # We'd like to be able to just use the binding of the caller without passing it in, but unfortunately I don't know how (yet)... ## 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, options = {}) ⇒ 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.
o = nil
xmp '3 + x', binding
# Outputs:
# 3 + x
# => 4
56 57 58 59 |
# File 'lib/quality_extensions/kernel/example_printer.rb', line 56 def xmp(code, binding = nil, = {}) result = put_statement(code, binding) puts "=> #{result}" end |