# Each value in an array dump to new line?

**URL:** <https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363>\
**Category:** ruby-talk\
**Created:** [25 August 2013 15:59 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363 "2013-08-25T15:59:32Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [25 August 2013 15:59 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/1 "2013-08-25T15:59:32Z")

</div>

So I am trying to generate a list of my numbers in a rage and export  
that to a text file, wherein each number is written on it's own line. I  
have no problem generating the array and writing it to file, I just  
can't figure out how to make each value appear on it's own line.

Ex. I want this:  
1  
2  
3  
4

Ex. My output is this  
1, 2, 3, 4

this is the section of my code that I am having problems with

while start\_number \< end\_number do  
&nbsp;&nbsp;numberlist.push(start\_number)  
&nbsp;&nbsp;start\_number = start\_number +1  
&nbsp;&nbsp;end

File.open(city\_name, 'w' ) do |w|  
&nbsp;&nbsp;w.write(numberlist)  
end

I have been searching online for a day and a half and I keep coming  
across json and yaml. My understanding is that these gems provide an  
intermediary standardized format that is acceptable by other languages,  
and everything I am reading is about importing from .json or writing to  
.json.

How could I just write to a .txt file in the format I have described?

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Hassan\_Schroeder](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/hassan_schroeder/32/1838_2.png) [@Hassan\_Schroeder](https://rubytalk.org/u/Hassan_Schroeder)\
**Post date:** [25 August 2013 16:39 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/2 "2013-08-25T16:39:44Z")

</div>

> So I am trying to generate a list of my numbers in a rage

That way leads to the dark side. Take a moment to meditate and let go of  
the anger. 🙂

> Ex. I want this:  
> 1  
> 2  
> 3  
> 4
> 
> &nbsp;&nbsp;w.write(numberlist)

If numberlist is an array (as it appears), then

&nbsp;&nbsp;w.write( numberlist.join("\n") )

The Array class has lots of handy methods; reading the doc might  
be very useful.

HTH,

> **···**
>
> On Sun, Aug 25, 2013 at 8:59 AM, Ben G. \<lists@ruby-forum.com\> wrote:  
> --  
> Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
> 
> > **[Hassan Schroeder on about.me](https://about.me/hassanschroeder)**
> >
> > I use about.me to show people what matters most to me.
> 
> twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 August 2013 17:49 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/3 "2013-08-25T17:49:51Z")

</div>

> So I am trying to generate a list of my numbers in a rage and export  
> that to a text file, wherein each number is written on it's own line. I  
> have no problem generating the array

But why generate an Array first? You don't need that.

> and writing it to file, I just  
> can't figure out how to make each value appear on it's own line.
> 
> Ex. I want this:  
> 1  
> 2  
> 3  
> 4
> 
> Ex. My output is this  
> 1, 2, 3, 4
> 
> this is the section of my code that I am having problems with
> 
> while start\_number \< end\_number do  
> &nbsp;&nbsp;numberlist.push(start\_number)  
> &nbsp;&nbsp;start\_number = start\_number +1  
> &nbsp;&nbsp;end
> 
> File.open(city\_name, 'w' ) do |w|  
> &nbsp;&nbsp;w.write(numberlist)  
> end

The most simple solution if numberlist is an Array is probably this:

File.open(city\_name, 'w') do |w|  
&nbsp;&nbsp;w.puts numberlist  
end

> I have been searching online for a day and a half and I keep coming  
> across json and yaml. My understanding is that these gems provide an  
> intermediary standardized format that is acceptable by other languages,  
> and everything I am reading is about importing from .json or writing to  
> .json.
> 
> How could I just write to a .txt file in the format I have described?

Solutions without generating the Array:

File.open(city\_name, 'w') |w|  
&nbsp;&nbsp;for i in start\_number...end\_number  
&nbsp;&nbsp;&nbsp;&nbsp;w.puts i  
&nbsp;&nbsp;end  
end

File.open(city\_name, 'w') |w|  
&nbsp;&nbsp;(start\_number...end\_number).each do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;w.puts i  
&nbsp;&nbsp;end  
end

Kind regards

robert

> **···**
>
> On Sun, Aug 25, 2013 at 5:59 PM, Ben G. \<lists@ruby-forum.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [25 August 2013 23:21 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/4 "2013-08-25T23:21:41Z")

</div>

OH that's so beautifully simple!, I tried adding "\n" to when adding to  
the array earlier but it didn't solve my problem. Nice spot on my rage  
range. I'll try to keep it under control when coding in the future!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple1](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Tamara\_Temple1](https://rubytalk.org/u/Tamara_Temple1)\
**Post date:** [26 August 2013 06:37 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/5 "2013-08-26T06:37:36Z")

</div>

If you hadn't said that, I would have. So I'll follow with: "Impressive. Most impressive."

> **···**
>
> On Aug 25, 2013, at 11:39 AM, Hassan Schroeder \<hassan.schroeder@gmail.com\> wrote:
> 
> > On Sun, Aug 25, 2013 at 8:59 AM, Ben G. \<lists@ruby-forum.com\> wrote:  
> > So I am trying to generate a list of my numbers in a rage
> > 
> > That way leads to the dark side. Take a moment to meditate and let go of the anger. 🙂

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 August 2013 09:00 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/6 "2013-08-26T09:00:28Z")

</div>

> No.

then why would the output come out as?

02  
21  
02  
22  
02  
23  
02  
24

unless they were separate elements?

When i switch from puts to print or write

it becomes "02\n20", "02\n21", "02\n22", "02\n23", "02\n24"

and I can't figure out where the \n is coming from.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 August 2013 09:31 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/7 "2013-08-26T09:31:26Z")

</div>

It's probably coming from your input. Try replacing "gets.to\_s" with  
"gets.chomp"

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 August 2013 13:02 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/8 "2013-08-26T13:02:32Z")

</div>

gets.chomp fixed everything.

This is the first time I have used gets.to\_i or gets.to\_s usually I do  
use gets.chomp as that is what codeaccademy taught me. It's not  
optimally fast but it works. Now I can duplicate and try and make it  
faster!

You have all been very helpful especially Robert! Thanks Guys!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple1](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Tamara\_Temple1](https://rubytalk.org/u/Tamara_Temple1)\
**Post date:** [26 August 2013 20:26 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/9 "2013-08-26T20:26:43Z")

</div>

If you hadn't said that, I would have. So I'll follow with: "Impressive. Most impressive."

> **···**
>
> On Aug 25, 2013, at 11:39 AM, Hassan Schroeder \<hassan.schroeder@gmail.com\> wrote:
> 
> > On Sun, Aug 25, 2013 at 8:59 AM, Ben G. \<lists@ruby-forum.com\> wrote:  
> > So I am trying to generate a list of my numbers in a rage
> > 
> > That way leads to the dark side. Take a moment to meditate and let go of the anger. 🙂

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 August 2013 06:37 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/10 "2013-08-26T06:37:36Z")

</div>

> File.open(city\_name, 'w') |w|  
> &nbsp;&nbsp;for i in start\_number...end\_number  
> &nbsp;&nbsp;&nbsp;&nbsp;w.puts i  
> &nbsp;&nbsp;end  
> end
> 
> File.open(city\_name, 'w') |w|  
> &nbsp;&nbsp;(start\_number...end\_number).each do |i|  
> &nbsp;&nbsp;&nbsp;&nbsp;w.puts i  
> &nbsp;&nbsp;end  
> end

Robert, clever idea, I hadn't thought about it that way but I believe  
you forgot "do" before |w| 🙂

The reason I am using an array is that sometimes I will need to generate  
a secondary file, based off of the same numbers. In the second file I  
will need to iterate over the array and concatenate additional numbers  
onto the front of each value within the array.

Sometimes that number begins with a 0, so I collect start\_code with

start\_code = gets.to\_s

then as the number list consists of integers I also have to convert that  
so I can concatenate the the values together. So I wrote this but this  
only generates an identical file with the altered file name. It seems as  
if start\_code isn't being concatenated? I have tried several variations  
and different combinations in and out of block form.

&nbsp;&nbsp;File.open(city\_name + start\_code, 'w' ) do |w|  
&nbsp;&nbsp;w.write((numberlist.each { |p|start\_code \<\< p.to\_s }).join("\n") )

end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 August 2013 10:53 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/11 "2013-08-26T10:53:03Z")

</div>

Good point! Another reason why working with integers internally would be  
preferable.

Cheers

robert

> **···**
>
> On Mon, Aug 26, 2013 at 11:31 AM, Joel Pearson \<lists@ruby-forum.com\> wrote:
> 
> > It's probably coming from your input. Try replacing "gets.to\_s" with  
> > "gets.chomp"
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 August 2013 06:45 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/12 "2013-08-26T06:45:56Z")

</div>

Ben G. wrote in post #1119568:

> > File.open(city\_name, 'w') |w|  
> > &nbsp;&nbsp;for i in start\_number...end\_number  
> > &nbsp;&nbsp;&nbsp;&nbsp;w.puts i  
> > &nbsp;&nbsp;end  
> > end
> > 
> > File.open(city\_name, 'w') |w|  
> > &nbsp;&nbsp;(start\_number...end\_number).each do |i|  
> > &nbsp;&nbsp;&nbsp;&nbsp;w.puts i  
> > &nbsp;&nbsp;end  
> > end
> 
> Robert, clever idea, I hadn't thought about it that way but I believe  
> you forgot "do" before |w| 🙂

Right! Well spotted.

> The reason I am using an array is that sometimes I will need to generate  
> a secondary file, based off of the same numbers. In the second file I  
> will need to iterate over the array and concatenate additional numbers  
> onto the front of each value within the array.

What do you mean by "concatenate additional numbers onto the front of  
each value within the array"? Do you mean prepend to the string  
representation?

> Sometimes that number begins with a 0, so I collect start\_code with
> 
> start\_code = gets.to\_s

".to\_s" is superfluous. I'd rather do

start\_code = Integer(gets)

> then as the number list consists of integers I also have to convert that  
> so I can concatenate the the values together. So I wrote this but this  
> only generates an identical file with the altered file name. It seems as  
> if start\_code isn't being concatenated? I have tried several variations  
> and different combinations in and out of block form.
> 
> &nbsp;&nbsp;File.open(city\_name + start\_code, 'w' ) do |w|  
> &nbsp;&nbsp;w.write((numberlist.each { |p|start\_code \<\< p.to\_s }).join("\n") )

You are modifying start\_code all the time here (if it is a String; if it  
is an integer value you create a shifted int value that is immediately  
lost. Since you do not modify numberlist and values contained in it you  
do see exactly the same output as before.

You rather want something like

File.open(city\_name + start\_code, 'w' ) do |w|  
&nbsp;&nbsp;puts numberlist.map {|i| "#{start\_code}#{i}"}  
end

Kind regards

robert

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 August 2013 07:29 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/13 "2013-08-26T07:29:05Z")

</div>

> The reason I am using an array is that sometimes I will need to generate  
> a secondary file, based off of the same numbers. In the second file I  
> will need to iterate over the array and concatenate additional numbers  
> onto the front of each value within the array.

That is not a good reason IMHO. Only if creating the sequence of numbers  
was costly (i.e. involving expensive calculations) this would be a good  
reason. OTOH if the range is potentially large you are burning a lot of  
memory which might cause an issue. Whether that IS an issue for your use  
case I cannot tell because there is a whole lot that we do not know about  
your program. Generally you could assume though that the cost of IO  
dominates the iteration - but that is just a rule of thumb.

> Sometimes that number begins with a 0, so I collect start\_code with

I think I'd still rather use Fixnum for iterating the range and use a  
proper String formatting (printf) on output to preserve the appropriate  
number of leading zeros.

Kind regards

robert

> **···**
>
> On Mon, Aug 26, 2013 at 8:37 AM, Ben G. \<lists@ruby-forum.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 August 2013 07:54 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/14 "2013-08-26T07:54:44Z")

</div>

Thanks Robert for these great timely and detailed responses!

Yes, prepend describes exactly what I am trying to accomplish. So I have  
my array numberlist. It is simply a range of generated numbers  
ex. 21,22,23,24

then I need to be able to prepend a code (ex. 02) in front of each value  
ex. 21,22,23,24 becomes 0221, 0222, 02223, 0224 and write that to a file  
like above so the final output is

0221  
0222  
0223  
0224

> start\_code = Integer(gets)

originally I used a similar thing  
start\_code = gets.to\_i

however in the instances where the start code begins with a 0 the 0 gets  
dropped

ex. start\_code = gets.to\_i  
start\_code = 02  
=\> 2

so I switched to  
start\_code = gets.to\_s  
which gives me the proper  
=\>02

> File.open(city\_name + start\_code, 'w' ) do |w|  
> &nbsp;&nbsp;puts numberlist.map {|i| "#{start\_code}#{i}"}  
> end

Okay so I have read through the documentation on the map method. This  
does look like almost what I need but it seems as if the map method,  
inserts start\_code in between each element as opposed to prepending it  
to each element. Is this correct?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 August 2013 08:06 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/15 "2013-08-26T08:06:11Z")

</div>

> OTOH if the range is potentially large you are burning a lot of  
> memory which might cause an issue.

Well right now I have kept it at generating 80 numbers but the final  
goal is to generate a range that is in the millions. I hadn't considered  
the speed effects of storing the values in RAM as opposed to writing  
them straight to disk, but logically that makes a lot more sense as it  
eliminates half the work. This is the first program of any substance  
that I have created and this consideration was a very large oversight.  
Thank you for explaining this.

> I think I'd still rather use Fixnum for iterating the range and use a  
> proper String formatting (printf) on output to preserve the appropriate  
> number of leading zeros.

I am reading the documentation on these now. It looks like I have got  
some rewritting to do 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 August 2013 08:05 UTC](https://rubytalk.org/t/each-value-in-an-array-dump-to-new-line/68363/16 "2013-08-26T08:05:56Z")

</div>

> Thanks Robert for these great timely and detailed responses!

You're welcome.

> so I switched to  
> start\_code = gets.to\_s  
> which gives me the proper  
> =\>02

The ".to\_s" is still superfluous. And, as I said, I'd prefer to work with  
integers internally.

> File.open(city\_name + start\_code, 'w' ) do |w|  
> \> puts numberlist.map {|i| "#{start\_code}#{i}"}  
> \> end
> 
> Okay so I have read through the documentation on the map method. This  
> does look like almost what I need but it seems as if the map method,  
> inserts start\_code in between each element as opposed to prepending it  
> to each element. Is this correct?

No.

Cheers

robert

> **···**
>
> On Mon, Aug 26, 2013 at 9:54 AM, Ben G. \<lists@ruby-forum.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
