additional methods for Array

D

David Garamond

I'm wondering what other people's thoughts are about adding these
methods. They are found in PHP but not in Ruby at the moment. I'm not
submitting an RCR because I don't feel strongly about these myself, but
since Ruby's class library are usually "battery-included", there might
be interest to include the methods below as well. Each of these can be
implemented with a few lines of Ruby.

Array#shuffle -> nil
shuffle an array. perhaps add an argument to specify "how much" do
we want to shuffle?

Array#pick(n=1) -> anArray (method name?)
pick one or more random elements from array.

Array#pick(n=1) { |x,y,z...| } (syntax? method name?)
feed block with random element(s) from the array.

Perhaps it would be nice too if there is a random pick method with
weighting, but I cannot propose the decent syntax for it.

Array#chunk(n) -> anArray
split an array into chunks, each containing at most n elements.

Any other "useful" methods people want for Array?
 
K

KONTRA Gergely

Array#chunk(n) -> anArray
split an array into chunks, each containing at most n elements.

Maybe Array#%() can be a synonim (% - the modulo operator)

How about enabling the user to define custom operators? Like in sml and
prolog? (The charset of the operators is given, and one can define
operators like <<< -+- whatever...)

What do you think?

Gergo
 
G

Gavin Sinclair

I would suggest that you may want to implement these in pure Ruby and add
them to Gavin Sinclair's Standard Library Extensions library available on
RubyForge. If they become popular enough, then people can note that in
feedback and that would weigh heavily on their utility and need.

I've highlighted the relevant messages as a reminder for me to add
(some of) these.

Cheers,
Gavin
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top