Saturday, July 31, 2010

Ruby string array converted into integer array


["1","2","3","4"].map(&:to_i) => [1, 2, 3, 4]
    OR
["1","2","3","4"].collect(&:to_i) => [1, 2, 3, 4]

Dynamic html tag loop creation

arr = [1,2,3,4]
str = ''
i = 0
arr.each do |a|
        if i % 3 == 0
                str<<"#{a}"
        else
                str<<"#{a}"
        end
        i = i+1
        if i % 3 == 0 || arr.length == i
             str <<"
"
        end
end

puts str.inspect

output is like :

  • 1

    2

    3

  • 4