-
Notifications
You must be signed in to change notification settings - Fork 115
/
ruby_lab.rb
executable file
·58 lines (48 loc) · 1.14 KB
/
ruby_lab.rb
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/ruby
###############################################################
#
# CSCI 305 - Ruby Programming Lab
#
# <firstname> <lastname>
# <email-address>
#
###############################################################
$bigrams = Hash.new # The Bigram data structure
$name = "<firstname> <lastname>"
# function to process each line of a file and extract the song titles
def process_file(file_name)
puts "Processing File.... "
begin
if RUBY_PLATFORM.downcase.include? 'mswin'
file = File.open(file_name)
unless file.eof?
file.each_line do |line|
# do something for each line (if using windows)
end
end
file.close
else
IO.foreach(file_name, encoding: "utf-8") do |line|
# do something for each line (if using macos or linux)
end
end
puts "Finished. Bigram model built.\n"
rescue
STDERR.puts "Could not open file"
exit 4
end
end
# Executes the program
def main_loop()
puts "CSCI 305 Ruby Lab submitted by #{$name}"
if ARGV.length < 1
puts "You must specify the file name as the argument."
exit 4
end
# process the file
process_file(ARGV[0])
# Get user input
end
if __FILE__==$0
main_loop()
end