何时在 Ruby on Rails 中使用 lambda? [英] When to use a lambda in Ruby on Rails?

查看:32
本文介绍了何时在 Ruby on Rails 中使用 lambda?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候应该使用 lambda 或 proc?我已经看到它们被描述为匿名函数,但我很难理解这个概念.我希望您能在 Ruby 中使用它的任何链接或示例,尤其是在 Ruby on Rails 中.

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.

推荐答案

http://augustl.com/blog/2008/procs_blocks_and_anonymous_functions/ 简要介绍了什么是 blocks/procs/lambdas,如何使用它们,以及它们与其他语言中的函数的比较.它绝对可以回答您的问题.

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.

请注意,最后一节关于 lambdas 的注释"提到了一个仅在 Ruby 1.8 中成立并在 1.9 中更改的观点 - Ruby:Proc.new { 'waffles' } 与 proc { 'waffles' }

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' }

这篇关于何时在 Ruby on Rails 中使用 lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆