# Add a string on the end of values before splitting new line

**URL:** <https://rubytalk.org/t/add-a-string-on-the-end-of-values-before-splitting-new-line/52133>\
**Category:** ruby-talk\
**Created:** [5 March 2009 16:47 UTC](https://rubytalk.org/t/add-a-string-on-the-end-of-values-before-splitting-new-line/52133 "2009-03-05T16:47:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [5 March 2009 16:47 UTC](https://rubytalk.org/t/add-a-string-on-the-end-of-values-before-splitting-new-line/52133/1 "2009-03-05T16:47:55Z")

</div>

Hi again. I am starting a new topic since this question is not the same  
as my original topic started this morning. "It builds on to that  
question."

Originally I was reading a textfile that had values that were comma  
delimited. The values were not being outputted to separated lines. Now  
that they do that..I was hoping that someone could help me to add a  
comment before the line goes into splitting. Thanks everyone for being  
so helpful! MC

Question:  
Do you know how to add a string on the end of each value before  
splitting into a new line? For instance...

textfile contents: value1,value2,value3...

value1 comments  
value2 comments  
value3 comments

I tried this using Robert's code, however, it put the comments on a  
newline.

File.foreach "temp.txt" do |line|  
&nbsp;&nbsp;&nbsp;puts "", line.split(/,/),"comments" + @usercomments  
end

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

---

<div class="post-metadata">

**Author:** ![Choi\_Junegunn](https://avatars.discourse-cdn.com/v4/letter/c/eb9ed0/32.png) [@Choi\_Junegunn](https://rubytalk.org/u/Choi_Junegunn)\
**Post date:** [6 March 2009 11:44 UTC](https://rubytalk.org/t/add-a-string-on-the-end-of-values-before-splitting-new-line/52133/2 "2009-03-06T11:44:44Z")

</div>

You could use Array#map method.

> **···**
>
> 2009/3/6 Mmcolli00 Mom \<mmc\_collins@yahoo.com\>:
> 
> > Question:  
> > Do you know how to add a string on the end of each value before  
> > splitting into a new line? For instance...
> > 
> > File.foreach "temp.txt" do |line|  
> > puts "", line.split(/,/),"comments" + @usercomments  
> > end
> 
> --  
> junegunn.

---

<div class="post-metadata">

**Author:** ![Raveendran\_P](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Raveendran\_P](https://rubytalk.org/u/Raveendran_P)\
**Post date:** [6 March 2009 12:17 UTC](https://rubytalk.org/t/add-a-string-on-the-end-of-values-before-splitting-new-line/52133/3 "2009-03-06T12:17:34Z")

</div>

Mmcolli00 Mom wrote:

> Hi again. I am starting a new topic since this question is not the same  
> as my original topic started this morning. "It builds on to that  
> question."
> 
> Originally I was reading a textfile that had values that were comma  
> delimited. The values were not being outputted to separated lines. Now  
> that they do that..I was hoping that someone could help me to add a  
> comment before the line goes into splitting. Thanks everyone for being  
> so helpful! MC
> 
> Question:  
> Do you know how to add a string on the end of each value before  
> splitting into a new line? For instance...
> 
> textfile contents: value1,value2,value3...
> 
> value1 comments  
> value2 comments  
> value3 comments
> 
> I tried this using Robert's code, however, it put the comments on a  
> newline.
> 
> File.foreach "temp.txt" do |line|  
> &nbsp;&nbsp;&nbsp;puts "", line.split(/,/),"comments" + @usercomments  
> end

Hi Mom,

1. Collect all data from file and store it as a single String variable  
@a. So @a.class==String

2. Ex, @file="value1,value2,value3,value4"

3. Code

@file="value1,value2,value3,value4"  
@output=  
@file.split(/,/).each do |record|  
&nbsp;&nbsp;@output \<\< record + " comments"  
end  
puts @output # =\> It gives =\> ["value1 comments","value2  
comments","value3 comments","value4 comments"]

So use puts @output.join("\n").

It will gives output like:

value1 comments  
value2 comments  
value3 comments  
value4 comments

Regards,  
P.Raveendran

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

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

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [6 March 2009 12:40 UTC](https://rubytalk.org/t/add-a-string-on-the-end-of-values-before-splitting-new-line/52133/4 "2009-03-06T12:40:56Z")

</div>

@file="value1,value2,value3,value4"  
# @output=  
# @file.split(/,/).each do |record|  
# @output \<\< record + " comments"  
# end

output = @file.split(/,/).map {|record| record + comments}

each + filling an array =\> map

Jesus.

> **···**
>
> On Fri, Mar 6, 2009 at 1:17 PM, jazzez ravi \<jazzezravi@gmail.com\> wrote:
> 
> > 3. Code
