More elegant way to write a multi-dimensional array of characters in ruby?
I am trying to make a multi-dimensional array of characters in ruby, and this works, but is there a more elegant way?
def initialize(text)
@map = Array.new
i = 0
text.split("开发者_如何学Go\n").each do |x|
@map[i] = x.scan(/./)
i += 1
end
#@map = text
end#constructor
@map = text.split("\n").map{|x| x.scan(/./)}
#looks slightly better, needs at least 1.8.7
@map = text.lines.map{|x| x.scan(/./)}
@map = text.lines.to_a.map { |s| s.chomp.split("") }
精彩评论