I just finished going over the Programming Ruby book (pragmatic programmers). I pretty much got everything except Symbols. I'm not too sure what they are and what the use of them is.
Could somebody please help fill me in?
| SitePoint Sponsor |
I just finished going over the Programming Ruby book (pragmatic programmers). I pretty much got everything except Symbols. I'm not too sure what they are and what the use of them is.
Could somebody please help fill me in?
Ohai!




Symbols are like strings, but symbols are important to the program, and strings are user data.
Example:
Say you want to code program that generates HTML for a website. You execute it via the command line. You want to be able to configure the color scheme of your website. So you set a variable in the configuration:
then you have this in your generation:Code:color = "red"
But in Ruby, you would use symbols instead:Code:if color == "red" # create a red website elseif color == "green" # create a green website ... ...
Because the data is important for the behavior of the program.Code:color = :red if color == :red # create a red website elseif color == :green # create a green website ... ...
Another example: if you want to create an application which manages clients of a company. The company wants to know how to contact them, so they ask the client's email address, postal code and telephone number. The company wants to know what is the preferred way to contact their clients. You can use this data structure:
There are two uses of symbols here: for the hash's keys, and for the contact_method. You can use a symbol here because there is a limited set of contact methods.Code:person1 = { :name => 'Chris Johnsson', :telephone => '9235792739', :postal_code => '23423', :email => 'chris@johnsson.com', :contact_method => :email }
If you wanted to print the contact information, you could use this code:
This would print the email adress: chris@johnsson.com. But if you set the contact_method to : postal_code, this would print 23423.Code:puts person1[person1[:contact_method]]
I hope this is clear.
Thanks, that cleared things up for me. I have another question not TOO related but I didn't feel like starting a new thread. Basically, I don't understand the following syntax:
Are we defining methods? Calling methods? I don't see what's going on syntax wise.class Product < ActiveRecord::Base
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
validates_uniqueness_of :title
end
Ohai!


You're calling methods. You could write the same thing this way:
Code:validates_presence_of(:title, :description, :image_url)
Studio Rockstar's Blog - A journey to quitting the dayjob.





Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?



The reason that symbols are better for programming data is because they use less memory than do strings. So using :red instead of "red" is better for the computer/server the application is running on![]()
And for the class you don't understand, yes, you're calling methods, but it's important to note that they are CLASS METHODS not instance methods. So deining a class method must be prefixed with "self" or the class name or under a derivitive of "self" like so:
Code:class Bla class << self def bla; end end end
Bookmarks