什么 ->Ruby 中的意思 [英] What does -> mean in Ruby

查看:44
本文介绍了什么 ->Ruby 中的意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在疯狂交易中看到了这一点.

I've seen that in spree commerce.

go_to_state :confirm, if: ->(order) { order.confirmation_required? }

那么这个符号会做什么?

So what'll do that symbol?

推荐答案

它是一个 lambda 文字.检查这个例子:

It is a lambda literal. Check this example:

 > plus_one = ->(x){x+1}
 => #<Proc:0x9fbaa00@(irb):3 (lambda)> 
 > plus_one.call(3)
 => 4 

lambda 文字是 Proc 的构造函数.Proc 是一种将代码块分配给变量的方法.在此之后,您可以根据需要使用不同的参数再次调用代码块.

A lambda literal is a constructor for Proc. A Proc is a way to have a block of code assigned to a variable. After this, you can call your block of code again, with different arguments, as many times as you wish.

这是在 ruby​​ 中传递函数"作为参数的方法.在许多语言中,您可以传递对函数的引用.在 ruby​​ 中,你可以传递一个 Proc 对象.

This is how you can pass a "function" as parameter in ruby. In many languages, you could pass a reference to a function. In ruby, you can pass a Proc object.

这篇关于什么 ->Ruby 中的意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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