Blocks, procs &&lambdas#16 ruby workshop, 2013-03-24Vidmantas Kabošis
Closure (CS)● function or reference to a function togetherwith a referencing environment● closure <..> allows a function t...
Closures in Ruby1. Block2. Proc3. proc4. lambda
Blocks● NOT an object (mind=blown)● Implicit pass/call● Explicit pass/call*● Speed! Explicit invoke is considerably slower...
Proc● Pretty usual method object● Call● Pass any number of argument(s)● respond_to(:arity)● Flow control keywords (return,...
proc● Ruby 1.8: proc == lambda● Ruby 1.9 and up: proc == Proc○ lambda?
lambda● Count of arguments● return == break (proc)● ->
end● Implicit blocks are not objects, but canbecome an object when referenced(explicitly)● Proc.new == proc, dont care abo...
end!
Upcoming SlideShare
Loading in …5
×

Blocks, procs && lambdas

264 views

Published on

Small presentation about blocks, procs & lambdas in Ruby programming language

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
264
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Blocks, procs && lambdas

  1. 1. Blocks, procs &&lambdas#16 ruby workshop, 2013-03-24Vidmantas Kabošis
  2. 2. Closure (CS)● function or reference to a function togetherwith a referencing environment● closure <..> allows a function to accessthose non-local variables even when invokedoutside of its immediate lexical scope [1]1 - http://en.wikipedia.org/wiki/Closure_%28computer_science%29
  3. 3. Closures in Ruby1. Block2. Proc3. proc4. lambda
  4. 4. Blocks● NOT an object (mind=blown)● Implicit pass/call● Explicit pass/call*● Speed! Explicit invoke is considerably slower(~50%)*● Implicit block within implicit block?● Implicit block behaves like Proc*
  5. 5. Proc● Pretty usual method object● Call● Pass any number of argument(s)● respond_to(:arity)● Flow control keywords (return, break, redo,retry, ...)
  6. 6. proc● Ruby 1.8: proc == lambda● Ruby 1.9 and up: proc == Proc○ lambda?
  7. 7. lambda● Count of arguments● return == break (proc)● ->
  8. 8. end● Implicit blocks are not objects, but canbecome an object when referenced(explicitly)● Proc.new == proc, dont care about args toomuch & returns● lambda counts args & diminutive returns
  9. 9. end!

×