From: John Feminella Date: 2011-07-25T19:44:03+09:00 Subject: Re: Please give simple example of when to use symbols > When do you normally use a symbol instead of a string? If the exact sequence of characters is what's important, use a String. If the identity is what's important, use a Symbol. Here are some examples: 1.) A person's name should be a String, because the sequence of characters is important. names = "John Paul Jones".split puts "first name is: #{names.first}" 2.) A status flag should be a Symbol, because the identity is what's important. The actual name is immaterial (although of course you want to pick a readable name that makes sense). missile.fire! puts "jammed!" if missile.status == :launch_failure 3.) When referring to other program constructs, you should almost always use Symbols. obj.send(:some_method) 4.) The same rules apply when deciding whether to use Symbol or String keys for a Hash. For instance, you'd probably have: phonetic_pronunciations = {"pneumonia" => "new moan ya", ...} but you'd also have: colors = {:red => [255, 0, 0], :green => [0, 255, 0], :yellow => [255, 255, 0], ...} ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Mon, Jul 25, 2011 at 03:20, Kaye Ng wrote: > I'm still a bit confused on whether to use strings or symbols in certain > situations.  They produce the same results, although strings take up > more memory, which I'm not sure how or why exactly. > > current_situation = "good" > puts "Everything is fine" if current_situation == "good" > puts "PANIC!" if current_situation == "bad" > > "good" and "good" and "bad" (3 strings) take up memory.  Why is that? > Isn't the second "good"  same as the first? > > When do you normally use a symbol instead of a string? > > Thanks guys! > > -- > Posted via http://www.ruby-forum.com/. > >