The code below works fine. But it seem clunky.
How would you combine the following code into one line?
data = Array.new
@results.each{ |result| data.push(result.get_output) }
data.join("\n")
Ruby 1.9.3
The code below works fine. But it seem clunky.
How would you combine the following code into one line?
data = Array.new
@results.each{ |result| data.push(result.get_output) }
data.join("\n")
Ruby 1.9.3
Map the original collection instead of creating a new array first:
@results.map(&:get_output).join("\n")
I haven’t checked this, but try something like:
data = @results.map{ |result| result.get_output }.join("\n")
data never contained the joined array, just the array and had join called on it. But that may just be me being pedantic.
Jun 25 2012 at 1:15
def and end in the question.
data = @results.collect(&:get_output).join("\")
collect (or map) to create an arrayIf you get empty lines, add compact before join. This helps clean up the results.