Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
what does .seek means in ruby

what is the purpose of f.seek(0) in this script? Why do we need to rewind(current_file), if the file has already been opened by the program?

input_file = ARGV[0]

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.readline()}"
end

current_file = File.open(input_file)

puts "First Let's print the whole file:"
puts # a blank line

print_all(current_file)

puts "Now Let's rewind, kind of like a tape"

rewind(current_file)

puts "Let's print the first line:"

current_line = 1
print_a_line(current_line, current_file)
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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 also create another method called almost_rewind and write:

def almost_rewind(f)
  f.seek(-10, IO::SEEK_END)
end

This would go 10 positions backwards in your stream, starting from the END of the stream.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda