Hi Everyone, I *kind of* get the idea of *symbols*. So I need help to understand them more fully. --- config.yml config: some_setting: ''setting_one'' another_setting: - ''element1'' - ''element2'' So how do I ingest this: yaml = YAML.load(''config.yml'') Are these true or false? yaml[''config''] == yaml[:config] yaml[''config''][''another_setting''] == yaml[:config][:another_setting] How else do you use *symbols*? Thank you. P -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/67eed5d3-96ce-4259-a48c-e8c818a5221c%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
One reason to use Symbols is that they are immutable. When you''re passing one around as an argument or Hash key, it won''t change. Another is that multiple instances of a Symbol are the same object, making a smaller memory footprint than Strings. A string is not a symbol. Some structures will use to_s or to_sym to allow you to pass either as an argument, but that''s not their default behaviour.> {''a''=>1,:a=>2}=> {"a"=>1, :a=>2}> {''a''=>1,''a''=>2}=> {"a"=>2} -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/34b316ac9098fe1cc8a7e07ed2413388%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
On Friday, July 12, 2013 12:49:11 AM UTC-7, Ruby-Forum.com User wrote:> > One reason to use Symbols is that they are immutable. When you''re > passing one around as an argument or Hash key, it won''t change. > Another is that multiple instances of a Symbol are the same object, > making a smaller memory footprint than Strings. > > A string is not a symbol. Some structures will use to_s or to_sym to > allow you to pass either as an argument, but that''s not their default > behaviour. > > > {''a''=>1,:a=>2} > => {"a"=>1, :a=>2} > > > {''a''=>1,''a''=>2} > => {"a"=>2} >I know about this behavior, but this doesn''t answer the question I posted. I tested my question above. I can create a hash in ruby and explicitly give it symbols for keys and it will work. But when I retrieve a hash from a yml file, I cannot reference the keys as symbols. Are these true or false?> yaml[''config''] == yaml[:config] > yaml[''config''][''another_setting''] == yaml[:config][:another_setting] >The answer to both is false. So that said, where/how else can symbols be used outside of being hash keys? Thanks. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8e38cdaa-b190-4790-b1d7-7504a9838dae%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
> Are these true or false? >> yaml[''config''] == yaml[:config] >> yaml[''config''][''another_setting''] == yaml[:config][:another_setting] >> > > The answer to both is false. >yaml[''config''] shows me the values from the yml file. yaml[:config] doesn''t. How else do you use symbols? I have read all the definitions. I understand the explanations, but the rest of where I am confused can now only be answered by example usages. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3005b21d-6756-4b65-b822-8b8893b93e28%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Symbols are used all the time in Ruby/Rails when sending methods. For example, look at the following method call: instance.do_stuff(:foo => :bar, :answer => 3) What you are actually sending to the do_stuff method is a hash that looks like the following: { :foo => :bar, :answer => 3 } For these purposes, they are basically human-readable identifiers. The advantage they have over strings in this case is that they are re-used, so if you use :foo in your application 100 times, it''s only in memory once. Whereas if you use "foo" 100 times, it''s in memory 100 times. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fba6676d-9f04-4b7e-abc5-e76ce0e90595%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
On 2013-Jul-12, at 14:53 , Peter wrote:> > Are these true or false? > yaml[''config''] == yaml[:config] > yaml[''config''][''another_setting''] == yaml[:config][:another_setting] > > The answer to both is false. > > yaml[''config''] shows me the values from the yml file. yaml[:config] doesn''t. > > How else do you use symbols? I have read all the definitions. I understand the explanations, but the rest of where I am confused can now only be answered by example usages. >As it pertains to YAML, String keys and Symbol keys are stored differently: irb2.0.0> require ''yaml'' #2.0.0 => true irb2.0.0> puts({:symbol => "I am a Symbol", ''string'' => "I am a String"}.to_yaml) --- :symbol: I am a Symbol string: I am a String #2.0.0 => nil Note that there is actually a : (colon) in front of the :symbol and not in front of the string. In both cases there is a : after the key and before the value. (You might also note that the default value is a string and quoting is often implied.) Here are more types so you can see how they get formatted as YAML: irb2.0.0> puts({:symbol => "I am a Symbol", ''string'' => "I am a String", 42 => "I am the Answer", "41" => "I am not a number", "NumberFive" => 5, "String5" => "5", '' quote me '' => " I need quotes to keep my leading/trailing spaces"}.to_yaml) --- :symbol: I am a Symbol string: I am a String 42: I am the Answer ''41'': I am not a number NumberFive: 5 String5: ''5'' '' quote me '': '' I need quotes to keep my leading/trailing spaces'' #2.0.0 => nil -Rob -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2B060F3F-74B2-42E9-89F5-7B68C13B86B6%40agileconsultingllc.com. For more options, visit https://groups.google.com/groups/opt_out.
Thanks guys, that has been very informative. Although, each example has been symbols used in a hash array. Any other uses? Perhaps outside of a "hash"? Thanks. On Friday, July 12, 2013 12:54:03 PM UTC-7, Rob Biedenharn wrote:> > > On 2013-Jul-12, at 14:53 , Peter wrote: > > > Are these true or false? >>> yaml[''config''] == yaml[:config] >>> yaml[''config''][''another_setting''] == yaml[:config][:another_setting] >>> >> >> The answer to both is false. >> > > yaml[''config''] shows me the values from the yml file. yaml[:config] > doesn''t. > > How else do you use symbols? I have read all the definitions. I understand > the explanations, but the rest of where I am confused can now only be > answered by example usages. > > > As it pertains to YAML, String keys and Symbol keys are stored differently: > > irb2.0.0> require ''yaml'' > #2.0.0 => true > irb2.0.0> puts({:symbol => "I am a Symbol", ''string'' => "I am a > String"}.to_yaml) > --- > :symbol: I am a Symbol > string: I am a String > #2.0.0 => nil > > Note that there is actually a : (colon) in front of the :symbol and not in > front of the string. In both cases there is a : after the key and before > the value. (You might also note that the default value is a string and > quoting is often implied.) > > Here are more types so you can see how they get formatted as YAML: > > irb2.0.0> puts({:symbol => "I am a Symbol", ''string'' => "I am a String", > 42 => "I am the Answer", "41" => "I am not a number", "NumberFive" => 5, > "String5" => "5", '' quote me '' => " I need quotes to keep my > leading/trailing spaces"}.to_yaml) > --- > :symbol: I am a Symbol > string: I am a String > 42: I am the Answer > ''41'': I am not a number > NumberFive: 5 > String5: ''5'' > '' quote me '': '' I need quotes to keep my leading/trailing spaces'' > #2.0.0 => nil > > > -Rob > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b1cfe445-df6b-4a4e-9fdc-c8e6056ffdba%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.