in Ruby, how do I write array elements into txt file such that each element is on a separate line?












0















I am having difficulty in writing array elements into a text file in the form of 1 element per line. In this instance array is built on sentences (.) .



Please see comments in code below:



puts "enter paragraph:"
para = gets.chomp.to_s
my_array =

para.split('.').each { |p| my_array << p+ '.'; print "pushed #{p}.";puts}
new_text = File.new("new_text.txt", "w+")
p my_array
my_array.each { |m| new_text.write(m)} #clearly iterating over my_array.
#.each should be writing each element on a different line, no? Where have I gone wrong?
new_text.seek(0)

#text file is still stored in new_text variable
#the read out shows elements are not written per line
line = 1
new_text.each do |n|
puts "line #{line}: #{n}"
line += 1
end









share|improve this question

























  • One way to write each element (a string) of an array arr to a file, one line per element, is File.write(filename, arr.join("n")).

    – Cary Swoveland
    Nov 13 '18 at 3:04
















0















I am having difficulty in writing array elements into a text file in the form of 1 element per line. In this instance array is built on sentences (.) .



Please see comments in code below:



puts "enter paragraph:"
para = gets.chomp.to_s
my_array =

para.split('.').each { |p| my_array << p+ '.'; print "pushed #{p}.";puts}
new_text = File.new("new_text.txt", "w+")
p my_array
my_array.each { |m| new_text.write(m)} #clearly iterating over my_array.
#.each should be writing each element on a different line, no? Where have I gone wrong?
new_text.seek(0)

#text file is still stored in new_text variable
#the read out shows elements are not written per line
line = 1
new_text.each do |n|
puts "line #{line}: #{n}"
line += 1
end









share|improve this question

























  • One way to write each element (a string) of an array arr to a file, one line per element, is File.write(filename, arr.join("n")).

    – Cary Swoveland
    Nov 13 '18 at 3:04














0












0








0








I am having difficulty in writing array elements into a text file in the form of 1 element per line. In this instance array is built on sentences (.) .



Please see comments in code below:



puts "enter paragraph:"
para = gets.chomp.to_s
my_array =

para.split('.').each { |p| my_array << p+ '.'; print "pushed #{p}.";puts}
new_text = File.new("new_text.txt", "w+")
p my_array
my_array.each { |m| new_text.write(m)} #clearly iterating over my_array.
#.each should be writing each element on a different line, no? Where have I gone wrong?
new_text.seek(0)

#text file is still stored in new_text variable
#the read out shows elements are not written per line
line = 1
new_text.each do |n|
puts "line #{line}: #{n}"
line += 1
end









share|improve this question
















I am having difficulty in writing array elements into a text file in the form of 1 element per line. In this instance array is built on sentences (.) .



Please see comments in code below:



puts "enter paragraph:"
para = gets.chomp.to_s
my_array =

para.split('.').each { |p| my_array << p+ '.'; print "pushed #{p}.";puts}
new_text = File.new("new_text.txt", "w+")
p my_array
my_array.each { |m| new_text.write(m)} #clearly iterating over my_array.
#.each should be writing each element on a different line, no? Where have I gone wrong?
new_text.seek(0)

#text file is still stored in new_text variable
#the read out shows elements are not written per line
line = 1
new_text.each do |n|
puts "line #{line}: #{n}"
line += 1
end






ruby file iteration each element






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 2:07







foo

















asked Nov 13 '18 at 1:54









foofoo

377




377













  • One way to write each element (a string) of an array arr to a file, one line per element, is File.write(filename, arr.join("n")).

    – Cary Swoveland
    Nov 13 '18 at 3:04



















  • One way to write each element (a string) of an array arr to a file, one line per element, is File.write(filename, arr.join("n")).

    – Cary Swoveland
    Nov 13 '18 at 3:04

















One way to write each element (a string) of an array arr to a file, one line per element, is File.write(filename, arr.join("n")).

– Cary Swoveland
Nov 13 '18 at 3:04





One way to write each element (a string) of an array arr to a file, one line per element, is File.write(filename, arr.join("n")).

– Cary Swoveland
Nov 13 '18 at 3:04












1 Answer
1






active

oldest

votes


















3














.each should be writing each element on a different line? no



No, whether you're iterating over something does not really matter. What matters is how you're writing into the file.



Currently you're using IO#write which doesn't say anything about adding newlines. If you change new_text.write to new_text.puts (IO#puts) you will be writing a new line after each element from your array.



You can easily see it by using $stdout directly:



> a = %w(foo bar)
=> ["foo", "bar"]
> a.each(&$stdout.method(:write)) # write -- no newlines
foobar => ["foo", "bar"]
> a.each(&$stdout.method(:puts)) # puts -- newlines
foo
bar
=> ["foo", "bar"]





share|improve this answer


























  • my mistake. I have removed faulty question.

    – foo
    Nov 13 '18 at 2:08






  • 1





    Also, notably, new_text.puts(my_array) will do exactly what you want, even without any explicit loop - puts prints arrays with one line per row by default.

    – Amadan
    Nov 13 '18 at 7:40













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272662%2fin-ruby-how-do-i-write-array-elements-into-txt-file-such-that-each-element-is-o%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














.each should be writing each element on a different line? no



No, whether you're iterating over something does not really matter. What matters is how you're writing into the file.



Currently you're using IO#write which doesn't say anything about adding newlines. If you change new_text.write to new_text.puts (IO#puts) you will be writing a new line after each element from your array.



You can easily see it by using $stdout directly:



> a = %w(foo bar)
=> ["foo", "bar"]
> a.each(&$stdout.method(:write)) # write -- no newlines
foobar => ["foo", "bar"]
> a.each(&$stdout.method(:puts)) # puts -- newlines
foo
bar
=> ["foo", "bar"]





share|improve this answer


























  • my mistake. I have removed faulty question.

    – foo
    Nov 13 '18 at 2:08






  • 1





    Also, notably, new_text.puts(my_array) will do exactly what you want, even without any explicit loop - puts prints arrays with one line per row by default.

    – Amadan
    Nov 13 '18 at 7:40


















3














.each should be writing each element on a different line? no



No, whether you're iterating over something does not really matter. What matters is how you're writing into the file.



Currently you're using IO#write which doesn't say anything about adding newlines. If you change new_text.write to new_text.puts (IO#puts) you will be writing a new line after each element from your array.



You can easily see it by using $stdout directly:



> a = %w(foo bar)
=> ["foo", "bar"]
> a.each(&$stdout.method(:write)) # write -- no newlines
foobar => ["foo", "bar"]
> a.each(&$stdout.method(:puts)) # puts -- newlines
foo
bar
=> ["foo", "bar"]





share|improve this answer


























  • my mistake. I have removed faulty question.

    – foo
    Nov 13 '18 at 2:08






  • 1





    Also, notably, new_text.puts(my_array) will do exactly what you want, even without any explicit loop - puts prints arrays with one line per row by default.

    – Amadan
    Nov 13 '18 at 7:40
















3












3








3







.each should be writing each element on a different line? no



No, whether you're iterating over something does not really matter. What matters is how you're writing into the file.



Currently you're using IO#write which doesn't say anything about adding newlines. If you change new_text.write to new_text.puts (IO#puts) you will be writing a new line after each element from your array.



You can easily see it by using $stdout directly:



> a = %w(foo bar)
=> ["foo", "bar"]
> a.each(&$stdout.method(:write)) # write -- no newlines
foobar => ["foo", "bar"]
> a.each(&$stdout.method(:puts)) # puts -- newlines
foo
bar
=> ["foo", "bar"]





share|improve this answer















.each should be writing each element on a different line? no



No, whether you're iterating over something does not really matter. What matters is how you're writing into the file.



Currently you're using IO#write which doesn't say anything about adding newlines. If you change new_text.write to new_text.puts (IO#puts) you will be writing a new line after each element from your array.



You can easily see it by using $stdout directly:



> a = %w(foo bar)
=> ["foo", "bar"]
> a.each(&$stdout.method(:write)) # write -- no newlines
foobar => ["foo", "bar"]
> a.each(&$stdout.method(:puts)) # puts -- newlines
foo
bar
=> ["foo", "bar"]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 2:08

























answered Nov 13 '18 at 2:04









Marcin KołodziejMarcin Kołodziej

4,3111315




4,3111315













  • my mistake. I have removed faulty question.

    – foo
    Nov 13 '18 at 2:08






  • 1





    Also, notably, new_text.puts(my_array) will do exactly what you want, even without any explicit loop - puts prints arrays with one line per row by default.

    – Amadan
    Nov 13 '18 at 7:40





















  • my mistake. I have removed faulty question.

    – foo
    Nov 13 '18 at 2:08






  • 1





    Also, notably, new_text.puts(my_array) will do exactly what you want, even without any explicit loop - puts prints arrays with one line per row by default.

    – Amadan
    Nov 13 '18 at 7:40



















my mistake. I have removed faulty question.

– foo
Nov 13 '18 at 2:08





my mistake. I have removed faulty question.

– foo
Nov 13 '18 at 2:08




1




1





Also, notably, new_text.puts(my_array) will do exactly what you want, even without any explicit loop - puts prints arrays with one line per row by default.

– Amadan
Nov 13 '18 at 7:40







Also, notably, new_text.puts(my_array) will do exactly what you want, even without any explicit loop - puts prints arrays with one line per row by default.

– Amadan
Nov 13 '18 at 7:40




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272662%2fin-ruby-how-do-i-write-array-elements-into-txt-file-such-that-each-element-is-o%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ