1

For exercise 17, through searching other responses I was able to condense the following into one line (as asked in the extra credit #3)

from_file, to_file = ARGV
script = $0

input = File.open(from_file)
indata = input.read()

output = File.open(to_file, 'w')
output.write(indata)

output.close()
input.close()

I was able to condense it into:

from_file, to_file = ARGV
script = $0

File.open(to_file, 'w') {|f| f.write IO.read(from_file)}
  1. Is there a better/different way to condense this into 1 line?

  2. Can someone help explain the line I created? I created this from various questions/answers unrelated to this question. I have tried looking up exactly what I did but I am still a little lost and want a full understanding of it.

user2498045
  • 141
  • 1
  • 5

3 Answers3

4
  1. Similar to using IO::read to simplify "just read the whole file into a string", you can use IO::write to "just write the string to the file":

    from_file, to_file = ARGV
    IO.write(to_file, IO.read(from_file))
    

    Since you don't use script, it can be removed. If you really want to get things down to one line, you can do:

    IO.write(ARGV[1], IO.read(ARGV[0]))
    

    I personally find this just as comprehensible, and the lack of error checking is equivalent.

  2. You're using File#open with a block to open to_file in write-only mode ('w'). Inside the block you have access to the open file as f, and the file will be closed for you when the block terminates. IO::read reads the entire contents of from_file, which you then pass to IO#write on f (File is a subclass of IO), writing those contents to f (which is the open, write-only File for to_file).

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
1

There are always different ways of doing things:

Using File.open with a block is a good approach here. I like that to_file and from_file are declared in variables. So I think this is a good and readable solution that is not overly verbose.

The basic approach here is swapping out open/close operations with the more-clean File.open method with a block. File.open with a block will open a file, run the block, and then close the file, which is exactly what is needed here. Because the method automatically opens and closes the file, we are able to remove the boilerplate code that appears in the initial example. IO.read is another shortcut method that allows us to open/read/close the file without all of the open/close boilerplate. This is an exercise to learn more about Ruby's standard File/IO library, and in this case swapping out the more verbose methods is sufficient to reduce things to a single line.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Puhlze
  • 2,634
  • 18
  • 16
0

I'm just a complete beginner, but this works for me:

open(ARGV[1], 'w').write(open(ARGV[0]).read)    

It doesn't look elegant for me, but it works.

Edit: This is my attempt to put the entire script into one line if it's not clear.

  • 1
    This is not complete answer. What does mean? Please explain as care of question with more explain. – QMaster Dec 25 '14 at 20:54