Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Blocks, procs && lambdas

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Wird geladen in …3
×

Hier ansehen

1 von 9 Anzeige
Anzeige

Weitere Verwandte Inhalte

Diashows für Sie (20)

Ähnlich wie Blocks, procs && lambdas (20)

Anzeige

Aktuellste (20)

Anzeige

Blocks, procs && lambdas

  1. 1. Blocks, procs && lambdas #16 ruby workshop, 2013-03-24 Vidmantas Kabošis
  2. 2. Closure (CS) ● function or reference to a function together with a referencing environment ● closure <..> allows a function to access those non-local variables even when invoked outside of its immediate lexical scope [1] 1 - http://en.wikipedia.org/wiki/Closure_%28computer_science%29
  3. 3. Closures in Ruby 1. Block 2. Proc 3. proc 4. 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 can become an object when referenced (explicitly) ● Proc.new == proc, don't care about args too much & returns ● lambda counts args & diminutive returns
  9. 9. end!

×