Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ruby Symbols

Tags:

symbols

ruby

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.

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 

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

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.

like image 757
LuckyLuke Avatar asked Jul 12 '12 08:07

LuckyLuke


People also ask

How do you assign symbols in Ruby?

A Symbol object is created by prefixing an operator, string, variable, constant, method, class name with a colon (:). The symbol object will be unique for each different name but does not refer to a particular instance of the name, for the duration of a program's execution.

What is Ruby the symbol of?

The ruby has become a symbol of love and commitment. It was once thought to protect against misfortune and illness. Early cultures treasured the gem, believing that it held the power of life due to its color association with blood. It has also been thought to remedy bleeding and inflammation, and increase body warmth.

What does '?' Mean in Ruby?

Functions that end with ? in Ruby are functions that only return a boolean, that is, true, or false. When you write a function that can only return true or false, you should end the function name with a question mark.

What does @variable mean in Ruby?

In Ruby, the at-sign ( @ ) before a variable name (e.g. @variable_name ) is used to create a class instance variable.


1 Answers

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)  

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 "")

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

symbol vs string

like image 116
Sergio Tulentsev Avatar answered Sep 24 '22 12:09

Sergio Tulentsev