title: How to loop elements in 2d array to construct an string in "Ruby functional Style"
body: <p>Ruby use the functions from "functional concept" heavily, such as <strong>map, each</strong>.   They really depend on a  <strong>self-contained function</strong> which is so called <strong>block</strong> in Ruby.</p>
  
  <p>It is very common to loop though a 2d array, make an string about the elements.</p>
  
  <p>In java, it may looks like</p>
  
  <pre><code>   public String toString(){
          String output = "[";
          for (int i =0; i&lt;array.length; i++) {
              output+= "Row "+(i+1)+" : ";
              for (int j=0; j&lt;array[0].length;j++ ) {
                  output += array[i][j]+", ";
              }
              output += "\n";
          }
  
          return output += "]";
      }
  </code></pre>
  
  <p>I tried to rewrite such a thing in "Ruby functional Style", but I think there are still some improvements. Eg. I want to remove the mutable variable <strong>output</strong> </p>
  
  <pre><code>  def to_s
          output = "[\n"
          @data.each_with_index do |row,i|
              output &lt;&lt; "Row #{i+1} : "
              row.each { |num| output &lt;&lt; "#{num}," }
              output &lt;&lt; "\n"
          end
  
          output+"]"
      end
  </code></pre>