On May 14, 2006, at 6:17 PM, corey konrad wrote:
i dont know doing info = Array.new seems like an array declaration to
me. Couldn the constrution of an array object be implied in just
saying
info[] you what what i mean? That would be even more simplified.
The problem here is you are confusing declaring a variable with
assigning a vlaue to a variable.
In ruby I can type
x = 0 # assigns 0 to x
without declaring the variable x.
In C I have to type
int x; /* declares variable named x /
x = 0; / assigns 0 to it */
Acutally I could have typed
int x = 0;
but that’s merely a more concise way of writing the declaration and
the assigment.
Technically this is what happens in ruby as well, but declarations
occur as a side effect of the first (spatial, not necessarily
chronilogical) assignment.
You can see this with the following two pieces of code:
% cat example1.rb
puts x.inspect
% ruby example1.rb
example1.rb:1: undefined local variable or method `x’ for main:Object
(NameError)
You’ll note that since x doesn’t not appear in any preceding code as
the left hand side of an assignment statement, the variable x is not
declared. Likewise no method named x exists, so it raises a NameError
% cat example2.rb
if false
x = 0
end
puts x.inspect
% ruby example2.rb
nil
Now this time no exception was raised, even though x never actually
got set to zero. This is because ruby determines variable
declarations when it parses the source, it makes not of any
identifiers on the lhs of an assignment operator.
Now why can’t this work for
info[] = blah
?
because []= is a method call. Only identifier = expression is an
actual assignment. Any number of objects can have different meanings
for []=.
info[] = blah
is
info.[]=(blah)
in actuality.