InterviewSolution
Saved Bookmarks
| 1. |
Difference between File.new method and File.open method in Ruby? |
|
Answer» Both the methods are used to OPEN a file in Ruby Difference between both the methods is that File.new method cannot be associated with a BLOCK whereas File.open method can be File.new method: Using this method a new file can be created for READING, writing or both. Syntax: F = File.new("fileName.rb") File.open method : Using this method a new file object is created. That file object is ASSIGNED to a file. Syntax: #!/usr/bin/ruby File.open('about', 'w') do |f| f.puts "This is KnowledgeHut" f.write "You are reading Ruby interview questions\n" f << "Please visit our website.\n" end |
|