-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.rb
116 lines (77 loc) · 2.43 KB
/
20.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# file name
input_file = ARGV.first
# define function
def print_all(f)
# read file
puts f.read
end
# define function
def rewind(f)
f.seek(0)
end
# define function
def print_a_line(line_count, f)
puts "#{line_count}, #{f.gets.chomp}"
end
# open file
current_file = open(input_file)
# print stetment
puts "First let's print the whole file:\n"
# print file
print_all(current_file)
puts "Now let's rewind, kind of like a tape."
# rewind file
rewind(current_file)
puts "Let's print three lines:"
# variable
current_line = 1
# print lines one by one
print_a_line(current_line, current_file)
# print lines one by one
current_line = current_line + 1
print_a_line(current_line, current_file)
# print lines one by one
current_line = current_line + 1
print_a_line(current_line, current_file)
# Each time print_a_line is run, you are passing in a variable current_line. Write out what current_line is equal to on each function call, and trace how it becomes line_count in print_a_line.
current_line = 1
print_a_line(current_line , file name)
current_line = current_line + 1 #line value = 2
print_a_line(current_line, file name)
current_line = current_line + 1 #line value = 3
print_a_line(current_line, file name)
# Find each place a function is used, and check its def to make sure that you are giving it the right arguments.
def print_all(f)
puts f.read
end
def rewind(f)
f.seek(0)
end
def print_a_line(line_count, f)
puts "#{line_count}, #{f.gets.chomp}"
end
# Research online what the seek function for file does. Try ri File, and see if you can figure it out from there. Then try ri "File#seek" to see what seek does.
# Seeks to a given offset anInteger in the stream according to the value of whence:
# Research the shorthand notation +=, and rewrite the script to use += instead.
input_file = ARGV.first
def print_all(f)
puts f.read
end
def rewind(f)
f.seek(0)
end
def print_a_line(line_count, f)
puts "#{line_count}, #{f.gets.chomp}"
end
current_file = open(input_file)
puts "First let's print the whole file:\n"
print_all(current_file)
puts "Now let's rewind, kind of like a tape."
rewind(current_file)
puts "Let's print three lines:"
current_line = 1
print_a_line(current_line, current_file)
current_line += 1
print_a_line(current_line, current_file)
current_line += 1
print_a_line(current_line, current_file)