tags:

views:

90

answers:

3

I have:

 o = File.new("ouput.txt", "rw+")
 File.new("my_file.txt").lines.reverse_each { |line|
       ?????  line 
 }
 o.close

I don't know what method to use to write to the file output o

A: 

I knew it was easy, what I don't quite get is why is not documented here?

o = File.new("ouput.txt", "w+")
File.new("my_file.txt").lines.reverse_each { |line|
    o.puts line 
}
o.close
OscarRyz
out.puts or o.puts?
Jason Noble
@OscarRyz- It isn't documented there because `puts` isn't a member of class `File`. It's actually a member of class `IO`, and class `File` is a subclass of `IO`. The documentation for it is here: http://ruby-doc.org/core/classes/IO.html
bta
A: 

You're gonna wanna do something more like...

new_text = File.readlines('my_file').reverse.join
File.open('my_file', 'w+') { |file| file.write(new_text) }

Check out this documentation to figure out what w+ means.

c00lryguy
+3  A: 

puts understands arrays, so you can simplify this to:

File.open("f2.txt","w") {|o| o.puts File.readlines("f1.txt").reverse}
glenn mcdonald
Didnt even think of this, nice.
c00lryguy