nanenj icon

File.gets example.

nanenj | PRO | 11/14/17 02:05:34 PM UTC | 0 ⭐ | 300 👁️ | Never ⏰ | []
Ruby |

1.79 KB

|

None

|

0 👍

/

0 👎

http://ruby-doc.org/core-2.0.0/IO.html#method-i-gets
 
https://ruby-doc.org/core-2.2.0/doc/syntax/control_expressions_rdoc.html#label-while+Loop
 
[powder:~] cat sample_file.txt                                                                                                                                                                                             This is the first line
This is the second line
This is the third line
 
[powder:~] cat sample_file_2.txt                                                                                                                                                                                           This will be considered the first line$This will be considered the second line$This will be considered the third line
This is part of the third line.
 
--
 
irb(main):001:0> File.new("sample_file.txt")
=> #<File:sample_file.txt>
irb(main):002:0> f = _
=> #<File:sample_file.txt>
irb(main):003:0> f.gets
=> "This is the first line\n"
irb(main):004:0> f.gets
=> "This is the second line\n"
irb(main):005:0> f.gets
=> "This is the third line\n"
irb(main):006:0> f.gets
=> "\n"
irb(main):007:0> f.gets
=> nil
irb(main):008:0>
 
--
 
irb(main):008:0> f = File.new("sample_file_2.txt")
=> #<File:sample_file_2.txt>
irb(main):009:0> f.gets
=> "This will be considered the first line$This will be considered the second line$This will be considered the third line\n"
irb(main):010:0>
 
--
 
irb(main):010:0> f = File.new("sample_file_2.txt")
=> #<File:sample_file_2.txt>
irb(main):011:0> f.gets("$")
=> "This will be considered the first line$"
irb(main):012:0> f.gets("$")
=> "This will be considered the second line$"
irb(main):013:0> f.gets("$")
=> "This will be considered the third line\nThis is part of the third line.\n"
irb(main):014:0>

Comments