Peter Peter - 1 year ago 89
Ruby Question

Building multi-line strings, programatically, in Ruby

Here's something I often do when programming:

code = ''
code << "next line of code #{something}" << "\n"
code << "another line #{some_included_expression}" << "\n"

Is there some better way than having
<< "\n"
+ "\n"
on every line?
This seems quite inefficient.

I'm interested in Ruby solutions, in particular. I'm thinking something like

code = string.multiline do
"next line of code #{something}"
"another line #{some_included_expression}"

Jim Jim
Answer Source

This would be one way:

code = []
code << "next line of code #{something}"
code << "another line #{some_included_expression}"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download