From: Ross Bamford Date: 2006-03-23T00:49:35+09:00 Subject: Re: questions/suggestions from a Ruby newbie On Wed, 2006-03-22 at 23:38 +0900, Diego.Virasoro@gmail.com wrote: > -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? > I don't think this is doable with things as they are, since Ruby's support for range indices differs from Fortran's (IIRC - I think var[0:10:2] = 0 would set 0,2,4,6,8,10 ?). In Ruby, when providing a range to Array#[]= it serves to select a continuous range of elements that are to be replaced with a single element (the RHS): a = [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j] # => [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j] a[0..3] = 0 # => 0 a # => [0, :e, :f, :g, :h, :i, :j] > -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)? I agree with David and Robert; when I first came over to Ruby I saw plenty of stuff that could be 'made better', but now I'm used to it I wouldn't change much at all. << is pretty consistently used throughout Ruby and makes lots of stuff easy thanks to duck typing (string? array? file? something else? who cares... :)) > Thank you very much. Anyway Ruby keeps being a great language and I > really love it (though I have not use it much yet). Give it a couple of months and you'll be using it for everything from shuffling your music to documenting C code :) -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk