Please give simple example of when to use symbols

I’m still a bit confused on whether to use strings or symbols in certain
situations. They produce the same results, although strings take up
more memory, which I’m not sure how or why exactly.

current_situation = “good”
puts “Everything is fine” if current_situation == “good”
puts “PANIC!” if current_situation == “bad”

“good” and “good” and “bad” (3 strings) take up memory. Why is that?
Isn’t the second “good” same as the first?

When do you normally use a symbol instead of a string?

Thanks guys!

“good” and “good” and “bad” (3 strings) take up memory. Why
is that? Isn’t the second “good” same as the first?

Quotes are just a shortcut for writing String.new–but not exactly. In
other words, quotes tell ruby to create a new object, and if you tell
ruby to create a new object, it will do your bidding.

When do you normally use a symbol instead of a string?

The easiest thing to do is never use symbols–symbols aren’t worth the
waste of time trying to understand them. Although, the idiotic
explanations I read are pretty hilarious. If you understand pointers,
then symbols are easy to understand. If you don’t know what a pointer
is, then you won’t ever understand symbols. You can rub all the
crystals you want and mediate on “the essence” of the thing, but you
still won’t have any idea what a symbol is.

Symbols also emit hydrocarbons, therefore they contribute to global
warming. So please, drive symbol-less and save the planet.

On Mon, Jul 25, 2011 at 8:51 AM, 7stud – [email protected]
wrote:

The easiest thing to do is never use symbols–symbols aren’t worth the
waste of time trying to understand them. Although, the idiotic
explanations I read are pretty hilarious. If you understand pointers,
then symbols are easy to understand. If you don’t know what a pointer
is, then you won’t ever understand symbol. You can rub all the crystals
you want and mediate on “the essence” of the thing, but you won’t have
any idea what you are dealing with.

This is really unhelpful. It is certainly possible to understand symbols
without understanding pointers, by looking at a few of their details and
when to use them in comparison to strings — which is exactly what is
being
asked for. Just because it might take a while to sink in doesn’t mean it
isn’t worthwhile.

On Mon, Jul 25, 2011 at 9:51 AM, 7stud – [email protected]
wrote:

waste of time trying to understand them. Although, the idiotic
explanations I read are pretty hilarious. If you understand pointers,
then symbols are easy to understand. If you don’t know what a pointer
is, then you won’t ever understand symbol. You can rub all the crystals
you want and mediate on “the essence” of the thing, but you won’t have
any idea what you are dealing with.

Why so grumpy? I can but support what Adam said. Symbols are useful
and it does make sense to use them as has been argued numerous times
here. The fact that you find that idiotic and hilarious probably
tells more about you than about the reasons which were given.

Symbols also emit hydrocarbons, so they contribute to global warming.

So do humans…

Cheers

robert

On Mon, Jul 25, 2011 at 2:20 AM, Kaye Ng [email protected] wrote:

I’m still a bit confused on whether to use strings or symbols in certain
situations. They produce the same results, although strings take up
more memory, which I’m not sure how or why exactly.

I think when people say they take up more memory, they mean that symbols
don’t get garbage collected. So if you make one, it is around until your
program ends. This could be a problem if you were generating them
dynamically, you could eat up all your memory.

When do you normally use a symbol instead of a string?

Check out the first 5 minutes of Ruby Kickstart Session 3 (
http://ruby-kickstart.com/#session3)

For some examples, you can watch past that.

On Mon, Jul 25, 2011 at 2:51 AM, 7stud – [email protected]
wrote:

It’s just an immutable string that doesn’t get garbage collected, and
every
symbol of a given name (text value) is the same object.

When do you normally use a symbol instead of a string?

If the exact sequence of characters is what’s important, use a String.
If the identity is what’s important, use a Symbol.

Here are some examples:

1.) A person’s name should be a String, because the sequence of
characters is important.

names = "John P. Jones".split
puts "first name is: #{names.first}"

2.) A status flag should be a Symbol, because the identity is what’s
important. The actual name is immaterial (although of course you want
to pick a readable name that makes sense).

missile.fire!
puts "jammed!" if missile.status == :launch_failure

3.) When referring to other program constructs, you should almost
always use Symbols.

obj.send(:some_method)

4.) The same rules apply when deciding whether to use Symbol or String
keys for a Hash. For instance, you’d probably have:

phonetic_pronunciations = {"pneumonia" => "new moan ya", ...}

but you’d also have:

colors = {:red => [255, 0, 0], :green => [0, 255, 0], :yellow =>

[255, 255, 0], …}

~ jf

John F.
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: User John Feminella - Stack Overflow

xxxx

When do you normally use a symbol instead of a string?

My rule of thumb is:

  • use a String when handling external data, e.g.
    data read/written to a file/database or
    data displayed to the user …
  • use a Symbol when handling internal data, e.g.
    description of a state, name of a method …

On 07/25/2011 02:20 AM, Kaye Ng wrote:

I’m still a bit confused on whether to use strings or symbols in certain
situations. They produce the same results, although strings take up
more memory, which I’m not sure how or why exactly.

The difference is that every string literal in your code, whether it is
the exact same sequence of bytes or not, is a separate object and thus
has its own piece of your program’s available memory. Every symbol with
the same name is the exact same object.

current_situation = “good”
puts “Everything is fine” if current_situation == “good”
puts “PANIC!” if current_situation == “bad”

The check for equality you performed here is not the same as a check for
object identity. Most of the time when you compare strings for equality
you don’t care whether they are the same object or not. You just want
to know if they have the same contents, so the == method for the String
class is defined to check for that.

“good” and “good” and “bad” (3 strings) take up memory. Why is that?
Isn’t the second “good” same as the first?

No, the second “good” not quite the same as this code will demonstrate:

string = “good”
puts “The id of #{string.inspect} is #{string.object_id}”
string = “good”
puts “The id of #{string.inspect} is #{string.object_id}”

puts

symbol = :good
puts “The id of #{symbol.inspect} is #{symbol.object_id}”
symbol = :good
puts “The id of #{symbol.inspect} is #{symbol.object_id}”

When you run this code with your Ruby of choice, you’ll see something
like the following:

The id of “good” is 3390460
The id of “good” is 3390340

The id of :good is 247688
The id of :good is 247688

The numbers will almost certainly be different for you, but take note
that the id of the string “good” changes while that of :good does not.
This means that even though the string literal “good” appears to be
identical as far as the code you write is concerned, the interpreter
sees each instance as an individual.

Now, the actual problem is that every individual object gets a place in
the memory of your program, so you’re wasting memory and possibly
processing time in the garbage collector if you have string literals in
your program that you really do want to treat as the exact same
object. Whether that matters to you is up to you and your needs as the
author.

When do you normally use a symbol instead of a string?

I would suggest that, broadly, you should avoid string literals unless
the data they would contain might be modified, leave your program and
enter another one, or be displayed directly to the user. Otherwise,
you’re probably better off using symbols in order to save work for the
garbage collector. Keep in mind, however, that symbols are never
garbage collected, so creating many of them dynamically (perhaps by
converting user input into symbols, for instance) would be detrimental
and probably a good use case for strings instead.

In any case, the difference in memory consumption and garbage collector
performance is probably negligible for most small scripts and
short-lived processes, so don’t get too hung up on these things. Just
keep the possibility of the difference being meaningful in mind if you
ever have a need to optimize a script, program, or library.

-Jeremy