I/o exercises/ file shuffle

I have trouble understanding the last part of file shuffle.If anyone could explain to me what this means it would be really helpful.
TIA

if FILE == $PROGRAM_NAME
IF ARGV.length==1
shuffle_file(ARGV.shift)
else
puts “ENTER FILE NAME TO SHUFFLE”
filename=gets.chomp
shuffle_file(filename)
end

Hi nathashas1,

So the if FILE == $PROGRAM_NAME part allows you to run whatever is inside your if statement when you call the file in the terminal like so ruby FILE.rb.
Now let’s discuss the inside if-else statement. Here you are checking if there are any arguments in your terminal input. Basically, you can pass in the file you want to shuffle as an argument to the terminal like so ruby FILE.rb file_you_want_to_shuffle.rb. The term ARGV stores your arguments in an array and then ARGV.shift passes the first element to your shuffle_file method. Note that if you enter multiple arguments, the if statement will be false because ARGV stores each argument as an element in its array. Here is the method from the ruby docs for reference: https://ruby-doc.org/core-1.9.3/ARGF.html#method-i-argv.
Therefore in your else statement, if you are not given an argument or have multiple arguments, you enter the file to be shuffled at the gets.chomp line where your input is parsed as a string and saved as the variable filename. This will then be the argument passed to the shuffle_file method. Hope this helps!

Michael

Thank you so much. Its clear now.

Could you please tell me how to test the shuffle file program.
How to exactly test it in the terminal?

Put the file you want to shuffle in the lib folder. cd into the lib folder. Then run
ruby io_exercises.rb file_to_shuffle.txt
but replace “file_to_shuffle.txt” with the name of your file.

The other way to do it is to run
ruby io_exercises.rb
And when you’re prompted enter
file_to_shuffle.txt

Hi nathashas1,

I would create a txt file in the same directory as your shuffle_file file. You can call it test.txt or whatever and you add lines of text in the txt file. You can copy some article or just write out something. Now you can call your shuffle_file method in the terminal ruby FILE.rb test.txt or ruby FILE.rb and then enter test.txt when prompted. Like how ThaBullfrog explained it as I was writing this. If shuffle_file is ran successfully, you should see a new file generated in the same directory with your text in test.txt all shuffled up. If not, you will need to do some debugging. Have fun!

Michael

It worked.Thank you !