[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

slicing in ruby

Robert Citek

12/19/2007 11:59:00 PM

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

8 Answers

Robert Citek

12/20/2007 12:12:00 AM

0

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

Tim Hunter

12/20/2007 12:28:00 AM

0

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.ruby...

Phrogz

12/20/2007 12:39:00 AM

0

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

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#builtinclasses...
http://phrogz.net/ProgrammingRuby/lib_standard.html#stand...
http://www.rub...

botp

12/20/2007 3:23:00 AM

0

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

Marc Heiler

12/20/2007 6:38:00 AM

0

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

Check out http://www.no... something which i think ruby should
have had in the beginning :)
--
Posted via http://www.ruby-....

Robert Klemme

12/20/2007 8:40:00 AM

0

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

?

Kind regards

robert


;-)

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

Robert Citek

12/20/2007 3:28:00 PM

0

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

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

Rick DeNatale

12/20/2007 4:56:00 PM

0

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.no... 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/...
http://www.ruby...

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...