Wikipedia A closure is a function or reference to a function together with a referencing environment - a table storing a reference to each of the non-local variables (also called free variables) of that function. A closure - unlike a plain function pointer - allows a function to access those non-local variables even when invoked outside of its immediate lexical scope. Tuesday, May 14, 13
blocks 999.times do |index| puts "#{ index } elefantes se balanceaban sobre la tela de una araña, como veian que resistía fueron a buscar otro elefante" end Tuesday, May 14, 13
Blocks ['python', 'ruby', 'lua'].each do |language| puts "#{ language } is a pretty cool language!" end #=> python is a pretty cool language! # ruby is a pretty cool language! # lua is a pretty cool language! Tuesday, May 14, 13
Procs p = Proc.new do |text| text.split(' ').each do |word| puts "#{ word } \n" } end puts "- ♪ 㽈" end p.call("uno dos tres cuatro cinco cinco seis") Tuesday, May 14, 13
Procs p = Proc.new do |text| text.split(' ').each do |word| puts "#{ word } \n" } end puts "- ♪ 㽈" end p.call("uno dos tres cuatro cinco cinco seis") Tuesday, May 14, 13
Lambdas def args(code) one, two = 1, 2 code.call(one, two) end args(Proc.new{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"}) args(lambda{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"}) # => Give me a 1 and a 2 and a NilClass # *.rb:8: ArgumentError: wrong number of arguments (2 for 3) (ArgumentError) Tuesday, May 14, 13
Links @poteland * Understanding Ruby Blocks, Procs and Lambdas - http://bit.ly/9qcstJ * This Talk - http://bit.ly/18IFJmP * More info - http://bit.ly/r8BnTA Tuesday, May 14, 13