Skip to content

Instantly share code, notes, and snippets.

@gregolsen
Created November 19, 2012 19:02
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 gregolsen/4112908 to your computer and use it in GitHub Desktop.
Save gregolsen/4112908 to your computer and use it in GitHub Desktop.
def to_lambda &block
obj = Object.new
obj.define_singleton_method(:_, &block)
return obj.method(:_).to_proc
end
def with_file(name, &block)
l = to_lambda(&block)
puts "Open file"
f = File.open(name, "r")
l[f]
puts "Close file"
f.close
end
content = ""
with_file("/etc/hosts") do |f|
puts "Read content"
return 'hho'
content << f.read
end
p content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment