From: Robert Citek Date: 2007-12-20T09:11:46+09:00 Subject: Re: slicing in ruby Found it: indices, which is deprecated in favor of values_at. $ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print $F.values_at(1,2,3,8,2,2).join(" -- ") ' 1 2 3 4 5 6 7 8 9 10 2 -- 3 -- 4 -- 9 -- 3 -- 3 Discovered this with a brute-force search: ruby -e 'puts [].methods.sort' | while read name ; do echo == $name ri Array."${name}" | cat done Interestingly, a lot of methods didn't return anything via ri. For example: $ ri Array.methods Nothing known about Array.methods Anyone have any suggestions on a "cleaner" way to discover methods and their docs? Regards, - Robert On Dec 19, 2007 5:58 PM, Robert Citek wrote: > > Is there a way to slice an array like in perl? > > For example, what's the ruby equivalent to doing this: > > $ echo $(seq 1 10) | tee /dev/stderr | perl -lane 'print join(" -- ", > @F[1,2,3,8,2,2]) ' > 1 2 3 4 5 6 7 8 9 10 > 2 -- 3 -- 4 -- 9 -- 3 -- 3 > > I can get this to work: > > $ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print $F.join(" -- ") ' > 1 2 3 4 5 6 7 8 9 10 > 1 -- 2 -- 3 -- 4 -- 5 -- 6 -- 7 -- 8 -- 9 -- 10 > > and this: > > $ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print $F[1,2].join(" -- ") ' > 1 2 3 4 5 6 7 8 9 10 > 2 -- 3 > > but not this: > > $ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print > $F[1,2,3,8,2,2].join(" -- ") ' > 1 2 3 4 5 6 7 8 9 10 > -e:1:in `[]': wrong number of arguments (6 for 2) (ArgumentError) > from -e:1 > > > There's probably some method I need to invoke, but which one? > > Regards, > - Robert > >