使用 Ruby 符号 [英] Using Ruby Symbols

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

问题描述

我第一次尝试学习 Ruby 是在 2 年前,现在我又开始了.我停止的原因是因为我无法理解 Symbol 类.现在我又回到了同一点,完全迷失在何时以及为何使用 Symbols 中.我已经阅读了 Stackoverflow 上的其他帖子,并在谷歌上搜索了一些解释.但我还是不明白.

First time I tried learning Ruby was 2 years ago, now I have started again. The reason I stopped was because I could not understand the Symbol class. And now I am at the same point again, completely lost in when and why you use Symbols. I have read the other posts on Stackoverflow as well as Googled for several explanations. But I do not understand it yet.

首先,我认为符号只是一种创建某种命名常量"的方法,而不必经历与 Java 相同的过程.

First I thought symbols was just a way to create some sort of "named constant" without having to go through the same process as in let say Java.

:all 

而不是用任意值创建一个常量public static final String ALL = 8;

instead of making a constant with an arbitrary value public static final String ALL = 8;

然而,当你使用它时它没有多大意义,例如attr_accessor :first_name 等Symbols 只是一个轻量级的 String 类吗?我在理解我应该如何解释、何时以及如何在我自己的类和框架中使用符号时遇到问题.

However it does not make much sense when you use it in e.g. attr_accessor :first_name etc. Are Symbols just a lightweight String class? I am having problems understanding how I should interpret, when and how to use symbols both in my own classes and in frameworks.

推荐答案

简而言之,符号轻量级的字符串,但它们也是不可变且不可垃圾收集的.

In short, symbols are lightweight strings, but they also are immutable and non-garbage-collectable.

您应该不要在您的数据处理任务中将它们用作不可变的字符串(请记住,一旦符号被创建,它就不能被销毁).您通常使用符号来命名事物.

You should not use them as immutable strings in your data processing tasks (remember, once symbol is created, it can't be destroyed). You typically use symbols for naming things.

# typical use cases

# access hash value
user = User.find(params[:id])

# name something
attr_accessor :first_name

# set hash value in opts parameter
db.collection.update(query, update, multi: true, upsert: true)  

让我们举第一个例子,params[:id].在一个中等规模的 Rails 应用程序中,可能有成百上千的那些分散在代码库中.如果我们使用字符串访问该值,params["id"],这意味着每次都分配新的字符串(并且需要在之后收集该字符串).在符号的情况下,它实际上是到处相同的符号.内存分配器、垃圾收集器甚至你的工作更少(:"" 键入速度更快)

Let's take first example, params[:id]. In a moderately big rails app there may be hundreds/thousands of those scattered around the codebase. If we accessed that value with a string, params["id"], that means new string allocation each time (and that string needs to be collected afterwards). In case of symbol, it's actually the same symbol everywhere. Less work for memory allocator, garbage collector and even you (: is faster to type than "")

如果您的代码中经常出现一个简单的单字字符串,并且您没有对它做一些时髦的事情(插值、gsub、upcase 等),那么它可能是一个很好的符号候选.

If you have a simple one-word string that appears often in your code and you don't do something funky to it (interpolation, gsub, upcase, etc), then it's likely a good candidate to be a symbol.

但是,这是否仅适用于用作实际程序逻辑(例如命名)的一部分的文本,而不适用于您在实际运行程序时获得的文本...例如来自用户/网络等的文本?

However, does this apply only to text that is used as part of the actual program logic such as naming, not text that you get while actually running the program...such as text from the user/web etc?

我想不出任何一种情况,我想将数据从用户/网络转换为符号(可能除了解析命令行选项).主要是因为后果(一旦创建的符号永远存在).

I can not think of a single case where I'd want to turn data from user/web to symbol (except for parsing command-line options, maybe). Mainly because of the consequences (once created symbols live forever).

此外,许多编辑器为符号提供了不同的颜色,以在代码中突出显示它们.看看这个例子

Also, many editors provide different coloring for symbols, to highlight them in the code. Take a look at this example

这篇关于使用 Ruby 符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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