何时在 Ruby on Rails 中使用 lambda?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1232817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use a lambda in Ruby on Rails?
提问by Cameron
When should a lambda or proc be used? I have seen them described as anonymous functions, but I am struggling to understand this concept. I would appreciate any links to or examples of when you might use one in Ruby, but especially in Ruby on Rails.
什么时候应该使用 lambda 或 proc?我已经看到它们被描述为匿名函数,但我很难理解这个概念。如果您可以在 Ruby 中使用它的任何链接或示例,我将不胜感激,尤其是在 Ruby on Rails 中。
采纳答案by August Lilleaas
http://augustl.com/blog/2008/procs_blocks_and_anonymous_functions/has a run-down of what blocks/procs/lambdas are, how you can use them, and how they compare to functions in other languages. It definitely answers your question.
http://augustl.com/blog/2008/procs_blocks_and_anonymous_functions/简要介绍了 blocks/procs/lambdas 是什么、如何使用它们以及它们与其他语言中的函数的比较。它绝对回答了你的问题。
Do be aware that the last section 'A note on lambdas' mentions a point that is only true in Ruby 1.8 and changed in 1.9 - Ruby: Proc.new { 'waffles' } vs. proc { 'waffles' }
请注意,最后一节“关于 lambdas 的注释”提到了一个仅在 Ruby 1.8 中成立并在 1.9 中更改的观点- Ruby: Proc.new { 'waffles' } vs. proc { 'waffles' }
回答by ehsanul
I don't see where you make the distinction between Ruby on Rails and Ruby. If you're writing a Ruby on Rails application, you're writing Ruby code, so if it's useful in Ruby, it should be useful in Ruby on Rails.
我看不出你在哪里区分 Ruby on Rails 和 Ruby。如果您正在编写 Ruby on Rails 应用程序,那么您正在编写 Ruby 代码,因此如果它在 Ruby 中有用,那么它在 Ruby on Rails 中也应该很有用。
Anyway, this article, Some Useful Closures in Ruby, should be helpful, as well as this: http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
无论如何,这篇文章,Ruby 中的一些有用的闭包,应该会有所帮助,还有这个:http: //www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
回答by ez.
It is a piece of code that allows you to pass around.
它是一段代码,可以让你四处传递。
It is especially useful in named_scope, it allows to you do something like this:
它在 named_scope 中特别有用,它允许你做这样的事情:
named_scope :scoped_by_user, lambda {|user| {:conditions=>{:user_id=>user.id}}}
Say you have a Project model and you want to get all the projects for one particular user, you can do something like this:
假设您有一个 Project 模型,并且想要获取一个特定用户的所有项目,您可以执行以下操作:
Project.scoped_by_user(123)
回答by MadNik
What is lambda?
什么是拉姆达?
Try this with your irb.
用你的irb试试这个。
lam = lambda { puts "Hello world"}
lam.class
#output is
=> Proc
lambda in ruby is also a instance of Proc class. lambdas are a different flavor of procs.
ruby 中的 lambda 也是 Proc 类的一个实例。lambdas 是一种不同风格的 proc。
What is Proc?
什么是Proc?
Proc objects are blocks of code that have been bound to a set of local variables.
Proc 对象是绑定到一组局部变量的代码块。
proc = Proc.new { puts "Hello World" }
proc.call
#output is
=> Hello World
What is the difference between a proc and lambda? Comparission will explain usecases
proc 和 lambda 之间有什么区别?比较将解释用例
Lambdas check the number of arguments, while procs do not.
Lambdas 检查参数的数量,而 procs 不检查。
multiply = lambda { |x,y| x*y }
multiply.call(2,3) #=>6
multiply.call(2) #ArgumentError: wrong number of arguments (1 for 2)
multiply = Proc.new {|x| x*x}
multiply.call(2) # =>4
multiply.call(2,3) # =>4 (It ignore the second argument)
Lambdas and procs treat the ‘return' keyword differently (Read the article below for example)
Lambdas 和 procs 以不同的方式对待“return”关键字(例如阅读下面的文章)
Read this great article for more details http://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/
阅读这篇很棒的文章以获取更多详细信息 http://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/
回答by sudonim
Where I've seen Lambda used is in testing...
我见过使用 Lambda 的地方是在测试中......
lambda do
post :create, :user => @attr
end.should_not change(User, :count)
Lambda allows you to have that test at the end to make sure that the code executed in the lambda block doesn't change change the User count.
Lambda 允许您在最后进行该测试,以确保在 lambda 块中执行的代码不会更改用户计数。
回答by ErsatzRyan
lambda is exceptionally useful in named_scope, so that you can pass parameters to named_scopes.
lambda 在 中非常有用named_scope,因此您可以将参数传递给named_scopes。
回答by Schwern
Many validatoroptions accept a lambda or Proc if the work is too simple to warrant a named function. For example, :ifand :unless.
如果工作太简单而无法保证命名函数,则许多验证器选项都接受 lambda 或 Proc。例如,:if和:unless。
validates :thing, presence: true, unless: ->(obj) { obj.something? }
:messagewill accept a Proc for custom validator messages.
:message将接受自定义验证器消息的 Proc。
validates :terms, acceptance: {
message: ->(person) "%{person.name} must accept our terms!"
}
Generally, lambdas (and callbacks in general) are useful as a lightweight way to allow more customization than a string or number allows, but without the user having to write a full class.
通常,lambdas(以及一般的回调函数)作为一种轻量级的方式很有用,它允许比字符串或数字允许的更多自定义,但用户不必编写完整的类。

