SlideShare a Scribd company logo
Blocks, procs &&
lambdas
#16 ruby workshop, 2013-03-24
Vidmantas Kabošis
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
Closures in Ruby
1. Block
2. Proc
3. proc
4. lambda
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*
Proc
● Pretty usual method object
● Call
● Pass any number of argument(s)
● respond_to(:arity)
● Flow control keywords (return, break, redo,
retry, ...)
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 can
become an object when referenced
(explicitly)
● Proc.new == proc, don't care about args too
much & returns
● lambda counts args & diminutive returns
end!

More Related Content

PDF
The art of concurrent programming
Iskren Chernev
 
PDF
Introduction to ZeroMQ - eSpace TechTalk
Mahmoud Said
 
PDF
Find the bottleneck of your system
Jian-Hong Pan
 
PDF
Zeromq anatomy & jeromq
Dongmin Yu
 
KEY
Distributed app development with nodejs and zeromq
Ruben Tan
 
PDF
On component interface
Laurence Chen
 
PDF
ZeroMQ with NodeJS
Fernando Sanabria
 
PDF
CODEsign 2015
Max Kleiner
 
The art of concurrent programming
Iskren Chernev
 
Introduction to ZeroMQ - eSpace TechTalk
Mahmoud Said
 
Find the bottleneck of your system
Jian-Hong Pan
 
Zeromq anatomy & jeromq
Dongmin Yu
 
Distributed app development with nodejs and zeromq
Ruben Tan
 
On component interface
Laurence Chen
 
ZeroMQ with NodeJS
Fernando Sanabria
 
CODEsign 2015
Max Kleiner
 

What's hot (20)

PDF
Clojure concurrency overview
Sergey Stupin
 
PPTX
A simple tool for debug (tap>)
Laurence Chen
 
PDF
Node.js 1, 2, 3
Jian-Hong Pan
 
PPTX
Diagnosing HotSpot JVM Memory Leaks with JFR and JMC
Mushfekur Rahman
 
PPTX
Building a Unified Logging Layer with Fluentd, Elasticsearch and Kibana
Mushfekur Rahman
 
PDF
Wireshark lecture
Bhupesh Rawat
 
PDF
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Codemotion
 
PPT
Leveraging zeromq for node.js
Ruben Tan
 
PDF
Practical SystemTAP basics: Perl memory profiling
Lubomir Rintel
 
PDF
TANET 2018 - Insights into the reliability of open-source distributed file sy...
Hua Chu
 
ODP
Event Driven with LibUV and ZeroMQ
Luke Luo
 
PDF
Play Framework
Eduard Tudenhoefner
 
PDF
Socket Programming using Java
Rahul Hada
 
ODP
Overview of ZeroMQ
pieterh
 
PDF
Reflection
Marcus Denker
 
PPTX
Coroutines in Kotlin
Jemo Mgebrishvili
 
PPTX
Polymorphism in c++
Dheenadayalan18
 
PDF
PostgreSQL: present and near future
NaN-tic
 
PDF
[DevDay 2019] Reactive Programming with JavaScript - By Pham Nguyen Duc, Web ...
DevDay Da Nang
 
PDF
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
AboutYouGmbH
 
Clojure concurrency overview
Sergey Stupin
 
A simple tool for debug (tap>)
Laurence Chen
 
Node.js 1, 2, 3
Jian-Hong Pan
 
Diagnosing HotSpot JVM Memory Leaks with JFR and JMC
Mushfekur Rahman
 
Building a Unified Logging Layer with Fluentd, Elasticsearch and Kibana
Mushfekur Rahman
 
Wireshark lecture
Bhupesh Rawat
 
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Codemotion
 
Leveraging zeromq for node.js
Ruben Tan
 
Practical SystemTAP basics: Perl memory profiling
Lubomir Rintel
 
TANET 2018 - Insights into the reliability of open-source distributed file sy...
Hua Chu
 
Event Driven with LibUV and ZeroMQ
Luke Luo
 
Play Framework
Eduard Tudenhoefner
 
Socket Programming using Java
Rahul Hada
 
Overview of ZeroMQ
pieterh
 
Reflection
Marcus Denker
 
Coroutines in Kotlin
Jemo Mgebrishvili
 
Polymorphism in c++
Dheenadayalan18
 
PostgreSQL: present and near future
NaN-tic
 
[DevDay 2019] Reactive Programming with JavaScript - By Pham Nguyen Duc, Web ...
DevDay Da Nang
 
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
AboutYouGmbH
 
Ad

Similar to Blocks, procs && lambdas (16)

PDF
EKON 12 Closures Coding
Max Kleiner
 
PDF
Callable and runnable objects in ruby
Rahul Bajaj
 
PPT
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
PPT
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
PDF
02. Ruby Basic slides - Ruby Core Teaching
quanhoangd129
 
PDF
What is Closure and Its Uses in PHP
Andolasoft Inc
 
PDF
A closure ekon16
Max Kleiner
 
PPT
Wow! closure in_javascript
cnlangzi
 
PDF
Flow Control
Michal Píše
 
PPT
Andy On Closures
melbournepatterns
 
KEY
Learn Ruby 2011 - Session 5 - Looking for a Rescue
James Thompson
 
PDF
What's new in Ruby 2.0
Kartik Sahoo
 
PDF
12 ruby blocks
Walker Maidana
 
PPT
PHP 5.3 Part 2 - Lambda Functions & Closures
melechi
 
PDF
Ruby 1.9
guestaef7ea
 
PDF
Ruby closures, how are they possible?
Carlos Alonso Pérez
 
EKON 12 Closures Coding
Max Kleiner
 
Callable and runnable objects in ruby
Rahul Bajaj
 
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
02. Ruby Basic slides - Ruby Core Teaching
quanhoangd129
 
What is Closure and Its Uses in PHP
Andolasoft Inc
 
A closure ekon16
Max Kleiner
 
Wow! closure in_javascript
cnlangzi
 
Flow Control
Michal Píše
 
Andy On Closures
melbournepatterns
 
Learn Ruby 2011 - Session 5 - Looking for a Rescue
James Thompson
 
What's new in Ruby 2.0
Kartik Sahoo
 
12 ruby blocks
Walker Maidana
 
PHP 5.3 Part 2 - Lambda Functions & Closures
melechi
 
Ruby 1.9
guestaef7ea
 
Ruby closures, how are they possible?
Carlos Alonso Pérez
 
Ad

More from Vidmantas Kabošis (13)

PDF
One on Ones
Vidmantas Kabošis
 
PDF
In Quality We Trust
Vidmantas Kabošis
 
PDF
Dangerous Ruby (or: Job Security)
Vidmantas Kabošis
 
PDF
Freelancing platforms - diff
Vidmantas Kabošis
 
PDF
Ruby.new @ VilniusRB
Vidmantas Kabošis
 
PDF
10 tėvystės pamokų
Vidmantas Kabošis
 
PDF
Cucumber @ VilniusPHP
Vidmantas Kabošis
 
PDF
Ruby. Pradžia
Vidmantas Kabošis
 
PDF
Rails Girls Vilnius - lightning talk
Vidmantas Kabošis
 
PDF
Capistrano @ VilniusPHP
Vidmantas Kabošis
 
PDF
Ruby on rails @ Tobulėtuvė
Vidmantas Kabošis
 
PDF
RubyConfLT2012: Legacy / long running projects
Vidmantas Kabošis
 
PDF
ERb alternatyvos
Vidmantas Kabošis
 
One on Ones
Vidmantas Kabošis
 
In Quality We Trust
Vidmantas Kabošis
 
Dangerous Ruby (or: Job Security)
Vidmantas Kabošis
 
Freelancing platforms - diff
Vidmantas Kabošis
 
Ruby.new @ VilniusRB
Vidmantas Kabošis
 
10 tėvystės pamokų
Vidmantas Kabošis
 
Cucumber @ VilniusPHP
Vidmantas Kabošis
 
Ruby. Pradžia
Vidmantas Kabošis
 
Rails Girls Vilnius - lightning talk
Vidmantas Kabošis
 
Capistrano @ VilniusPHP
Vidmantas Kabošis
 
Ruby on rails @ Tobulėtuvė
Vidmantas Kabošis
 
RubyConfLT2012: Legacy / long running projects
Vidmantas Kabošis
 
ERb alternatyvos
Vidmantas Kabošis
 

Recently uploaded (20)

PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
The Future of Artificial Intelligence (AI)
Mukul
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 

Blocks, procs && lambdas

  • 1. Blocks, procs && lambdas #16 ruby workshop, 2013-03-24 Vidmantas Kabošis
  • 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. Closures in Ruby 1. Block 2. Proc 3. proc 4. lambda
  • 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. Proc ● Pretty usual method object ● Call ● Pass any number of argument(s) ● respond_to(:arity) ● Flow control keywords (return, break, redo, retry, ...)
  • 6. proc ● Ruby 1.8: proc == lambda ● Ruby 1.9 and up: proc == Proc ○ lambda?
  • 7. lambda ● Count of arguments ● return == break (proc) ● ->
  • 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