Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8220957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:43:50+00:00 2026-06-07T13:43:50+00:00

First time I tried learning Ruby was 2 years ago, now I have started

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:43:51+00:00Added an answer on June 7, 2026 at 1:43 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First time I have tried to implement either of these and I am unsure
It's not the first time I have tried to execute a system command from
I have never used Emacs before and the first time I tried it I
I first time tried to subClassed an NSDate to give it 2 methods that
First time when I tried to do something in WPF, I was puzzled with
I tried using PHPDoc for the first time today and quickly ran into a
this is my first time asking a question here. I tried to be well
I tried to update an fullcalendar event with updateEvent but only the first time
I am trying White for the first time. I tried to run a sample
first time post here. I was hoping someone could help me make custom SPARQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.