2008/3/12, Todd Benson <caduceass / gmail.com>:
> On Wed, Mar 12, 2008 at 12:34 AM, Ma Fe <frytaz / gmail.com> wrote:
>  > well I dont know where in array i have .txt files but its sure that
>  >  there is just one and i want to put it at first place of array...
>
> Try using the #partition method (my last example) then.  It gives you
>  all .txt files at the beginning of the array.  I made a mistake
>  though.  There should only be one dot, not two for that (same thing if
>  you use #select)...
>
>  a = ['one.jpg', 'two.txt', 'three.pdf', 'four.txt']
>  a_new = a.partition {|i| i =~ /\.txt$/}
>  p a_new

For the fun of it: here's a solution with #inject:

irb(main):009:0> a = ['one.jpg', 'two.txt', 'three.pdf']
=> ["one.jpg", "two.txt", "three.pdf"]
irb(main):010:0> a.inject([[],[]]) {|(tx,ot),x| (/\.txt$/ =~ x ? tx :
ot) << x;[tx,ot]}.flatten
=> ["two.txt", "one.jpg", "three.pdf"]

There's even a version with *two* #injects:

irb(main):011:0> a.inject([[],[]]) {|(tx,ot),x| (/\.txt$/ =~ x ? tx :
ot) << x;[tx,ot]}.inject {|a,b| a.concat b}
=> ["two.txt", "one.jpg", "three.pdf"]

It's been a while so I *had* to do it. Once in a while I need my
#injection.  :-))

Kind regards

robert

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