4ee8e20a519f3afdcd03bcf24681dbd8

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

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 !

1e8f141e7857d397d8020ed3b759e88a

Maciej Piechotka

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

No rating. Login to rate!

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]
1e8f141e7857d397d8020ed3b759e88a

Maciej Piechotka

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

No rating. Login to rate!

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]
A8d3f35baafdaea851914b17dae9e1fc

Adam

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

1 rating. Login to rate!
1
2
3
4
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

1
2
3
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

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]
F549e720298ea80b55006a1f58695d6b

Vsevolod

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

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

Your refactoring





Format Copy from initial code

or Cancel