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.
o = nil
xmp 'o = C.new', binding
# => o = C.new
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/quality_extensions/kernel/example_printer.rb', line 29 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.
49 50 51 52 |
# File 'lib/quality_extensions/kernel/example_printer.rb', line 49 def xmp(code, binding = nil, = {}) result = put_statement(code, binding) puts "=> #{result}" end |