Skip to content

Instantly share code, notes, and snippets.

@ender672
Created June 27, 2009 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ender672/136829 to your computer and use it in GitHub Desktop.
Save ender672/136829 to your computer and use it in GitHub Desktop.
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