Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def block_to_string
stack = caller
return nil if stack.empty?
file_name, line = parse_stack_line stack.shift
return nil unless file_name
block_from_file file_name, line
end
def parse_stack_line(where)
match = where.match /^(.*):(\d+)/
[ match[1], match[2].to_i ] if match
end
def block_from_file(file_name, line)
content = File.readlines file_name
lines = []
i = line
until content[i] =~ /^\s*end(\s*|#|$)/ || i == content.size
lines << content[i]
i += 1
end
lines
end
block = block_to_string do
p 'hi'
p 'bye'
p "this is the end "
end
p block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.