Ruby/Symbols

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application

[edit] Ruby Symbols


Symbols in Ruby are very much like strings, except a symbol takes only one memory address, no matter how many times you use it(no matter how many instances of it you have in your program). Each instance of a string takes a different memory address.

The first advantage of using symbols has that we can draw from the things mentioned above, is that using symbols as often as possible instead of strings helps us save memory space.Symbols are used especially when defining Hashes (discussed in a further chaper)

Symbols are global read-only objects, you cannot alter the value of a symbol as you can do with strings.

You access a symbol using the ":[nameofsymbol]" syntax such as ":birthday".Ruby would look for the object associated with ":birthday" and it would return the exact same value every time an instance of the symbol is used.

A symbol can be easily converted to a string or integer by:

:testsymb.to_s
:testsymb.to_i

where "testsymb" represents the name of the symbol in cause.

You can also convert a string directly to a symbol like this:

x = "abracadabra"
y = :magicword
y == x.to_sym


A symbol is an instance of the Symbol class.A symbol has also been referenced as "an object that has a name". It can be anything you want, not necessarily made up from just one word.

For example:

mysymbol1=:"this is good coffee"
mysymbol2=:"thanks"
mysymbol3=:"^ l33t ^"

...all of the above can be considered valid symbols.

The most common use of symbols in Ruby is for defining attributes on a class such as this for example:

class Programming
  attr_accessor :language
 
  def initialize(language)
  @language = language
  end
 
end
favorite = Programming.new("Ruby")

There isn't much to say about symbols anyway so we will stop here for the moment.On with the next chapter...

Previous Next
Personal tools