Advertisement
mjshi

Reading and Writing Text Files in Ruby

Jan 4th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.73 KB | None | 0 0
  1. %Q(=============================================================================
  2.  
  3.           Reading and Writing Text Files in Ruby -  A Quick Tutorial
  4.                             presented by mjshi
  5.                            
  6. --------------------------------------------------------------------------------
  7.     Making a file!
  8. --------------------------------------------------------------------------------
  9.    
  10.   f = File.new("file_name.txt", "w")
  11.    
  12.     Of course, you can have your own filename, and f can be anything!
  13.     You just have to change everything else to match.
  14.     So if I had
  15.    
  16.   a = File.new("boo.txt", "w")
  17.  
  18.     then my other commands would be
  19.    
  20.   a.write("BOO!")
  21.   a.close
  22.    
  23. --------------------------------------------------------------------------------
  24.     Saving text into it!
  25. --------------------------------------------------------------------------------
  26.  
  27.   f.write("Hi there!")
  28.    
  29.     To make text appear on different lines, add a \n!
  30.    
  31.   f.write("Hi\nthere!")
  32.    
  33.     will make the text file look like this:
  34.    
  35.     Hi
  36.     there!
  37.    
  38. --------------------------------------------------------------------------------
  39.     Closing the file
  40. --------------------------------------------------------------------------------
  41.    
  42.     Make sure to close the file after you're done with writing stuff into
  43.    it so the file doesn't take up memory space.
  44.    
  45.   f.close
  46.  
  47.     Well, that was easy.
  48.    
  49. --------------------------------------------------------------------------------
  50.     Reading Text Files
  51. --------------------------------------------------------------------------------
  52.    
  53.   c = File.open("key.txt", &:gets) if File.exist?("key.txt")
  54.    
  55.     Now, let's translate this into common English!
  56.    
  57.  +-----------------------------------------------------------------------------
  58.  |  c =
  59.  |    Save the following stuff in c --
  60.  |  
  61.  |  File.open("key.txt", &:gets)
  62.  |    the strings of text inside the file named "key.txt"
  63.  |  
  64.  |  if File.exist?("key.txt")
  65.  |    if the file named "key.txt" can be found in the game root directory.
  66.  +-----------------------------------------------------------------------------
  67.  
  68.    File.exist?("filename.txt") can actually also be used in a conditional
  69.    branch's script section to check if the file is in that directory, and
  70.    
  71.     File.zero?("filename.txt") can also be used to check if the file is empty.
  72.    
  73. --------------------------------------------------------------------------------
  74.  
  75.                             Hope that was helpful!
  76.                     Now, go forth and break some 4th walls!
  77.                        
  78. ===============================================================================)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement