Simon Kröger
3/22/2006 9:55:00 PM
Welcome Diego,
first let me say it may not be the wisest thing to introduce oneself to
a language group by suggesting to change that language. (this may
explain the relative low volume of replies you got)
> -Is there a way to have multidimentional arrays in Ruby? Ie something
> like variable[3][2]? One could use arrays of arrays but it does not
> look quite as clear when reading the code.
This comes up ever so often, and i don't get it. I think arrays of
arrays is the more flexible and general case. Perhaps this is enough to
make you happy?
class MultiArray
def MultiArray.new *args
Array.new(args.shift) {args.empty? ? nil : MultiArray.new(*args)}
end
end
usage:
m = MultiArray.new(2, 3, 4)
m[1][2][3] = 'O'
p m
#=>[[[nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, nil]],
[[nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, "O"]]]
> -Fortran 95 had a really nice feature when using arrays: you could
> assign values using ranges. I am glad to see something similar exist in
> Ruby too, (eg. variable[0..100]=0)
> However F95 went a bit further, since the ranges could include an
> optional parameter, the step. In F95 it would look something like:
> variable[min:max:step] = 0
> There's a lot of cases when something like this is really convinient. I
> know you can produce a similar effect using a few more lines, but I
> think a feature like a range+step would have several advantages, namely
> that it would look more clear in less code (a common feature in Ruby)
> and it's closer to the way we think to be very desirable. Has there
> been any talk about such a feature in future Ruby?
Not that i know of, and to be honest I'm having a hard time imagining a
case where i would like to use it. But nevertheless i think one of the
greatest thinks about Ruby is that you can create nearly everything by
yourself, so creating a ComplexRange class and enabling Array to handle
it correctly should be possible. (ok, you loose some of the syntactic
sugar, granted)
> -What is the difference between the << and + concatenation for strings?
> Is it just that the former modifies the string while the second one
> returns a new string? Why then not use something like +! (just as an
> alternative)?
Never saw that before and have no desire to see it again, ever.
(sorry i don't want to be rude, but i would suggest to learn and get
used to a language that evolved for some years now before making such
suggestions)
> -I read the discussion about the use of the "end" keyword, and possible
> alternatives. Instead of trying to change that, I think it would be
> [...]
> In other words it would look just like for iterators (more consistent
> Ruby) and at the same time make happy those that want brackets instead
> of keywords to delimit if/while/unless: two birds with one stone.
Of course this would break existing code also, perhaps more subtle but
thats even more dangerous than a syntax error. Blocks are full closures
and - for example - introduce a new scope.
> Thank you very much. Anyway Ruby keeps being a great language and I
> really love it (though I have not use it much yet).
Again, you are very welcome! Try to ask concrete questions on real
problems and I'm quite sure you will get friendly and very helpful
answers which make your head spin and let your jaw drop when you see
whats possible even without changing the language.
(it happened to me, at least :) )
> Diego Virasoro
cheers
Simon