2014-01-20 19 views
0

我有哈希值的數組看起來像這樣:追加「 n」來,除了最後的所有元素在Ruby中數組

texts = [{:text => 'my text', :width => 123}, {:text => 'my other text', :width => 200}] 

我想有一個最後文本應該是這樣的:

my_final_text = 'my text\nmy other text' 

我也試着這樣做:

def concat_breakline(texts) 
    final_text = "" 
    texts.each do |text| 
    final_text << text[:text] + "\n" 
    end 
end 

但是,這將增加「\ n」來最後一個元素,我想避免這種情況。我怎麼能解決這個問題?

回答

8

這是很容易:

texts.collect { |text| text[:text] }.join("\n") 

join方法添加的東西在中間,但不是終點。

相關問題