Change 'nil' using literal notation?

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

= X will set value if it’s nil to X.

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@googlegroups.com.

To post to this group, send email to rubyonrails-talk@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

ahh the Conditional Assignment Operator, thanks :slight_smile:

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 advance

As 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!")

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?

In ruby, any variable whose name starts with a capital letter becomes a constant.

Which implies there are actually constants in Ruby.

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”

I didn't know that, thank you!

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>

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.)

Sorry, classes should be constants.

Jordon Bedwell wrote in post #1102618: