Logan Capaldo
10/12/2006 1:48:00 AM
On Thu, Oct 12, 2006 at 12:20:10AM +0900, Jan Reitz wrote:
>
> Logan Capaldo wrote:
> > Yes, because since you can define [] for anything, ruby has no way of
> > "guessing" what kind of container you want like PHP does.
>
> i know that not only an array can have [], strings can have them too in
> php, but for ease of use someone defined that if you use [] on an
> undefinded variable it would become an array not a string, and since
> noone uses []= on NilClass atm it could be used, to do just that in
> ruby.
> i don't ask "why not?", because most ruby users are not used to it and
> wouldnt want it, but i do. i started to dislike php more and more, but
> not because i can write $ary[1] = 'hans'; but because phps oo sucks
> compared to ruby, the part with the $ary[1] = 'hans'; is better there.
>
In PHP $ary[1] = 'hans' must be an array. I can't create something else
that supports the same syntax.
In Ruby, ary[1] = 'hans' could be an array, could be a hash, could be a
string, could be any user created class. It could also mask errors,
Like for instance:
b = 7
b[3] = 2
Should 'b' now magically be turned into [nil, nil, nil, 2] ? (I don't
know, I don't think so, but maybe it should).
When asking for magic, you must always consider the impact it will have
on the rest of the language and the rest of the programs out there.
> you use the "a = []" shortcut too, its a shortcut by definition, it
> says "make an Array", not a string, or something else that supports [].
> just Array.new
>
Ahh, but [] is different from []. ;) Rather, when I say a = [] it has
nothing to do with a[i] but rather it's writing an array literal (like
PHP's array()).
[1,2,3] would be the same as array(1,2,3) in PHP
[1,2] would be the same as array(1,2) in PHP
[1] would be the same as array(1) in PHP
and [] is the same as array() in PHP.
It's just an (un?)happy coincidence that it looks the same as the index
operator.
> > defining []= for nil is probably not a good idea. What I would suggest
> > you do is create your own container that has the semantics you want, if
> > you want to go down the route of writing additional code.
>
> i just want an array, nothing more
> ary = nil
> ary[1] = 'moep'
> ary.class
> => Array
>
> OR
>
> ary = Array.new(16) { Array.new 16 }
> ary[99][99] = 'moep'
> ary[99].class
> => Array
>
> with a little typechecking on the parameter inside the brackets you
> could even:
>
> moep[:test_symbol] = "hans"
> moep.class
> => Hash
>
> moep2["string as key"] = "hans"
> moep2.class
> => Hash
>
> my problem is that if i have an multi dimensional array, and it needs
> to grow (by itself if i assign values outside of the allocated space),
> the newly created will be "nil" so i cant use my own class for that.
>
It doesn't have to be, that's why you write your own class.
> in c# 2.0 i would write
> List<List<string>> mda = new List<List<string>>()
> mda[99][99] = "test";
>
> Tada, tested it and it does _not_ work, lol.....
>
> BUT what hinders ruby to be better than that....
>
Better is a question of opinion in this case.
> the question i ask is is it doable ?
> can someone help solving ?
> im stuck with this "cant reassign self" or whatever it said.
>