Ruby on Rails 变量、对象属性和在它们之前或之后使用 : 的方法 [英] Ruby on Rails variables, object attributes, and methods that use a : before or after them

查看:43
本文介绍了Ruby on Rails 变量、对象属性和在它们之前或之后使用 : 的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个一般性问题,我只是希望对有关冒号位置的 RoR 语法有所了解.

This is a general question I'm just hoping to gain a little clarity about RoR syntax on the placement of the colon.

我正在第二次阅读 Michael Hartl 的 Rails 教程,我想知道是否有人愿意向我解释在名称之前或之后使用 : 调用方法或变量之间的区别?例如,Users 类的email"属性有时用 :email 调用,有时用 email: 调用.我知道 ruby​​ 非常通用,所以我想我真正的问题是,ruby 解释器是否将这些作为对相同对象/属性/方法的引用读取,或者冒号出现的位置的语法是否以某种方式改变了处理?一种方式调用,另一种方式更像是一种引用,相同的约定是否适用于所有对象?

I'm going through Michael Hartl's rails tutorial for a second time and I'm wondering if anyone would be kind enough to explain to me the difference between calling a method or variable with the : before or after the name? For instance, the "email" attribute to the Users class is sometimes called with :email and other times with email:. I know ruby is pretty versatile so I guess my real question is, does the ruby interpreter read these as a reference to the same object/attribute/method or does the syntax of where the colon appears change the handling in some way? Is one way calling and the other is more of a reference and would the same convention be applicable to all objects?

最后,我想这更像是一种 RoR 风格,还是当我深入研究 ruby​​ 时我会经常看到它?

And lastly I guess is this more of a RoR style or will I see it often as I venture deeper into ruby itself?

谢谢

因提出合法问题以帮助澄清语法用法和含义,然后感谢人们的回答而被否决?这里有些人真的需要克服自己.

Down-voted for asking a legitimate question to help clarify syntax usage and meanings and then thanking people for answering? Some people on here really need to get over themselves.

@AnthonyMichaelCook 我听到你在说什么,但这个问题非常基础,不需要代码示例来回答.两个人给出了清晰简洁的答案,另外一个人对变量赋值发表了评论.我感谢他们的回应并继续前进,然后有两个人落后并在事后投票;它只是有一个戒指而已.具有直接答案的简单问题几乎不需要一些冗长的问题/解释和多个示例,这些示例只会用冗余和/或不必要的信息惹恼读者.但在某种程度上,关于人们需要克服自己的评论,不仅源于这种被否决的投票,还源于我在自己寻找答案时阅读有关 stackoverflow 的其他问题的回答.我遇到了不止几个帖子,其中一些回复者几乎不尊重,并且给出了居高临下甚至不回答的观点,只是一个明智的评论.几年前,当我第一次来到这个网站时,我是一名网络管理员,我的印象是,也许社区与我以前是常客/贡献者时的情况相比发生了一些变化.

@AnthonyMichaelCook I hear what you're saying, but the question was very basic and didn't require code samples in order to be answered. Two people gave clear concise answers and 1 other commented on variable assignments. I thanked them for their responses and moved on, then two ppl came behind and voted after the fact; it just has a ring a petty to it. Simple questions that have straightforward answers hardly require some long drawn out question/explanation with multiple examples that just annoy the reader with redundant and/or unnecessary information. But in-part the comment about people needing to get over themselves, was derived not just from this being down-voted but also from responses I read to other questions on stackoverflow while searching for the answer myself. I came across more than a few posts where some responders were borderline disrespectful and gave condescending or even non-answers and just a wise-buck comment. I was a network admin years ago when I first came to this site and I just get the impression that maybe the community has changed a bit from what it was when I used to be a regular visitor/contributor.

推荐答案

:符号

{key: value}

当冒号变量/对象之前时,它表示一个符号",意味着要放置在那里的一段数据.该符号通常可用于调用属性(键)或散列的一部分:@user.comment[:created_at]

When the colon is before the variable / object, it denotes a "symbol", meaning a piece of data to be placed there. The symbol can typically be used in the likes of calling an attribute (key) or in part of a hash: @user.comment[:created_at]

当冒号变量/对象之后时,这意味着您正在设置获取<代码中的某种值>key: value 对.{key: value} 对主要用于散列,有点像数组,但您可以调用特定的键.这就是在 Rails 中调用模型属性的方式.EG:

When the colon is after the variable / object, it means you're setting or getting some sort of value in a key: value pair. {key: value} pairs are used mostly in hashes, kind of like an array except you can call the specific keys. This is how the attributes of models are invoked in Rails. EG:

@user = {name: "joe", email: "joe@email.com"}
@user.name #-> "joe"

这篇关于Ruby on Rails 变量、对象属性和在它们之前或之后使用 : 的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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