Undefined local var hash not trapped

R

RichardOnRails

The following program:

# K:\Analysis
# TestUndefinedLocalVar_hash.rb

a = []
a << "xyz"
a << hash
a << undefined

crashes on line 8 (a << undefined) rather than on line 7 (which
apparently appends a reference to the undefined "hash" to the array.
What causes this mythical creation of "hash"? Is this behavior
defined anywhere?

If it matters, the following explains how I encountered this problem:

I have a program which had been using a local var named "hash". Use
of that name caused no problem.
However, I decided I should change the value assigned to hash, so I
changed all references to it to "hash_string" ... all but one. I
expected that after I made changes to the way hash_string was
computed, my program would fail when the reference to the now-
undefined hash was encountered. No such luck.

Thanks in Advance,
Richard
 
H

Hassan Schroeder

What causes this mythical creation of "hash"? =A0Is this behavior
defined anywhere?

Uh, "mythical"?? Ruby does have docs, you know :)

Hint: Object#hash would be a good place to look...

HTH,
--=20
Hassan Schroeder ------------------------ (e-mail address removed)
twitter: @hassan
 
R

RichardOnRails

The following program:

# K:\Analysis
# TestUndefinedLocalVar_hash.rb

a = []
a << "xyz"
a << hash
a << undefined

crashes on line 8 (a << undefined) rather than on line 7 (which
apparently appends a reference to the undefined "hash" to the array.
What causes this mythical creation of "hash"?  Is this behavior
defined anywhere?

If it matters,  the following explains how I encountered this problem:

I have a program which had been using a local var named "hash".  Use
of that name caused no problem.
However, I decided I should change the value assigned to hash,  so I
changed all references to it to "hash_string" ... all but one.  I
expected that after I made changes to the way hash_string was
computed,  my program would fail when the reference to the now-
undefined hash was encountered.  No such luck.

Thanks in Advance,
Richard
That certainly helps in that it shows me what Ruby was "thinking".

RubyDocs shows that lots of class have hash methods (maybe because
they all inherit from Object.) I listed a few "uses" below but have
adopted a new programming rule: don't use Ruby method-names as local
variables.

Below are the few examples that seem pretty useless to me.

Best wishes,
Richard

a = []
# a = "" # Can't append hash to a string
a << hash
a << "xyz"
 
D

Dhruva Sagar

[Note: parts of this message were removed to make it a legal post.]

Object#Hash was a good indication.
in irb session, the IRB object #hash is executed when you type hash.

regarding strings, again, strings have a method << which is used for
appending. a<< hash will return in an error since hash returns a number and
it is very large to be converted to a char...

On Sun, Jan 2, 2011 at 08:40, RichardOnRails <
The following program:

# K:\Analysis
# TestUndefinedLocalVar_hash.rb

a = []
a << "xyz"
a << hash
a << undefined

crashes on line 8 (a << undefined) rather than on line 7 (which
apparently appends a reference to the undefined "hash" to the array.
What causes this mythical creation of "hash"? Is this behavior
defined anywhere?

If it matters, the following explains how I encountered this problem:

I have a program which had been using a local var named "hash". Use
of that name caused no problem.
However, I decided I should change the value assigned to hash, so I
changed all references to it to "hash_string" ... all but one. I
expected that after I made changes to the way hash_string was
computed, my program would fail when the reference to the now-
undefined hash was encountered. No such luck.

Thanks in Advance,
Richard
That certainly helps in that it shows me what Ruby was "thinking".

RubyDocs shows that lots of class have hash methods (maybe because
they all inherit from Object.) I listed a few "uses" below but have
adopted a new programming rule: don't use Ruby method-names as local
variables.

Below are the few examples that seem pretty useless to me.

Best wishes,
Richard

a = []
# a = "" # Can't append hash to a string
a << hash
a << "xyz"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top