-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex20.rb
More file actions
49 lines (33 loc) · 1.24 KB
/
ex20.rb
File metadata and controls
49 lines (33 loc) · 1.24 KB
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
input_file = ARGV.first
def print_all(f)
puts f.read
end
def rewind(f)
f.seek(0)
end
# It seeks ("goes to", "attempts to find") a given position (as integer) in a stream. In your code you define a new method called rewind which takes one argument. When you call it with
#rewind(current_file)
#you send the current_file (the one you have opened from disk or from anywhere else) which is defined as:
#
# current_file = File.open(input_file)
#to the rewind method and it will "seek" to position 0 which is the beginning of the file.
#
# You could for example make another method called almost_rewind and write:
#
# def almost_rewind(f)
# f.seek(-10)
# 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 wholefile: \n"
print_all(current_file)
puts "Let's print three lines:"
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
#x = x + y is the same as x += y