How could I implement this? I think my decision is very dirty, and I would like to make it better. I think there is an easy way in Ruby to do this, but I can't remember. I want to use it with Rails, so if Rails provides something like this, that is fine too. use should be as follows:
fruits = ['banana', 'strawberry', 'kiwi', 'orange', 'grapefruit', 'lemon', 'melon']
odd_fruits = array_mod(fruits, :mod => 2, :offset => 0)
even_fruits = array_mod(fruits, :mod => 2, :offset => 1)
puts odd_fruits
banana
kiwi
grapefruit
melon
puts even_fruits
strawberry
orange
lemon
******* EDIT *******
for those you want to know, here is what I finally did:
in the rails project, I created a new file config/initializers/columnize.rbthat looks like this:
class Array
def columnize args = { :columns => 1, :offset => 0 }
column = []
self.each_index do |i|
column << self[i] if i % args[:columns] == args[:offset]
end
column
end
end
Rails Rails. , , , - :) "Array", - :
>> arr = [1,2,3,4,5,6,7,8]
=> [1, 2, 3, 4, 5, 6, 7, 8]
>> arr.columnize :columns => 2, :offset => 0
=> [1, 3, 5, 7]
>> arr.columnize :columns => 2, :offset => 1
=> [2, 4, 6, 8]
>> arr.columnize :columns => 3, :offset => 0
=> [1, 4, 7]
>> arr.columnize :columns => 3, :offset => 1
=> [2, 5, 8]
>> arr.columnize :columns => 3, :offset => 2
=> [3, 6]
. , , - , , nil . . , JS , , 960 Grid (http://960.gs)