Skip to content

Instantly share code, notes, and snippets.

@netsprout
Created March 26, 2019 18:00
Show Gist options
  • Save netsprout/5fa6c7468a4a930b1278d804cf60d780 to your computer and use it in GitHub Desktop.
Save netsprout/5fa6c7468a4a930b1278d804cf60d780 to your computer and use it in GitHub Desktop.
Fun with Ruby Closures
def aclosure(outer)
-> (inner) {inner + outer}
end
add3 = aclosure(3)
add10 = aclosure(10)
puts add3.call(5)
puts add10.call(5)
def fill_me_up(num=0, &block)
puts "Start #{num}..."
yield(num) if block_given?
puts '...end'
end
fill_me_up(1)
fill_me_up(2) do |num|
puts "Some stuff added here!: The Number=#{num}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment