在Ruby中,"="是什么?是什么意思,它是如何工作的? [英] In Ruby what does "=>" mean and how does it work?

查看:119
本文介绍了在Ruby中,"="是什么?是什么意思,它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习Ruby时,我偶尔遇到过"=>"运算符.通常我会以

While learning Ruby I've come across the "=>" operator on occasion. Usually I see it in the form of

:symbol => value

并且似乎在将值传递给函数时经常使用.该运算符到底叫什么?它做什么/意味着什么?它是内置于Ruby中还是将不同的框架(如Rails和DataMapper)添加到符号类中?它仅与符号类一起使用吗?谢谢.

and it seems to be used frequently when passing values to functions. What exactly is that operator called? What does it do/mean? Is it built into Ruby or is it something that different frameworks like Rails and DataMapper add to the symbol class? Is it only used in conjunction with the symbol class? Thanks.

推荐答案

=>将键与哈希映射文字中的值分开.它不可重载,也没有专门连接到符号.

=> separates the keys from the values in a hashmap literal. It is not overloadable and not specifically connected to symbols.

hashmap文字的格式为{key1 => value1, key2 => value2, ...},但是当用作函数的最后一个参数时,可以省略花括号.因此,当您看到像f(:a => 1, :b => 2)这样的函数调用时,将使用一个参数调用f,这是一个具有键:a:b以及值12的哈希映射.

A hashmap literal has the form {key1 => value1, key2 => value2, ...}, but when used as the last parameter of a function, you can leave off the curly braces. So when you see a function call like f(:a => 1, :b => 2), f is called with one argument, which is a hashmap that has the keys :a and :b and the values 1 and 2.

这篇关于在Ruby中,"="是什么?是什么意思,它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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