Skip to content

Instantly share code, notes, and snippets.

@rauchy
Created November 25, 2012 11:33
Show Gist options
  • Save rauchy/4143194 to your computer and use it in GitHub Desktop.
Save rauchy/4143194 to your computer and use it in GitHub Desktop.
Blocks In-depth: Procs
def connect(address, &log)
log.call("About to connect…")
response = do_actual_connection(address)
log.call("Error: #{response.message}") if response.error?
end
connect("http://localhost") { |msg| puts "Info: #{msg}" }
def connect(address, log)
log.call("About to connect…")
response = do_actual_connection(address)
log.call("Error: #{response.message}") if response.error?
end
my_proc = proc { |msg| puts "Info: #{msg}" }
connect("http://localhost", my_proc)
p = proc { |name| "Hello, #{name}!" }
p.("Omer") # huh?
p["Omer"] # oh my!
p === "Omer" # WTFZ
p = Proc.new { |name| "Hello, #{name}!" }
p.call("Omer") # => "Hello, Omer!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment