What Is the Difference Between a Block, a Proc, and a Lambda in Ruby
来源:互联网 发布:js删除七牛云的图片 编辑:程序博客网 时间:2024/05/16 07:38
原文链接:http://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/
感觉写的很清晰易懂,虽然不一定全面,但是我感觉还是蛮不错的,转载了下。
What are blocks, procs, and lambdas?
Coder Talk: Examples of closures in ruby.
Plain old english: Ways of grouping code we want to run.
While it looks like these are all very similar, there are subtle differences that I will cover below.
Differences between Blocks and Procs
- Procs are objects, blocks are notA proc (notice the lowercase p) is an instance of the Proc class. This lets us call methods on it and assign it to variables. Procs can also return themselves. In contrast, a block is just part of the *syntax* of a method call. It doesn’t mean anything on a standalone basis and can only appear in argument lists.
- At most one block can appear in an argument listIn contrast, you can pass multiple procs to methods.
Differences between Procs and Lambdas
Before I get into the differences between procs and lambdas, it is important to mention that they are both Proc objects.
However, lambdas are a different ‘flavor’ of procs. This slight difference is shown when returning the objects.
The (lambda) notation is a reminder that while procs and lambdas are very similar, even both instances of the Proc class, they are also slightly different. Below are the key differences.
- Lambdas check the number of arguments, while procs do not In contrast, procs don’t care if they are passed the wrong number of arguments. As shown above, procs don’t freak out and raise errors if they are passed the wrong number of arguments. If the proc requires an argument but no argument is passed then the proc returns nil. If too many arguments are passed than it ignores the extra arguments.
- Lambdas and procs treat the ‘return’ keyword differently‘return’ inside of a lambda triggers the code right outside of the lambda code ‘return’ inside of a proc triggers the code outside of the method where the proc is being executed
So what is a closure?
Coder Talk: ‘A function or a reference to a function together with a referencing environment. Unlike a plain function, closures allow a function to access non-local variables even when invoked outside of its immediate lexical scope.’ – Wikipedia
Plain old english: Similar to a suitcase, it’s a group of code that when opened (i.e. called), contains whatever was in it when you packed it (i.e. created it).
Background Part 1: Lambda Calculus and Anonymous Functions
Lambda get its name from a type of calculus introduced in the 1930s to help investigate the foundations of mathematics. Lambda calculus helps make computable functions easier to study by simplifying its semantics. The most relevant of these simplifications is that is treats functions ‘anonymously’, meaning that no explicit names are given to functions.
Generally speaking, in programming the term lambda refers to anonymous functions. These anonymous functions are very common and explicit in some languages (i.e. Javascript) and implicit in others (i.e. Ruby).
Background Part 2: Where Does the Name Proc Come From
Proc is short for procedure, which is a set of instructions packaged as a unit to perform a specific task. In different languages these may be called functions, routines, methods, or the generic term callable units. They are usually made to be called multiple times and from multiple times in a program.
Summary Differences
- Procs are objects, blocks are not
- At most one block can appear in an argument list
- Lambdas check the number of arguments, while procs do not
- Lambdas and procs treat the ‘return’ keyword differently
- What Is the Difference Between a Block, a Proc, and a Lambda in Ruby
- What is the difference between a thread and a process?
- What is the difference between a definition and a declaration?
- What is the difference between a process and a thread?
- In SQL, what is the difference between a left join and a left outer join?
- What is the difference between a soft reference and a weak reference in Java?
- What is the difference between an EXE and a DLL?
- What is the difference between a Generative and Discriminative Algorithm?
- What is the difference between GenericServlet, HttpServlet and a Servlet?
- What is the difference between a = a * (2^3) and a = a << 3?
- What is the difference between a processor, a chip, a socket, and a core?
- What is the difference between a theorem, a lemma, and a corollary?
- What is the difference between a URI, a URL and a URN
- What's the difference between a thread and a process?
- What’s the difference between a stack and a heap?
- What's the difference between a URI and a URL?
- What is the difference between a von Neumann architecture and a Harvard architecture?
- What is the difference between a pack slip and a pick slip?
- Dubbo zookeeper 初探
- hdu 2216 Game III
- 2D翻转
- GUI编程学习
- akka入门-调用子Actor处理消息
- What Is the Difference Between a Block, a Proc, and a Lambda in Ruby
- Qtcreator中常用快捷键总结
- 疯狂XML学习笔记(9)-------------Schema内置类型
- JAVA基础笔记五——面向对象的几大特征
- C语言 时间函数
- hdoj 2546 饭卡 【01背包】
- MATLAB把ISE中产生的1QN格式的十六进制数据转换为十进制数进行分析
- POJ 题目3318 Matrix Multiplication(快速判断矩阵乘是否正确)
- A+B for Input-Output Practice (I)