SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Symbols

  1. #1
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Symbols

    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!

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    color = "red"
    then you have this in your generation:

    Code:
    if color == "red"
      # create a red website
    elseif color == "green"
      # create a green website
    ...
    ...
    But in Ruby, you would use symbols instead:

    Code:
    color = :red
    
    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.

    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:

    Code:
    person1 = {
    :name => 'Chris Johnsson',
    :telephone => '9235792739',
    :postal_code => '23423',
    :email => 'chris@johnsson.com',
    :contact_method => :email
    }
    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.

    If you wanted to print the contact information, you could use this code:

    Code:
    puts person1[person1[:contact_method]]
    This would print the email adress: chris@johnsson.com. But if you set the contact_method to : postal_code, this would print 23423.

    I hope this is clear.

  3. #3
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    class Product < ActiveRecord::Base
    validates_presence_of :title, :description, :image_url
    validates_numericality_of :price
    validates_uniqueness_of :title
    end
    Are we defining methods? Calling methods? I don't see what's going on syntax wise.
    Ohai!

  4. #4
    SitePoint Addict Brak's Avatar
    Join Date
    Jul 2004
    Location
    Central Coast, CA
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •