From: sanjeev mathur Date: 2011-02-16T13:04:04+09:00 Subject: Re: Why use a symbol in place of a variable? --0015175cd11c4f87c9049c5e5e02 Content-Type: text/plain; charset=ISO-8859-1 This is quite good tutorial about symbols and strings here http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ On Wed, Feb 16, 2011 at 6:38 AM, John Feminella wrote: > A symbol and a variable are two different things. A symbol is > essentially a special kind of literal, just like a number or a string > is. You cannot assign values to symbols, just like you can't assign > values to numbers or strings -- they are their own values. That is, it > makes no sense to say `42 = "banana"`, just as it makes no sense to > say `:banana = 42`. > > In this case, the author is using the symbol :largecave to represent a > particular location. The reason why he might prefer a symbol literal > to a string literal is that symbols are immutable. "Immutable" means > that you can't do operations on symbols to change them (unlike, say, > strings). Immutability is a good property because it decreases that > number of surprises you can have, and because it makes reasoning about > your program easier. > > ~ jf > -- > John Feminella > Principal Consultant, BitsBuilder > LI: http://www.linkedin.com/in/johnxf > SO: http://stackoverflow.com/users/75170/ > > > > On Tue, Feb 15, 2011 at 19:41, Gaba Luschi > wrote: > > Hi, > > Say you have a method in a class that's defined as this: > > (working from Peter Cooper's Beginning Ruby book, p. 155 of the book) > > > > class Dungeon > > . > > . > > . > > > > def start(location) > > @player.location = location > > show_current_description > > end > > > > why is it that when you place the player in the large cave, it's > > my_dungeon.start(:largecave) > > > > instead of > > my_dungeon.start(largecave) > > ? > > why is largecave a symbol? > > Thanks so much! > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > > > --0015175cd11c4f87c9049c5e5e02--