in Ruby, how do I write array elements into txt file such that each element is on a separate line?
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
One way to write each element (a string) of an arrayarr
to a file, one line per element, isFile.write(filename, arr.join("n"))
.
– Cary Swoveland
Nov 13 at 3:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
ruby file iteration each element
edited Nov 13 at 2:07
asked Nov 13 at 1:54
foo
63
63
One way to write each element (a string) of an arrayarr
to a file, one line per element, isFile.write(filename, arr.join("n"))
.
– Cary Swoveland
Nov 13 at 3:04
add a comment |
One way to write each element (a string) of an arrayarr
to a file, one line per element, isFile.write(filename, arr.join("n"))
.
– Cary Swoveland
Nov 13 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 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 at 3:04
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
.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"]
my mistake. I have removed faulty question.
– foo
Nov 13 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 at 7:40
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
.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"]
my mistake. I have removed faulty question.
– foo
Nov 13 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 at 7:40
add a comment |
up vote
3
down vote
.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"]
my mistake. I have removed faulty question.
– foo
Nov 13 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 at 7:40
add a comment |
up vote
3
down vote
up vote
3
down vote
.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"]
.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"]
edited Nov 13 at 2:08
answered Nov 13 at 2:04
Marcin Kołodziej
3,332314
3,332314
my mistake. I have removed faulty question.
– foo
Nov 13 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 at 7:40
add a comment |
my mistake. I have removed faulty question.
– foo
Nov 13 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 at 7:40
my mistake. I have removed faulty question.
– foo
Nov 13 at 2:08
my mistake. I have removed faulty question.
– foo
Nov 13 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 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 at 7:40
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
One way to write each element (a string) of an array
arr
to a file, one line per element, isFile.write(filename, arr.join("n"))
.– Cary Swoveland
Nov 13 at 3:04