Hi,
I am trying to iterate through an array, convert the output to a string
and puts that string.
CODE:
@hosts = [“test1”, “test2”, “test3”, “test4”
puts @hosts.each { |x| print x + “\n” }.to_s
CURRENT OUTPUT:
[“test1”, “test2”, “test3”, “test4”]
DESIRED OUTPUT:
However, I want this output to go into an email message, so I’m want to
lose the Square brackets and quotes, so it looks like this (and is a
string):
test1
test2
test3
Any ideas?
Thanks.
Dwight
[“test1”, “test2”, “test3”, “test4”]
Any ideas?
Hi there,
In this case I think a simple join would be appropriate:
irb(main):001:0> @hosts = [“test1”, “test2”, “test3”, “test4”]
=> [“test1”, “test2”, “test3”, “test4”]
irb(main):002:0> puts @hosts.join("\n")
test1
test2
test3
test4
Regards,
Chris W.
http://www.twitter.com/cwgem
hi,
u have output @hosts’ elements in the block? why would you add a puts in
front of it?
u can do it just with Array.join
puts hosts.join("\n")
On Tue, Sep 13, 2011 at 10:33 PM, dwight schrute [email protected]
wrote:
CURRENT OUTPUT:
test2
–
Best Regards,
Larry Lv
@ Baidu NLP
On Tue, Sep 13, 2011 at 4:40 PM, Larry Lv [email protected] wrote:
hi,
u have output @hosts’ elements in the block? why would you add a puts in
front of it?
u can do it just with Array.join
puts hosts.join("\n")
You don’t even need the join.
irb(main):001:0> @hosts = [“test1”, “test2”, “test3”, “test4”]
=> [“test1”, “test2”, “test3”, “test4”]
irb(main):002:0> puts @hosts
test1
test2
test3
test4
=> nil
irb(main):003:0>
Cheers
robert
irb(main):003:0>
Cheers
robert
But, he wants it to be a string.
print @hosts.map{|x| x + “\n”}.join
Harry
Thank you Harry! That is exactly what I needed 
Dwight
Except, I need to put the output into a string. When I use the to_s
method, I end up with square brackets and quotes:
irb(main):001:0> @hosts = [“test1”, “test2”, “test3”, “test4”]
=> [“test1”, “test2”, “test3”, “test4”]
irb(main):002:0> puts @hosts
test1
test2
test3
test4
=> nil
irb(main):003:0> puts @hosts.to_s
[“test1”, “test2”, “test3”, “test4”]
=> nil
On Tue, Sep 13, 2011 at 3:33 PM, dwight schrute [email protected]
wrote:
puts @hosts.each { |x| print x + “\n” }.to_s
Just for completeness since nobody has quite covered it yet,
@hosts.each { … } returns the receiver, which in this case is
@hosts, and then you call .to_s on the result. So, closer to the
argument actually passed to puts, you essentially have @hosts.to_s
which is “[“test1”, “test2”, “test3”, “test4”]”, which, when
puts’d, is [“test1”, “test2”, “test3”, “test4”]. However, in your
block you also print, so the real, full output should have been:
test1
test2
test3
test4
[“test1”, “test2”, “test3”, “test4”]
On Tue, Sep 13, 2011 at 4:51 PM, Robert K.
[email protected]wrote:
You don’t even need the join.
I think this is just an artefact of “puts” and not relevant for the
initial
problem.
If the original posters wants to have it in an e-mail, he will probably
will
want to interpolate in a template. Using simply the @hosts as is
suggested
above will then yield this:
$ ruby <<EOF
@hosts = [“test1”, “test2”, “test3”, “test4”]
puts “#{@hosts}”
EOF
test1test2test3test4
I think the OP probably needed to use ‘map’ instead of ‘each’.
[email protected]:~/a$ ruby <<EOF
@hosts = [“test1”, “test2”, “test3”, “test4”]
hosts_string = @hosts.join("\n")
puts hosts_string
EOF
test1
test2
test3
test4
HTH,
Peter