A more elegant way to write a multidimensional array of characters in ruby?

I'm trying to make a multidimensional character array in ruby ​​and this works, but is there a more elegant way?

def initialize(text)
    @map = Array.new
    i = 0
    text.split("\n").each do |x|
     @map[i] = x.scan(/./)
     i += 1
    end
    #@map = text
  end#constructor

      

+2


a source to share


2 answers


@map = text.split("\n").map{|x| x.scan(/./)}

#looks slightly better, needs at least 1.8.7
@map = text.lines.map{|x| x.scan(/./)} 

      



+7


a source


@map = text.lines.to_a.map { |s| s.chomp.split("") }

      



+3


a source







All Articles