简体   繁体   中英

Understanding IO methods and objects in Ruby

I am learning Ruby by following learnrubythehardway and it seems very easy and straightforward until you are asked to explain what certain things do. I have commented in my code what I believe is happening in the program. Wanted to see if I am on target, need to rethink things or have no clue and should stop trying to learn how to code.

# Sets variables to arguments sent through terminal
from_file, to_file = ARGV

# Saves from_file object into in_file
in_file = open(from_file)
puts in_file

# Saves from_file data into indata
indata = in_file.read
puts indata

# Save to_file object into out_file with writing privileages
out_file = open(to_file, 'w')
puts out_file

# Writes(copies) indata into out_file
out_file.write(indata)

# Closes files so they can not be accessed anymore
out_file.close
in_file.close

This is what the output in the terminal looks like:

#<File:0x0000000201b038>
This is text from the ex17_from.txt file.
Did it copy to the ex17_to.txt file?
#<File:0x0000000201ae58>

We are also given the task to try to reduce the amount of code needed and are told that we can do this entire action in one line of code. I figured I can just erase out all of the comments and puts statements, while everything else is put into one line of code. However, that will be one long line and I don't think that is what the author is asking for. Any ideas on how to shorten this code will be helpful.

I have commented in my code what I believe is happening in the program. Wanted to see if I am on target, need to rethink things or have no clue

You need to learn to see beyond the written words. For example, this comment is pretty useless:

# Saves from_file object into in_file
in_file = open(from_file)

Not only useless, but actually incorrect. What is this from_file object? What kind of object will in_file be? Why don't you mention open in any way?

What actually happens here is that a File/IO object is created by calling open . And from_file , in this case, is a path to file . Not much of an "object", is it? But in_file is a full File object, which you can use to read the file's contents.

The same goes for the rest of your comments. You simply reword the line of code with human words, without describing the intent behind the code.

You can use FileUtils#cp , and do the following:

require "fileutils"
FileUtils.cp *ARGV

* splats the ARGV array into two parameters needed by cp method


Alternatively, below is concise version of your code:

# Sets variables to arguments sent through terminal
from_file, to_file = ARGV

# Saves from_file object into to_file
open(to_file, 'w') do |out|
  open(from_file) do |f|
      f.each do |line|
        out << line
      end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM