-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_read.rb
More file actions
29 lines (24 loc) · 858 Bytes
/
file_read.rb
File metadata and controls
29 lines (24 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
if ARGV.size == 0
puts "You didn't tell us what file to read. Try this command:"
puts ""
puts " ruby file_read.rb random_file.txt"
exit # This exits the program
end
# If we've reached this line of code, we know the user supplied us with at least
# one command-line argument. We'll assume it's a file for us to read.
file_name = ARGV[0] # Set the value of file_name to the first command-line argument
file_contents = File.read(file_name) # Read the contents of the file specified by file_name
puts "The contents of #{file_name} are:"
puts "=========="
puts file_contents
puts "=========="
puts ""
puts "The contents of #{file_name} in all upper-case are:"
puts "=========="
puts file_contents.upcase
puts "=========="
puts ""
puts "The contents of #{file_name} in reverse are:"
puts "=========="
puts file_contents.reverse
puts "=========="