From: William James Date: 2009-02-19T17:39:50+09:00 Subject: Re: literal syntax for array of arrays of strings Joel VanderWerf wrote: > > We have this: > > a = %w{ a b c d e f } > p a # ==> ["a", "b", "c", "d", "e", "f"] > > Has anyone ever felt the need for something like > > a = %t{ > a b c > d e f > } # ==> [["a", "b", "c"], ["d", "e", "f"]] > > It's probably too special (this is the first time in 8 years I've > wanted it), and it is easy to implement it as a method rather than a > literal: > > def table s > s.split("\n").map!{|t|t.split} > end > > t = table %q{\ > a b c > d e f > } > > p t # ==> [["a", "b", "c"], ["d", "e", "f"]] > > Really just an idle question... class Array def slices n a = [] each_slice(n){|s| a << s} a end end ==>nil %w[a b c d e f].slices 3 ==>[["a", "b", "c"], ["d", "e", "f"]]