1 2 3
artists = [1, 2, 3] column_1 = artists[0..artists.size/2] column_2 = artists[artists.size/2+1..artists.size]
Refactorings
No refactoring yet !
Maciej Piechotka
August 14, 2008, August 14, 2008 13:41, permalink
Something like that (a bit different rounding):
1 2 3
artists = [1, 2, 3] column_1 = artists[0, artists.size/2] column_2 = artists[artists.size/2...artists.size]
Maciej Piechotka
August 14, 2008, August 14, 2008 13:45, permalink
The same rounding:
1 2 3
artists = [1, 2, 3] column_1 = artists[0...(artists.size/2.0).ceil] column_2 = artists[(artists.size/2.0).ceil...artists.size]
Adam
August 14, 2008, August 14, 2008 14:03, permalink
1 2 3 4
require "enumerator" artists = [ 1, 2, 3 ] column_1, column_2 = artists.enum_slice((artists.size / 2.0).round).to_a
gudata
August 15, 2008, August 15, 2008 11:33, permalink
thanks for the (artists.size / 2.0).round array and the enumeration idea
1 2 3
artists = [1, 2, 3] column_1 = artists[0, (artists.size/2.0).round] column_2 = artists - column_1
gudata
August 15, 2008, August 15, 2008 12:11, permalink
works for any size of the array
1 2 3 4 5 6
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]
The example works if the array has more than 2 elements - this is defect not a feature :)