Ex20 print_a_line doesn't display current_line in terminal


#1
input_file = ARGV.first

def print_all(f)
  puts f.read
end

def rewind(f)
  f.seek(1)
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(1, current_file)
current_line = 2
print_a_line(2, current_file)
current_line += 1
print_a_line(current_line, current_file)

Here’s what my terminal displays:
First let’s print the whole file:
■T h i s i s l i n e 1
T h i s i s l i n e 2
T h i s i s l i n e 3
T h i s i s l i n e 4
T h i s i s l i n e 5
Now let’s rewind, kind of like a tape.
Let’s print three lines:
, ■T h i s i s l i n e 1
, T h i s i s l i n e 2
, T h i s i s l i n e 3

2 questions: (1) What is that little black box before “This is line 1” and why is it there? (2) Why don’t my current_line values display?

If I add a space before the #{line_count} in the print_a_line function it will move the numbers over and then I can see them. Like this…
First let’s print the whole file:
■T h i s i s l i n e 1
T h i s i s l i n e 2
T h i s i s l i n e 3
T h i s i s l i n e 4
T h i s i s l i n e 5
Now let’s rewind, kind of like a tape.
Let’s print three lines:
1, ■T h i s i s l i n e 1
2, T h i s i s l i n e 2
3, T h i s i s l i n e 3


#2

That means you created the file in PowerShell so it made a utf-16 file. Delete that file, then make it with your text editor and it should work. If it doesn’t check your text editor’s configuration so that it is saving text as ASCII or utf-8.