Created
September 24, 2016 01:56
-
-
Save sebabelmar/dd4c4ee5529d03981a7850ae917e062d to your computer and use it in GitHub Desktop.
Thoughts on 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
# outer = 1 | |
# def m | |
# inner = 99 | |
# puts "inner var = #{inner + outer}" # outer is out of reach | |
# end | |
# Thinking in JS world in m we have access to outer but in ruby NAH! | |
# Let's expose inner to outer... | |
# The block has access to outer and this is being pass to the | |
# internals of f(m) and internally is get added to a variable | |
# that lives in that scope. The Closure is related to outer and | |
# the block... since the block is an annonymous function... | |
# But you cant save it in a variable. Fails the definition of | |
# closure. | |
# outer = 1 | |
# def m | |
# inner = 99 | |
# yield inner | |
# puts "inner var = #{inner}" | |
# end | |
# m {|some_valid_num| outer += some_valid_num} # outer is accessable here YO! | |
# puts "outer var = #{outer}" | |
# -- Using Procs | |
# -------------------------- | |
# outer = 1 | |
# def m &a_block | |
# message = "This is crazy" | |
# a_block.call(message) | |
# end | |
# exclamation = "!!!!!!!!!!" | |
# m {|message| puts "#{message} Seba #{exclamation}"; exclamation = message} | |
# puts exclamation | |
# Passing the proc | |
# def m a_proc | |
# inner = 99 | |
# a_proc.call(inner) | |
# end | |
# outer = 1 | |
# m proc {|inner| outer += inner} | |
# puts "this is outer #{outer}" | |
# Now how we can assign the closure to a variable for defered execution | |
# outer = 1 | |
# def m a_var | |
# inner = 99 | |
# proc {inner + a_var} | |
# end | |
# closure = m(outer) | |
# closure2 = m(outer) | |
# outer = "WHAT??" | |
# p "closure: #{closure.call}" | |
# p "closure: 2 #{closure2.call}" | |
# p "closure: #{closure.call}" | |
# p "closure: 2 #{closure2.call}" | |
# p "closure: #{closure.call}" | |
# p "closure: 2 #{closure2.call}" | |
def method_a | |
lambda { return "return from lambda" }.call | |
return "method a returns" | |
end | |
def method_b | |
proc { return "return from proc" }.call | |
return "method b returns" | |
end | |
def method_c | |
proc { next "return from proc" }.call | |
return "method c returns" | |
end | |
p method_a | |
p method_b | |
p method_c | |
l = lambda {|x, y| "#{x} and #{y}"} | |
p l.call("Seba", "Pax") | |
p = proc {|x, y| "#{x} and #{y}"} | |
p p.call("SEBA") | |
p p.("SEBA") | |
p p::call("SEBA") | |
p p["SEBA"] | |
def count | |
counter = 0 | |
proc {counter += 1} | |
end | |
count1 = count | |
count2 = count | |
p "count1: #{count1.call}" | |
p '' | |
p "count1: #{count1.call}" | |
p '' | |
p "count2: #{count2.call}" | |
p '' | |
p "count2: #{count2.call}" | |
p '' | |
p "count1: #{count1.call}" | |
p '' | |
p "count1: #{count1.call}" | |
p '' | |
p "count2: #{count2.call}" | |
p '' | |
p "count2: #{count2.call}" | |
p '' | |
p "count2: #{count2.call}" | |
p '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment