slicing in ruby

R

Robert Citek

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
 
R

Robert Citek

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
 
T

Tim Hunter

Robert said:
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.
 
P

Phrogz

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#builtinclassesandmethods
http://phrogz.net/ProgrammingRuby/lib_standard.html#standardlibrary
http://www.ruby-doc.org/
 
B

botp

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
 
R

Robert Klemme

2007/12/20 said:
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


;-)
 
R

Rick DeNatale

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,055
Latest member
RacheleCar

Latest Threads

Top