Does anybody know can you change the output ''nil'' to a different string using literal notation? Besides constructor? Hash_Name = Hash.new("Anything other than nil!") Hash_name = { "key" => "value" }("Anything other than nil!") New at this, just wondering. Thanks in advance -- 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 For more options, visit https://groups.google.com/groups/opt_out.
||= X will set value if it''s nil to X. 2013/3/20 Todd Hartsfield <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Does anybody know can you change the output ''nil'' to a different string > using literal notation? Besides constructor? > > Hash_Name = Hash.new("Anything other than nil!") > > Hash_name = { > "key" => "value" > }("Anything other than nil!") > > New at this, just wondering. Thanks in advance > > -- > 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 > For more options, visit https://groups.google.com/groups/opt_out. > > >-- 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 For more options, visit https://groups.google.com/groups/opt_out.
ahh the Conditional Assignment Operator, thanks :) -- 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 For more options, visit https://groups.google.com/groups/opt_out.
Todd Hartsfield wrote in post #1102378:> Does anybody know can you change the output ''nil'' to a different string > using literal notation? Besides constructor? > > Hash_Name = Hash.new("Anything other than nil!") > > Hash_name = { > "key" => "value" > }("Anything other than nil!") > > New at this, just wondering. Thanks in advanceAs an aside, you do realize that naming your variable "Hash_Name" makes it a constant right? Should be: hash_name = Hash.new("Anything other than nil!") If you really intended to make that new hash a constant then the convention is: HASH_NAME = Hash.new("Anything other than nil!") -- 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 For more options, visit https://groups.google.com/groups/opt_out.
umm as long as a variable does not have special characters or a space in it, as far as I am aware I thought variables are case sensitive, but should work. The example above I see a variable needs to be ALL_CAPS or all_lowercase, you cannot HaVe_bOtH. Is that correct? -- 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 For more options, visit https://groups.google.com/groups/opt_out.
In ruby, any variable whose name starts with a capital letter becomes a constant. -- Dheeraj Kumar On Thursday 21 March 2013 at 5:48 AM, Todd Hartsfield wrote:> umm as long as a variable does not have special characters or a space in > it, as far as I am aware I thought variables are case sensitive, but > should work. The example above I see a variable needs to be ALL_CAPS or > all_lowercase, you cannot HaVe_bOtH. Is that correct? > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 For more options, visit https://groups.google.com/groups/opt_out.
On Wed, Mar 20, 2013 at 7:53 PM, Dheeraj Kumar <a.dheeraj.kumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In ruby, any variable whose name starts with a capital letter becomes a > constant.Which implies there are actually constants in Ruby. -- 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 For more options, visit https://groups.google.com/groups/opt_out.
Jordan Bedwell is correct. Ruby''s constants are /meant/ to be not changed, but you can define a constant and change its value later. Ruby will produce a warning ''already initialized constant'' Also note that your constant''s internal representation can be changed without triggering the warning. 1.9.3p374 :001 > Const = ''a'' => "a" 1.9.3p374 :002 > Const = ''b'' (irb):2: warning: already initialized constant Const => "b" 1.9.3p374 :003 > Const = {a: ''a''} (irb):3: warning: already initialized constant Const => {:a=>"a"} 1.9.3p374 :005 > Const[:a] = ''b'' => "b" -- Dheeraj Kumar On Thursday 21 March 2013 at 6:33 AM, Jordon Bedwell wrote:> On Wed, Mar 20, 2013 at 7:53 PM, Dheeraj Kumar > <a.dheeraj.kumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org (mailto:a.dheeraj.kumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org)> wrote: > > In ruby, any variable whose name starts with a capital letter becomes a > > constant. > > > > > Which implies there are actually constants in Ruby. > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 For more options, visit https://groups.google.com/groups/opt_out.
I didn''t know that, thank you! -- 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 For more options, visit https://groups.google.com/groups/opt_out.
tamouse mailing lists
2013-Mar-21 01:57 UTC
Re: Re: Change ''nil'' using literal notation?
On Wed, Mar 20, 2013 at 8:11 PM, Dheeraj Kumar <a.dheeraj.kumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Jordan Bedwell is correct. Ruby''s constants are /meant/ to be not changed, > but you can define a constant and change its value later. Ruby will produce > a warning ''already initialized constant'' > > Also note that your constant''s internal representation can be changed > without triggering the warning. > > 1.9.3p374 :001 > Const = ''a'' > => "a" > 1.9.3p374 :002 > Const = ''b'' > (irb):2: warning: already initialized constant Const > => "b" > 1.9.3p374 :003 > Const = {a: ''a''} > (irb):3: warning: already initialized constant Const > => {:a=>"a"} > 1.9.3p374 :005 > Const[:a] = ''b'' > => "b"The last is misleading. You have created a constant that points to an Hash object. However, the Hash object itself is not frozen. The values pointed to by the keys are not constant or frozen. You can add new keys to the Hash, change values, etc. But if you try to set Const to something else (any other object) it will again show the warning. irb(main):025:0> Const={a: ''a''} {:a=>"a"} irb(main):026:0> Const[:a] = ''b'' "b" irb(main):027:0> Const={A: ''A'', B: ''B''} (irb):27: warning: already initialized constant Const {:A=>"A", :B=>"B"} irb(main):029:0> Const[:z] = ''Z'' "Z" irb(main):030:0> Const {:A=>"A", :B=>"B", :z=>"Z"} irb(main):031:0> -- 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 For more options, visit https://groups.google.com/groups/opt_out.
Dheeraj Kumar wrote in post #1102510:> In ruby, any variable whose name starts with a capital letter becomes a > constant.In my previous post I was speaking more to the naming conventions used in Ruby. Variable names are lowercase_understored and constants are UPPER_CASE_UNDERSCORED. The mechanics of Ruby don''t enforce this convention, outside of presenting a warning when "constants" (any variable name starting with an uppercase letter) are changed after initialization. -- 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 For more options, visit https://groups.google.com/groups/opt_out.
On Thu, Mar 21, 2013 at 8:52 AM, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Dheeraj Kumar wrote in post #1102510: >> In ruby, any variable whose name starts with a capital letter becomes a >> constant. > > In my previous post I was speaking more to the naming conventions used > in Ruby. Variable names are lowercase_understored and constants are > UPPER_CASE_UNDERSCORED. > > The mechanics of Ruby don''t enforce this convention, outside of > presenting a warning when "constants" (any variable name starting with > an uppercase letter) are changed after initialization.Convention doesn''t equal required. The only reliably enforced "convention" is that classes must begin with a capital letter, it does not enforce CamalCase, it does not prevent Capital_Snake_Case. Ruby does not even disallow CamelConstants (even though technically classes are constants.) -- 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 For more options, visit https://groups.google.com/groups/opt_out.
On Thu, Mar 21, 2013 at 8:57 AM, Jordon Bedwell <envygeeks-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Convention doesn''t equal required. The only reliably enforced > "convention" is that classes must begin with a capital letter, it does > not enforce CamalCase, it does not prevent Capital_Snake_Case. Ruby > does not even disallow CamelConstants (even though technically classes > are constants.)Sorry, classes should be constants. -- 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 For more options, visit https://groups.google.com/groups/opt_out.
Jordon Bedwell wrote in post #1102618:> On Thu, Mar 21, 2013 at 8:52 AM, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: >> an uppercase letter) are changed after initialization. > Convention doesn''t equal required. The only reliably enforced > "convention" is that classes must begin with a capital letter, it does > not enforce CamalCase, it does not prevent Capital_Snake_Case. Ruby > does not even disallow CamelConstants (even though technically classes > are constants.)Is that exactly what I''ve said all along? By convention (not requirement) and name "should" be not "must" be? Or were you just +1 my last? -- 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 For more options, visit https://groups.google.com/groups/opt_out.