Advertisement
nanenj

File.gets example.

Nov 14th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.79 KB | None | 0 0
  1. http://ruby-doc.org/core-2.0.0/IO.html#method-i-gets
  2.  
  3. https://ruby-doc.org/core-2.2.0/doc/syntax/control_expressions_rdoc.html#label-while+Loop
  4.  
  5. [powder:~] cat sample_file.txt                                                                                                                                                                                             This is the first line
  6. This is the second line
  7. This is the third line
  8.  
  9. [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
  10. This is part of the third line.
  11.  
  12. --
  13.  
  14. irb(main):001:0> File.new("sample_file.txt")
  15. => #<File:sample_file.txt>
  16. irb(main):002:0> f = _
  17. => #<File:sample_file.txt>
  18. irb(main):003:0> f.gets
  19. => "This is the first line\n"
  20. irb(main):004:0> f.gets
  21. => "This is the second line\n"
  22. irb(main):005:0> f.gets
  23. => "This is the third line\n"
  24. irb(main):006:0> f.gets
  25. => "\n"
  26. irb(main):007:0> f.gets
  27. => nil
  28. irb(main):008:0>
  29.  
  30. --
  31.  
  32. irb(main):008:0> f = File.new("sample_file_2.txt")
  33. => #<File:sample_file_2.txt>
  34. irb(main):009:0> f.gets
  35. => "This will be considered the first line$This will be considered the second line$This will be considered the third line\n"
  36. irb(main):010:0>
  37.  
  38. --
  39.  
  40. irb(main):010:0> f = File.new("sample_file_2.txt")
  41. => #<File:sample_file_2.txt>
  42. irb(main):011:0> f.gets("$")
  43. => "This will be considered the first line$"
  44. irb(main):012:0> f.gets("$")
  45. => "This will be considered the second line$"
  46. irb(main):013:0> f.gets("$")
  47. => "This will be considered the third line\nThis is part of the third line.\n"
  48. irb(main):014:0>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement