Created
March 26, 2019 18:00
-
-
Save netsprout/5fa6c7468a4a930b1278d804cf60d780 to your computer and use it in GitHub Desktop.
Fun with Ruby Closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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