Skip to content

Instantly share code, notes, and snippets.

@freakhill
Last active December 15, 2015 15:39
Show Gist options
  • Save freakhill/5283615 to your computer and use it in GitHub Desktop.
Save freakhill/5283615 to your computer and use it in GitHub Desktop.
require 'ripper'
require 'sourcify'
def lines(source)
Enumerator.new do |out|
source.split("\n")[1..-2].reduce("") do |buffer, line|
buffer << "\n#{line}"
Ripper.sexp_raw(buffer) ? (out << buffer) && "" : buffer
end
end
end
# @param [Binding] world Binding to use for evaluation of statements
# @param [Proc] line2ansp Proc that takes the line to process as parameter and returns
# a Proc taking the result of previous line's evaluation as parameter
# @param [Proc] &block Block of code to process.
def line_eval(world, line2ansp, &block)
lines(block.to_source).each { |line| line2ansp[line][eval(line, world)] }
end
#example
def printrun(world = proc { binding }[], &block)
i = 0
line_eval(world,
proc { |line|
puts "#{i += 1} >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>#{line}\n --- "
proc { |ans| puts "#{ans}\ndone!" }
}, &block)
end
printrun do
a = 3
puts a
end
# careful of the current bug of sourcify with double quotes...
@ic
Copy link

ic commented Apr 2, 2013

This version works, cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment