# Slicing in ruby

**URL:** <https://www.ruby-forum.com/t/slicing-in-ruby/123721>\
**Category:** Ruby\
**Created:** [December 20, 2007, 12:59am UTC](https://www.ruby-forum.com/t/slicing-in-ruby/123721 "2007-12-20T00:59:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cluettr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7c8e57/32.png) [@cluettr](https://www.ruby-forum.com/u/cluettr)\
**Post date:** [December 20, 2007, 12:59am UTC](https://www.ruby-forum.com/t/slicing-in-ruby/123721/1 "2007-12-20T00:59:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cluettr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7c8e57/32.png) [@cluettr](https://www.ruby-forum.com/u/cluettr)\
**Post date:** [December 20, 2007, 1:12am UTC](https://www.ruby-forum.com/t/slicing-in-ruby/123721/2 "2007-12-20T01:12:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cluettr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7c8e57/32.png) [@cluettr](https://www.ruby-forum.com/u/cluettr)\
**Post date:** [December 20, 2007, 1:28am UTC](https://www.ruby-forum.com/t/slicing-in-ruby/123721/3 "2007-12-20T01:28:42Z")

</div>

Robert C. wrote:

> while read name ; do  
> their docs?

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

---

<div class="post-metadata">

**Author:** ![cluettr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7c8e57/32.png) [@cluettr](https://www.ruby-forum.com/u/cluettr)\
**Post date:** [December 20, 2007, 4:23am UTC](https://www.ruby-forum.com/t/slicing-in-ruby/123721/4 "2007-12-20T04:23:32Z")

</div>

On Dec 20, 2007 8:11 AM, Robert C.

> 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

---

<div class="post-metadata">

**Author:** ![cluettr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7c8e57/32.png) [@cluettr](https://www.ruby-forum.com/u/cluettr)\
**Post date:** [December 20, 2007, 1:40am UTC](https://www.ruby-forum.com/t/slicing-in-ruby/123721/5 "2007-12-20T01:40:58Z")

</div>

On Dec 19, 5:11 pm, Robert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> $ ri Array.methods  
> Nothing known about Array.methods

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

```
 Kernel#methods, Object#methods

```

## C:\Documents and Settings\gavin.kistner\>qri Object.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.  
🙂

[http://phrogz.net/ProgrammingRuby/builtins.html#builtinclassesandmethods](http://phrogz.net/ProgrammingRuby/builtins.html#builtinclassesandmethods)  
[http://phrogz.net/ProgrammingRuby/lib\_standard.html#standardlibrary](http://phrogz.net/ProgrammingRuby/lib_standard.html#standardlibrary)

> **[Ruby-Doc.org: Documenting the Ruby Language](https://ruby-doc.org/)**
>
> Fast, searchable Ruby documentation for core and standard libraries. Plus, links to tutorials, guides, books, and related sites.

---

<div class="post-metadata">

**Author:** ![cluettr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7c8e57/32.png) [@cluettr](https://www.ruby-forum.com/u/cluettr)\
**Post date:** [December 20, 2007, 9:41am UTC](https://www.ruby-forum.com/t/slicing-in-ruby/123721/6 "2007-12-20T09:41:06Z")

</div>

2007/12/20, Robert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

?

Kind regards

robert

😉

---

<div class="post-metadata">

**Author:** ![cluettr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7c8e57/32.png) [@cluettr](https://www.ruby-forum.com/u/cluettr)\
**Post date:** [December 20, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/slicing-in-ruby/123721/7 "2007-12-20T16:29:16Z")

</div>

On Dec 20, 2007 2:40 AM, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> $ 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
> 
> ?

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

---

<div class="post-metadata">

**Author:** ![cluettr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7c8e57/32.png) [@cluettr](https://www.ruby-forum.com/u/cluettr)\
**Post date:** [December 20, 2007, 7:37am UTC](https://www.ruby-forum.com/t/slicing-in-ruby/123721/8 "2007-12-20T07:37:57Z")

</div>

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

Check out [http://www.noobkit.com/](http://www.noobkit.com/) something which i think ruby should  
have had in the beginning 🙂

---

<div class="post-metadata">

**Author:** ![cluettr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7c8e57/32.png) [@cluettr](https://www.ruby-forum.com/u/cluettr)\
**Post date:** [December 20, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/slicing-in-ruby/123721/9 "2007-12-20T17:56:40Z")

</div>

On 12/20/07, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Anyone have any suggestions on a “cleaner” way to discover methods and  
> > their docs?
> 
> Check out [http://www.noobkit.com/](http://www.noobkit.com/) something which i think ruby should  
> have had in the beginning 🙂

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](http://www.railsbrain.com/api/edge/doc/index.html)

> **[rubybrain.com is For Sale](https://giganames.com/name/rubybrain.com)**
>
> Buy rubybrain.com. Brandable domain names for sale. Find your perfect brand

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
