Last active
December 15, 2015 15:39
-
-
Save freakhill/5283615 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This version works, cheers!