Slicing in ruby

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

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 <robert.citek@gmail.com> 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

Robert Citek wrote:

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?

The first two things that come to mind are: www.ruby-doc.org, if you want online doc, or one of the many fine books about Ruby if you want dead-tree doc.

···

--
RMagick: http://rmagick.rubyforge.org/

C:\Documents and Settings\gavin.kistner>qri methods
------------------------------------------------------ Multiple
choices:

     Kernel#methods, Object#methods

C:\Documents and Settings\gavin.kistner>qri Object.methods

···

On Dec 19, 5:11 pm, Robert Citek <robert.ci...@gmail.com> wrote:

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

---------------------------------------------------------
Object#methods
     obj.methods => array
------------------------------------------------------------------------
     Returns a list of the names of methods publicly accessible in
     _obj_. This will include all the methods accessible in _obj_'s
     ancestors.

        class Klass
          def kMethod()
          end
        end
        k = Klass.new
        k.methods[0..9] #=> ["kMethod", "freeze", "nil?", "is_a?",
                                "class", "instance_variable_set",
                                 "methods", "extend", "<em>send</em>",
"instance_eval"]
        k.methods.length #=> 42

But yes - documentation helps you find out what methods are available.
:slight_smile:

http://phrogz.net/ProgrammingRuby/builtins.html#builtinclassesandmethods
http://phrogz.net/ProgrammingRuby/lib_standard.html#standardlibrary

On Dec 20, 2007 8:11 AM, Robert Citek

Anyone have any suggestions on a "cleaner" way to discover methods and
their docs?

try
   qri values -Oa -F .fastri-fulltext | less +/values

kind regards -botp

Why not

$ ruby -e 'a=(1..10).to_a; $stderr.puts(a.join(" ")); puts
a.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

?

Kind regards

robert

:wink:

···

2007/12/20, Robert Citek <robert.citek@gmail.com>:

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

--
use.inject do |as, often| as.you_can - without end

Anyone have any suggestions on a "cleaner" way to discover methods and
their docs?

Check out http://www.noobkit.com/ something which i think ruby should
have had in the beginning :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.

First, thank you for the example. Made me wonder about the .to_a
method and discover that 1..10 works differently in ruby than in perl:

$ perl -le 'print 1..10'
12345678910

$ ruby -le 'print 1..10'
1..10

To answer your question: because 'echo $(seq 1 10)' represents the
abstract "any delimited input data" model, which could be multi-line,
thus the -lane options. Also, because I tend to build up my
quick-and-dirty scripts from the command line: first head the file,
then filter/modify the data stream, repeat quickly. So the example
above is merely one in a series of commands:

$ seq 1 10
$ echo $(seq 1 10)
$ echo $(seq 1 10) | tee /dev/stderr
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print $F'
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print $F.join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print $F(1,2).join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
$F[1,2,3].join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
$F.values_at(1,2).join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
$F.values_at(1,2,3).join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
$F.values_at(1..5).join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
$F.values_at(1,2,3,8,2,2).join(" -- ") ' # posted example
$ head /etc/passwd | tee /dev/stderr | ruby -F: -lane 'print
$F.values_at(1,2,3,8,2,2).join(" -- ") '
$ head /etc/passwd | tee /dev/stderr | ruby -F: -lane 'print
$F.values_at(0,2..5).join(" -- ") '
$ head /etc/passwd | tee /dev/stderr | ruby -F: -lane 'print
$F.values_at(0,2..5).join(" -- ") if $F[2] >= 1000 '
$ head /etc/passwd | tee /dev/stderr | ruby -F: -lane 'print
$F.values_at(0,2..5).join(" -- ") if $F[2].to_i >= 1000 '
$ cat /etc/passwd | tee /dev/stderr | ruby -F: -lane 'print
$F.values_at(0,2..5).join(" -- ") if $F[2].to_i >= 1000 '

Regards,
- Robert

···

On Dec 20, 2007 2:40 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

2007/12/20, Robert Citek <robert.citek@gmail.com>:
> 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

Why not

$ ruby -e 'a=(1..10).to_a; $stderr.puts(a.join(" ")); puts
a.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

?

I hadn't run across that one before. Very nice presentation and I
like the ability to add comments (although I haven't encountered any
as yet).

On the other hand, I find the navigation wanting. It would be really
cool if this could work like railsbrain/rubybrain in this regard:

http://www.railsbrain.com/api/edge/doc/index.html

···

On 12/20/07, Marc Heiler <shevegen@linuxmail.org> wrote:

> Anyone have any suggestions on a "cleaner" way to discover methods and
> their docs?

Check out http://www.noobkit.com/ something which i think ruby should
have had in the beginning :slight_smile:

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/