Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's a Symbol? - Lightning Talk

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

What's a Symbol? - Lightning Talk

Avatar for Ellen Musick

Ellen Musick

July 17, 2013

More Decks by Ellen Musick

Other Decks in Programming

Transcript

  1. A Symbol is a Name • "A Symbol is a

    constant integer with human readable name." (Wei Lie Sho) • “A symbol is the most basic Ruby object you can create. It's just a name and an internal ID.” (The Ruby Cookbook) • “Symbols are immutable, interned strings.” (The Ruby Programming Language) • “Simply, a symbol is something that you use to represent names and strings.” (Kevin Clark) • “A Ruby symbol is the internal representation of a name . . . Other languages call this process 'interning,' and call symbols 'atoms.'“ (1st edition of Pickax) • If you have a background in another programming language this link might be helpful: http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-s ymbol
  2. How and when do you use symbols? Examples: • keys

    in hashes flash[:notice] = "Project has been created." • refer to method names before_filter :find_project, :only => [:show, :edit, :update, :destroy] In General: • Ruby hacker Jim Weirich on when to use a string versus a symbol: “If the contents (the sequence of characters) of the object are important, use a string.” “If the identity of the object is important, use a symbol” • Anytime a string is used over and over. • FYI, Symbols are not garbage collected.
  3. Why use symbols? • Save on memory – compare symbols

    by identity – a symbol refers to the same object throughout the Ruby program • Bonus!
  4. Where did symbols come from? • Ruby draws inspiration from

    Lisp, Smalltalk, and Perl. • Lisp also has symbols! (different from Ruby symbols, but some similarities) • Symbols are not subclasses of strings.
  5. Sources • Ruby Programming Language by David Flanagan and Yukihiro

    Matsumoto • Ruby Cookbook by Lucas Carlson and Leonard Richardson • First edition of Pickax: http://www.ruby-doc.org/docs/ProgrammingRuby/ • “Understanding Ruby Symbols” by Kevin Clark http://glu.ttono.us/articles/2005/08/19/understanding-r uby-symbols • http://www.ruby-doc.org/docs/ProgrammingRuby/