Reputation: 137
I heard that two symbols with the same name create only one memory area, but two strings with the same content create two memory areas.
Upvotes: 2
Views: 1021
Reputation: 1691
I find it most helpful to think of symbols as strings without all the fancy string features. Their main use is as the name of other things in your program. For example, say you have a hash of user information. You could do
user = { 'name' => 'Tim', 'age' => 20 }
Here the hash keys are strings. This is fine, but it's really overkill. Strings have lots of fancy methods like substitution and smart capitalization, and they are mutable (i.e. string objects can be changed in place). You are never going to change hash keys, so you don't need any of that. Thus, it's better just to use symbols
user = { :name => 'Tim', :age => 20 }
Another place symbols show up is referring to class methods. Say I have the following class
class Foo
def initialize(bar)
@bar = bar
end
end
If I need access to the value of @bar
from outside of Foo
I need to add a getter method to Foo
.
That would look like this
class Foo
def initialize(bar)
@bar = bar
end
def bar
bar
end
end
However, this is a pretty common thing to want to do, so ruby provides shorthand as follows
class Foo
attribute_reader :bar
def initialize(bar)
@bar = bar
end
end
This tells Foo
to create for us the method we added by hand in the previous version.
As you can see, we refer to the @bar
variable using a symbol.
In answer to your second question, no, a symbol is not a variable. You can't "assign it a value." However, as you can see, symbols can sometimes be used to refer to variables.
Upvotes: 3
Reputation: 27747
Ok, so the misunderstanding probably stems from this:
A symbol is not a variable, it is a value. like 9
is a value that is a number.
A symbol is a value that is kinda of roughly a string... it's just not a string that you can change... and because you can't change it, we can use a shortcut -> all symbols with the same name/value are stored in the same memory-spot (to save space).
You store the symbol into a variable, or use the value somewhere - eg as the key of a hash.... this last is probably one of the most common uses of a symbol.
you make a hash that contains key-value pairs eg:
thing_attrs = {:name => "My thing", :colour => "blue", :size => 6}
thing_attrs[:colour] # 'blue'
In this has - the symbols are the keys you can use any object as a key, but symbols are good to use as they use english words, and are thus easy to understand what you're storing/fetching... much better than, say numbers. Imagine you had:
thing_attrs = {0 => "My thing", 1 => "blue", 2 => 6}
thing_attrs[1] # => "blue"
It would be annoying and hard to remember that attribute 1
is the colour... it's much nicer to give names that you can read when you're reading the code. Thus we have two options: a string, or a symbol.
There would be very little difference between the two. A string is definitely usable eg:
thing_attrs = {"name" => "My thing", "colour" => "blue", "size" => 6}
thing_attrs["colour"] # 'blue'
except that as we know... symbols use less memory. Not a lot less, but enough less that in a large program, over time, you will notice it. So it has become a ruby-standard to use symbols instead.
Upvotes: 4
Reputation: 1440
A Symbol is the most basic Ruby object you can create. It's just a name and an internal ID. Symbols are useful because a given symbol name refers to the same object throughout a Ruby program. Symbols are more efficient than strings. Two strings with the same contents are two different objects, but for any given name there is only one Symbol object. This can save both time and memory.
# p039symbol.rb
# use the object_id method of class Object
# it returns an integer identifier for an object
puts "string".object_id
puts "string".object_id
puts :symbol.object_id
puts :symbol.object_id
>ruby p039symbol.rb
21066960
21066930
132178
132178
>Exit code: 0
Upvotes: 3