什么是“等于大于"?运算符 =>在红宝石? [英] What is the "equals greater than" operator => in Ruby?

查看:43
本文介绍了什么是“等于大于"?运算符 =>在红宝石?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Ruby on Rails 教程中,我被要求输入:

In a Ruby on Rails tutorial, I am asked to type:

class Post < ActiveRecord::Base
    validates :name,  :presence => true  
    validates :title, :presence => true, :length => { :minimum => 5 }
end

我明白这是做什么的,但我想知道 => 运算符是什么.在 PHP-land 中,它将键和值链接到关联数组中.这里是一样的吗?是 Ruby 操作符还是 Rails 操作符?

I understand what this does, but I would like to know what the => operator is. In PHP-land, it links a key and a value in an associative array. Is it the same thing here? Is it a Ruby operator or a Rails operator?

推荐答案

它主要是一个 ruby​​ 运算符,用于设置散列中的键的值.因此:

It is mainly a ruby operator that sets the value of a key inside a hash. Thus :

{ :minimum => 5 }

是一个 ruby​​ 散列,以符号 :minimum 作为键,映射到 5 的值.在此示例中,一个包含一个条目的散列.同样适用于:

Is a ruby hash that has the symbol :minimum as a key that maps to the value of 5. A hash with one entry, in this example. Same for :

:presence => true

仍然是一个哈希值.但是,在 ruby​​ 中,当您有一个方法时,您可以省略围绕散列的 {}.这就是 validates 方法发生的情况.它是一种方法,因此传递的哈希值并不明确需要 {}.

Still a hash. However, in ruby, when you have a method, you can omit the {} that surround a hash. That is what happens with the validates method. It's a method and thus the passed hash does not explicitly need {}.

这篇关于什么是“等于大于"?运算符 =>在红宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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