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

  • Home
  • SEARCH
  • 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 6171579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:14:24+00:00 2026-05-23T23:14:24+00:00

I just started reading this book Eloquent Ruby and I have reached the chapter

  • 0

I just started reading this book Eloquent Ruby and I have reached the chapter about Symbols in Ruby.

Strings in Ruby are mutable, which means each string allocate memory since the content can change, and even though the content is equal. If I need a mutable String in Java I would use StringBuffer. However since regular Java Strings are immutable one String object can be shared by multiple references. So if I had two regular Strings with the content of “Hello World”, both references would point to the same object.

So is the purpose of Symbols in Ruby actually the same as “normal” String objects in Java? Is it a feature given to the programmer to optimize memory?

Is something of what I written here true? Or have I misunderstood the concept of Symbols?

  • 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-05-23T23:14:25+00:00Added an answer on May 23, 2026 at 11:14 pm

    Symbols are close to strings in Ruby, but they are not the equivalent to regular Java strings, although they, too, do share some commonalities such as immutability. But there is a slight difference – there is more than one way to obtain a reference to a Symbol (more on that later on).

    In Ruby, it is entirely possible to convert the two back and forth. There is String#to_sym to convert a String into a Symbol and there is Symbol#to_s to convert a Symbol into a String. So what is the difference?

    To quote the RDoc for Symbol:

    The same Symbol object will be created for a given name or string for the duration of a program‘s execution, regardless of the context or meaning of that name.

    Symbols are unique identifiers. If the Ruby interpreter stumbles over let’s say :mysymbol for the first time, here is what happens: Internally, the symbol gets stored in a table if it doesn’t exist yet (much like the “symbol table” used by parsers; this happens using the C function rb_intern in CRuby/MRI), otherwise Ruby will look up the existing value in the table and use that. After the symbol gets created and stored in the table, from then on wherever you refer to the Symbol :mysymbol, you will get the same object, the one that was stored in that table.

    Consider this piece of code:

    sym1 = :mysymbol
    sym2 = "mysymbol".to_sym
    
    puts sym1.equal?(sym2) # => true, one and the same object
    
    str1 = "Test"
    str2 = "Test"
    
    puts str1.equal?(str2) # => false, not the same object
    

    to notice the difference. It illustrates the major difference between Java Strings and Ruby Symbols. If you want object equality for Strings in Java you will only achieve it if you compare exactly the same reference of that String, whereas in Ruby it’s possible to get the reference to a Symbol in multiple ways as you saw in the example above.

    The uniqueness of Symbols makes them perfect keys in hashes: the lookup performance is improved compared to regular Strings since you don’t have to hash your key explicitly as it would be required by a String, you can simply use the Symbol’s unique identifier for the lookup directly. By writing :somesymbol you tell Ruby to “give me that one thing that you stored under the identifier ‘somesymbol'”. So symbols are your first choice when you need to uniquely identify things as in:

    • hash keys
    • naming or referring to variable, method and constant names (e.g. obj.send :method_name )

    But, as Jim Weirich points out in the article below, Symbols are not Strings, not even in the duck-typing sense. You can’t concatenate them or retrieve their size or get substrings from them (unless you convert them to Strings first, that is). So the question when to use Strings is easy – as Jim puts it:

    Use Strings whenever you need … umm … string-like behavior.

    Some articles on the topic:

    • Ruby Symbols.
    • Symbols are not immutable strings
    • 13 Ways of looking at a Ruby Symbol
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a bunch of data on S3 (images) but just started reading about
I have just started reading about neural networks and I have a basic question.
I have just started reading Modern C++ Design Generic programming and Design Patterns Applied
I just started programming in C# and was reading about dividing your application /
I just have started to learn Haskell and combine reading books and tutorials with
I just started reading about JPA and its implementation in Hibernate. To understand the
I have just started reading Spring In Action - Third edition and am stuck
I have just started reading up on Lucene. In one of the examples provided,
I am just learning about escaping things and started reading about how it could
I have just started learning Objective-C, I am reading Programming in Objective-C 3rd Edition

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.