There is good explanation about symbol in the book. One thing that is still not clear to me is that symbol sounds like a reference in languages like Java or pointers in C++. Can someone please clarify if my understanding is correct? TIA --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
This is more of a Ruby-Talk question, but symbols are nothing like pointers. They are more like constants, except their type is Symbol. Some people like to think of them as immutable strings. Perhaps they are more like C++ macros, if an analogy is possible. Pointers/ references refer to other objects that actually exist. Symbols really *are* objects but their most commonly-used attribute is their name. Hope this helps. On Oct 7, 2006, at 3:53 PM, Bala Paranj wrote:> > There is good explanation about symbol in the book. One thing that > is still not clear to me is > that symbol sounds like a reference in languages like Java or > pointers in C++. Can someone please > clarify if my understanding is correct? TIA > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Oct 7, 2006, at 5:53 PM, Bala Paranj wrote:> There is good explanation about symbol in the book. One thing that > is still not clear to me is > that symbol sounds like a reference in languages like Java or > pointers in C++. Can someone please > clarify if my understanding is correct?Not really: a symbol is simply a name. When we say :fred, we mean the name fred. When we say :has_many, we mean the name has_many. They''re a bit like read-only strings, but without any string methods. They''re also very similar to integers: just like there''s only one object that represents 42, there''s only one symbol :forty_two. Whereever you type 42 in your program, you''re referencing that one integer. Similarly, whereever you type :forty_two, you''re referencing that same symbol. That''s their power: you can use them as efficient ways of naming things, safe in the knowledge that a :duck is a :duck is a :duck. Cheers Dave --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-08 02:09 UTC
Re: Ruby on Rails Symbol Question
Hi -- On Sat, 7 Oct 2006, Bala Paranj wrote:> > There is good explanation about symbol in the book. One thing that > is still not clear to me is that symbol sounds like a reference in > languages like Java or pointers in C++. Can someone please clarify > if my understanding is correct? TIANo, a symbol in Ruby isn''t like a reference or pointer; a symbol does not have any special relation to any other object. I think one source of confusion with symbols is that the word "symbol" can be used in two ways: to mean an instance of Symbol and, more informally, as a general term for an identifier or token. So here: x = :hello :hello is a Symbol object, but x is also a symbol, in the informal sense. A Symbol object -- a symbol, in that sense -- does not point to or represent anything other than itself. See Dave Thomas''s reply, especially concerning the similarity between symbols and integers. I''d add that symbols are similar to integers also in the sense that symbols, like integers, are used both by Ruby, internally, and by us as programmers. But there''s no connection between Ruby''s use and our use. For example, consider this: irb(main):020:0> s = "Hello" => "Hello" # Ruby has entered the symbol :s into the local variable # symbol table: irb(main):021:0> Symbol.all_symbols.find {|s| s.to_s == "s" } => :s # I can make use of :s myself: irb(main):022:0> sym = :s => :s I''ve got a variable called s, which means that Ruby creates a symbol :s, internally. I can discover this via Symbol.all_symbols. Now I assign the symbol :s to my variable sym. But this has nothing to do with Ruby''s use of the symbol :s. My two variables, sym and s, have no connection to each other. Also, the fact that Ruby is using :s to track something (local variables) has no connection to the fact that I''m using the symbol :s in my program. It''s just like an integer -- say, 75. Ruby may have some reason to store an internal count of 75 somethings; and I may have some reason to use 75 in my program. But there''s no connection between the two uses. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
--- dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> > No, a symbol in Ruby isn''t like a reference or pointer; a symbol does > not have any special relation to any other object.Is it correct to say that Symbol represents a unique entity in the system? (unique label?) Maybe I am beating the dead horse here. I have to read the replies several times to get this new concept into my head. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-08 11:31 UTC
Re: Ruby on Rails Symbol Question
Hi -- On Sun, 8 Oct 2006, Bala Paranj wrote:> --- dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: >> >> No, a symbol in Ruby isn''t like a reference or pointer; a symbol does >> not have any special relation to any other object. > > Is it correct to say that Symbol represents a unique entity in the system? (unique label?)Yes, a symbol is unique; that is, there''s only one :hello. The symbol :hello can be used in many different places, but there''s still only one of it. Again, it''s very similar to integers: there''s only one 100, but it can be used repeatedly. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Same here, maybe someone has a good example of why :symbols are *really* useful? -ryan On Oct 8, 2006, at 2:11 AM, Bala Paranj wrote:> > > --- dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: >> >> No, a symbol in Ruby isn''t like a reference or pointer; a symbol does >> not have any special relation to any other object. > > Is it correct to say that Symbol represents a unique entity in the > system? (unique label?) > > Maybe I am beating the dead horse here. I have to read the replies > several times to get this new > concept into my head. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 10/9/06, Ryan Marsh <marsh.ryanw-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Same here, maybe someone has a good example of why :symbols are *really* > useful? > -ryan >Firstly, there is the advantage of less memory consumption. Depending on what you do with the symbols, this may or may not be significant. Secondly - and don''t discount this - symbols look nicer in a syntax-highlighting editor. This increases readability of code, which is significant. There may be other reasons that I''m not aware of. Cheers, Max --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
> Not really: a symbol is simply a name. When we say :fred, we mean the > name fred. When we say :has_many, we mean the name has_many. > > They''re a bit like read-only strings, but without any string methods. > They''re also very similar to integers: just like there''s only one > object that represents 42, there''s only one symbol :forty_two. > Whereever you type 42 in your program, you''re referencing that one > integer. Similarly, whereever you type :forty_two, you''re referencing > that same symbol. That''s their power: you can use them as efficient > ways of naming things, safe in the knowledge that a :duck is a :duck > is a :duck.When creating links in Rails, I see code that specify controller using symbols but the application has multiple controllers. So :controller will be pointing to different controller for different links. How does :controller remain constant in this case? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 09 Oct 2006, at 16:37, Bala Paranj wrote:> When creating links in Rails, I see code that specify controller > using symbols but the application > has multiple controllers. So :controller will be pointing to > different controller for different > links. How does :controller remain constant in this case?The symbol :controller is the key in a hash in this case, and the hash is a different instance for each link. Best regards Peter De Berdt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-09 15:24 UTC
Re: Ruby on Rails Symbol Question
Hi -- On Mon, 9 Oct 2006, Bala Paranj wrote:> >> Not really: a symbol is simply a name. When we say :fred, we mean the >> name fred. When we say :has_many, we mean the name has_many. >> >> They''re a bit like read-only strings, but without any string methods. >> They''re also very similar to integers: just like there''s only one >> object that represents 42, there''s only one symbol :forty_two. >> Whereever you type 42 in your program, you''re referencing that one >> integer. Similarly, whereever you type :forty_two, you''re referencing >> that same symbol. That''s their power: you can use them as efficient >> ways of naming things, safe in the knowledge that a :duck is a :duck >> is a :duck. > > When creating links in Rails, I see code that specify controller > using symbols but the application has multiple controllers. So > :controller will be pointing to different controller for different > links. How does :controller remain constant in this case?The same way the number 100 remains constant. Here''s a hash: { 100 => "hi" } and another: { 100 => "bye" } I''m using 100 twice as a hash key. There''s no connection between the two hashes; the two uses of 100 don''t "know" about each other. Similarly, I can do this: { :x => "hi" } and { :x => "bye" } Here, I''m using the symbol :x twice as a hash key. That''s what Rails does with symbols like :controller. The symbol :controller can serve any number of times as a hash key in different hashes. The use of a hash key in one has does not bind the key to the value, except in the context of that hash. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
"The use of a hash key in one has does not bind the key to the value, except in the context of that has" ..so why is this an advantage? why is this good? why go the extra mile and learn about symbols? to gain...what? ? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
wannaknow wrote the following on 09.10.2006 19:03 :> "The use of a hash key in one has does not bind the key to the > value, except in the context of that has" > > ..so why is this an advantage? why is this good? why go the extra mile > and learn about symbols? to gain...what? >1/ A string is a String object which is far more costlier than a Symbol object. You gain on memory usage and CPU time used for instanciation. 2/ A symbol doesn''t look like a string -> it can convey a different semantic content. For example, I use symbols instead of strings everywhere there''s an API defined (everywhere I need to pass a hash of parameters, I use symbols as keys, symbols can be used to represent possible object state values too). Everywhere you don''t need to treat a String object like a string (printing it, calling concat, gsub, ...) you can probably assume that using a symbol is more adequate (note that there''s a place for constants in there too, but it''s a different subject). Having the IDE colorize symbols differently from strings when you follow these conventions presents cleaner code to the reader. Lionel. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
I did not notice that each hash has its own unique key and they are different hashes. Thanks for the explanation. Now it is clear.> That''s what Rails does with symbols like :controller. The symbol > :controller can serve any number of times as a hash key in different > hashes. The use of a hash key in one has does not bind the key to the > value, except in the context of that hash. > > > David--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Bala Paranj wrote:> I did not notice that each hash has its own unique key and they are > different hashes. Thanks for > the explanation. Now it is clear.cool thread. i enjoyed it throughly. thanks all for the help. (not that i truly understand why i am using it, aside from an intuative point of view) [[ which i guess is good enough ]] 10x all. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---