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.

2013/3/20 Todd H. [email protected]

Todd H. 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!”)

ahh the Conditional Assignment Operator, thanks :slight_smile:

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?

On Wed, Mar 20, 2013 at 7:53 PM, Dheeraj K.
[email protected] wrote:

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

Which implies there are actually constants in Ruby.

I didn’t know that, thank you!

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


Dheeraj K.

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

On Wed, Mar 20, 2013 at 8:11 PM, Dheeraj K.
[email protected] wrote:

(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>

On Thu, Mar 21, 2013 at 8:52 AM, Robert W. [email protected]
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.)

Dheeraj K. 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.

Jordon B. wrote in post #1102618:

On Thu, Mar 21, 2013 at 8:52 AM, Robert W. [email protected]
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?

On Thu, Mar 21, 2013 at 8:57 AM, Jordon B. [email protected]
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.