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
···
--
Posted via http://www.ruby-forum.com/ .
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?
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 White
http://www.twitter.com/cwgem
Larry_Lv
(Larry Lv)
13 September 2011 14:40
3
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 <spambocks@yahoo.ca> wrote:
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
--
Posted via http://www.ruby-forum.com/\ .
--
Best Regards,
Larry Lv
@ Baidu NLP
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
···
--
Posted via http://www.ruby-forum.com/ .
Thank you Harry! That is exactly what I needed
Dwight
···
--
Posted via http://www.ruby-forum.com/ .
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 3:33 PM, dwight schrute <spambocks@yahoo.ca> wrote:
puts @hosts.each { |x| print x + "\n" }.to_s
Robert_K1
(Robert K.)
13 September 2011 14:51
7
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
···
On Tue, Sep 13, 2011 at 4:40 PM, Larry Lv <larrylv1990@gmail.com> 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")*
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Harry3
(Harry)
13 September 2011 15:06
8
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
But, he wants it to be a string.
print @hosts.map {|x| x + "\n"}.join
Harry
> 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")m*
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>
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'.
pev@pev-desktop:~/a$ ruby <<EOF
@hosts = ["test1", "test2", "test3", "test4"]
hosts_string = @hosts.join ("\n")
puts hosts_string
EOF
test1
test2
test3
test4
HTH,
Peter
···
On Tue, Sep 13, 2011 at 4:51 PM, Robert Klemme <shortcutter@googlemail.com>wrote:
On Tue, Sep 13, 2011 at 4:40 PM, Larry Lv <larrylv1990@gmail.com> wrote:
Cheers
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
--
Peter Vandenabeele
gsm: +32-478-27.40.69
e-mail: peter@vandenabeele.com
http://twitter.com/peter_v