Why can't I use `filename.open' instead of `open(filename)'? - ruby

In this piece of code which lets you read a file in the terminal, why do you need to use open(filename) rather than filename.open?
filename = ARGV.first
txt = open(filename)
puts "Here's your file #{filename}:"
print txt.read
print "Type the filename again: "
file_again = $stdin.gets.chomp
txt_again = open(file_again)
print txt_again.read

You cant use filename.open, because filename is a String and method open is not defined in String
Use File#open
File.open(filename)

File.open("file")
opens a local file and returns a file object. Here File#open is a method of File class.
open("file")
is actually Kernel#open and looks at the string to decide what to do with it.
Trivializing things:
File.open("file") is telling Ruby specifically to open a file.
In case of open("file") Ruby examines the string "file" to determine what type it is (here a file) and open corresponding type else throws appropriate error.

Ruby has a class for dealing with paths in an object-oriented way: Pathname
require 'pathname'
loop do
print 'Enter filename: '
pn = Pathname(gets.chomp)
if pn.file?
puts "Here's your file '#{pn}':", pn.read
elsif pn.exist?
puts 'That is not a file.'
else
puts 'File does not exist.'
end
end

Related

Reading file input with ruby via command line input

So I am trying to read a file in ruby by giving the name via the command line. So far my code reads as follows:
puts "What is the name of the file to read?"
fileName = gets.chomp
file = $stdin.read.strip
f = File.open(file, “r”)
f.each_line { |line|
puts line
}
What I see happening is it is reading the inputs through the command line but does not read a file. For example, I can pass 'input.txt', 'code.txt', and 'sonic.txt' as file names but the program just loops back seeking another input. How can I change this to read the file by name and then out put the contents of that file?
Your problems are:
The line fileName = gets.chomp is useless. Remove that.
file = $stdin.read.strip will not let you terminate the input. Use gets to get user's input from the command line.
You are using the wrong quotation “ in your parameter “r” for File.open.
You are not closing the file after reading it. It is better to use the block form of File.open to ensure the file is closed after using.
Here is a minimum fix:
puts "What is the name of the file to read?"
file = gets.chomp
File.open(file, "r"){|f|
f.each_line {|line|
puts line
}
}

How would you close this file descriptor?

Let's say you have the following code:
from_file, to_file = ARGV
puts "Copying from #{from_file} to #{to_file}"
#in_file = open(from_file)
#indata = in_file.read
indata = open(from_file).read # Combined in_file and indata.
puts "The input file is #{indata.length} bytes long."
puts "Does the output file exist? #{File.exist?(to_file)}"
puts "Ready, hit RETURN to continue or CTRL-C to abort."
$stdin.gets
out_file = open(to_file, 'w')
out_file.write(indata)
puts "Alright, all done."
out_file.close
#in_file.close
How would you close the file descriptor invoked by indata? You will need to close File open, but indata is really a (File open).read.
P.S. Since it's a script, it will be closed automatically upon exit. Let's assume that we're running a general, consistently running backend service. And we don't know whether garbage collector will kick in, so we will need to explicitly close it. What would you do?
If you are just copying the file...
you could just use FileUtils#cp:
FileUtils.cp("from_file", "to_file")
or even shell-out to the operating system and do it with a system command.
Let's suppose you want to do something to the input file before writing it to the output file.
If from_file is not large,...
you could "gulp it" into a string using IO.read:
str = IO.read(from_file)
manipulate str as desired, to obtain new_str, then then blast it to the output file using IO#write:
IO.write("to_file", new_str)
Note that for the class File:
File < IO #=> true # File inherits IO's methods
which is why you often see this written File.read(...) and File.write(...).
If from_file is large, read a line, write a line...
provided the changes to be made are done for each line separately.
f = File.open("to_file", "w") # or File.new("to_file", "w")
IO.foreach("from_file") do |line|
# < modify line to produce new_line >
f.puts new_line
end
f.close
foreach closes "from_file" when it's finished. If f.close is not present, Ruby will close "to_file" when the method containing the code goes out of scope. Still, it's a good idea to close it in case other work is done before the code goes out of scope.
Passing File.open a block is generally a nice way to go about things, so I’ll offer it up as an alternative even if it doesn’t seem to be quite what you asked.
indata = File.open(from_file) do |f|
f.read
end

Undefined method 'close' for main:Object (NoMethodError) in Ruby

filename = ARGV.first
txt = open filename
puts "Here's your file #{filename}:"
print txt.read
puts "Type the filename again: "
file_again = $stdin.gets.chomp
txt_again = open file_again
print txt_again.read
close(txt)
close(txt_again)
The program runs fine till the end, but crashes with the titled error message right after printing the contents of the second file.
I checked the txt, txt_again using (.class) and confirmed that both are File objects. Why isn't close working?
You need to call close on the file object:
txt.close

Sanitize filenames in Ruby / Rails

To determine the file type of an attached file, I used the OS "file" utility:
class AttachedFileTypeValidator < ActiveModel::Validator
def validate(record)
file = record.resource.uploaded_file
attached_file = Rails.root + file.path
file_type = `file #{attached_file}`
Rails.logger.info "Attached file type determined to be: #{file_type}"
unless file_type.split(',').first =~ /ASCII|UTF/
record.errors[:resource_content_type] << "Attachment does not appear to be a text CSV file, please ensure it was saved correctly."
end
end
end
Unfortunately brakeman suggests its a command line injection opportunity. I'm assuming this means someone figures out a clever name for a file like:
; rm -rf /;
And away we go. Whats a good way to sanitize filenames?
Use IO#popen to call the external command:
file_type = IO.popen(['file', attached_file]).read
This will handle proper escaping of funny characters in the filename for you.

How can I copy the contents of one file to another using Ruby's file methods?

I want to copy the contents of one file to another using Ruby's file methods.
How can I do it using a simple Ruby program using file methods?
There is a very handy method for this - the IO#copy_stream method - see the output of ri copy_stream
Example usage:
File.open('src.txt') do |f|
f.puts 'Some text'
end
IO.copy_stream('src.txt', 'dest.txt')
For those that are interested, here's a variation of the IO#copy_stream, File#open + block answer(s) (written against ruby 2.2.x, 3 years too late).
copy = Tempfile.new
File.open(file, 'rb') do |input_stream|
File.open(copy, 'wb') do |output_stream|
IO.copy_stream(input_stream, output_stream)
end
end
As a precaution I would recommend using buffer unless you can guarantee whole file always fits into memory:
File.open("source", "rb") do |input|
File.open("target", "wb") do |output|
while buff = input.read(4096)
output.write(buff)
end
end
end
Here my implementation
class File
def self.copy(source, target)
File.open(source, 'rb') do |infile|
File.open(target, 'wb') do |outfile2|
while buffer = infile.read(4096)
outfile2 << buffer
end
end
end
end
end
Usage:
File.copy sourcepath, targetpath
Here is a simple way of doing that using ruby file operation methods :
source_file, destination_file = ARGV
script = $0
input = File.open(source_file)
data_to_copy = input.read() # gather the data using read() method
puts "The source file is #{data_to_copy.length} bytes long"
output = File.open(destination_file, 'w')
output.write(data_to_copy) # write up the data using write() method
puts "File has been copied"
output.close()
input.close()
You can also use File.exists? to check if the file exists or not. This would return a boolean true if it does!!
Here's a fast and concise way to do it.
# Open first file, read it, store it, then close it
input = File.open(ARGV[0]) {|f| f.read() }
# Open second file, write to it, then close it
output = File.open(ARGV[1], 'w') {|f| f.write(input) }
An example for running this would be.
$ ruby this_script.rb from_file.txt to_file.txt
This runs this_script.rb and takes in two arguments through the command-line. The first one in our case is from_file.txt (text being copied from) and the second argument second_file.txt (text being copied to).
You can also use File.binread and File.binwrite if you wish to hold onto the file contents for a bit. (Other answers use an instant copy_stream instead.)
If the contents are other than plain text files, such as images, using basic File.read and File.write won't work.
temp_image = Tempfile.new('image.jpg')
actual_img = IO.binread('image.jpg')
IO.binwrite(temp_image, actual_img)
Source: binread,
binwrite.

Resources