In Ruby is there a way to combine all array elements into one string?
Example Array:
@arr = ['<p>Hello World</p>', '<p>This is a test</p>']
Example Output:
<p>Hello World</p><p>This is a test</p>
In Ruby is there a way to combine all array elements into one string?
Example Array:
@arr = ['<p>Hello World</p>', '<p>This is a test</p>']
Example Output:
<p>Hello World</p><p>This is a test</p>
Use the Array#join method (the argument to join is what to insert between the strings - in this case a space):
@arr.join(" ")
[1,2,3] => 123?join works with enumerables of anything that responds to to_s, including integers, but the result will always be a string. If you want an integer result, you can use to_i on the result.String#lines, you can sanely tied it back together using my_string.join('') (note the empty string argument).join tries #to_str first and #to_s second.While a bit more cryptic than join, you can also multiply the array by a string.
@arr * " "
$ Array.instance_methods.* ($ is shorthand for show-source)