4ee8e20a519f3afdcd03bcf24681dbd8

The example works if the array has more than 2 elements - this is defect not a feature :)

artists = [1, 2, 3]
    column_1 = artists[0..artists.size/2]
    column_2 = artists[artists.size/2+1..artists.size]

Refactorings

No refactoring yet !

1e8f141e7857d397d8020ed3b759e88a

Maciej Piechotka

August 14, 2008, August 14, 2008 13:41, permalink

No rating. Login to rate!

Something like that (a bit different rounding):

artists = [1, 2, 3]
column_1 = artists[0, artists.size/2]
column_2 = artists[artists.size/2...artists.size]
1e8f141e7857d397d8020ed3b759e88a

Maciej Piechotka

August 14, 2008, August 14, 2008 13:45, permalink

No rating. Login to rate!

The same rounding:

artists = [1, 2, 3]
column_1 = artists[0...(artists.size/2.0).ceil]
column_2 = artists[(artists.size/2.0).ceil...artists.size]
A8d3f35baafdaea851914b17dae9e1fc

Adam

August 14, 2008, August 14, 2008 14:03, permalink

1 rating. Login to rate!
require "enumerator"

artists = [ 1, 2, 3 ]
column_1, column_2 = artists.enum_slice((artists.size / 2.0).round).to_a
4ee8e20a519f3afdcd03bcf24681dbd8

gudata

August 15, 2008, August 15, 2008 11:33, permalink

No rating. Login to rate!

thanks for the (artists.size / 2.0).round array and the enumeration idea

artists = [1, 2, 3]
column_1 = artists[0, (artists.size/2.0).round]
column_2 =  artists - column_1
4ee8e20a519f3afdcd03bcf24681dbd8

gudata

August 15, 2008, August 15, 2008 12:11, permalink

1 rating. Login to rate!

works for any size of the array

irb(main):001:0> column_1 = [1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
irb(main):003:0> column_2 = column_1.slice!((column_1.size/2.0).round..column_1.size)
=> [4, 5, 6]
irb(main):004:0> column_1
=> [1, 2, 3]
F549e720298ea80b55006a1f58695d6b

Vsevolod

August 20, 2008, August 20, 2008 20:08, permalink

No rating. Login to rate!
artists = [1, 2, 3]
column_2 = (column_1 = artists.dup).slice!(0, artists.size/2)
D41d8cd98f00b204e9800998ecf8427e

gfarfl

December 8, 2008, December 08, 2008 11:07, permalink

No rating. Login to rate!
artists = [1, 2, 3, 4, 5, 6]
column1, column2 = artists.partition {|artist| artist <= artists.size }
Fc514d5e5d2e13a7c223b48bb5c2248a

Britto

September 29, 2010, September 29, 2010 20:06, permalink

No rating. Login to rate!

using Array.drop

list = [1, 2, 3, 4, 5, 6, 7]
half = list.size/2
part_1, part_2 = list[0, half], list.drop(half)
Acad2552784135c09b17c00853f5a6f8

dcadenas.blogspot.com

October 2, 2010, October 02, 2010 15:07, permalink

No rating. Login to rate!

If you are going to use this everywhere on many arrays on different places, it makes sense to extend Array:

class Array
  def split_in_two(first_chunk_size = self.size / 2)
   [self[0..first_chunk_size - 1], self[first_chunk_size..-1]]
  end
end

[1, 2, 3, 4, 5, 6].split_in_two 
> [[1, 2, 3], [4, 5, 6]]

[1, 2, 3, 4, 5, 6, 7].split_in_two 
> [[1, 2, 3], [4, 5, 6, 7]]

[1, 2, 3, 4, 5, 6, 7].split_in_two(5)
> [[1, 2, 3, 4, 5], [6, 7]]

Your refactoring





Format Copy from initial code

or Cancel