Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6347447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:12:59+00:00 2026-05-24T21:12:59+00:00

in a ruby learning book .I face this code : f = File.new(a.txt, r)

  • 0

in a ruby learning book .I face this code :

f = File.new("a.txt", "r")
while a = f.getc
    puts a.chr
    f.seek(5, IO::SEEK_CUR)
end

author writes that this code produce every fifth character in a file, but I don’t understand why? please explain me line by line.
thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:12:59+00:00Added an answer on May 24, 2026 at 9:12 pm
    f = File.new("a.txt", "r")
    

    This line opens the file in read mode and keeps the file object(as an I/O(Input/Output) stream) in variable f. (See File#new)

    while a = f.getc
    

    getc is a method of class IO which gets one character of the I/O stream at a time and it will give nil, when it meets the end of the I/O stream. So while a = f.getc will loop until the end of file. (See IO#getc)

    puts a.chr
    

    f.getc will give the ASCII value of the character and inorder to get the character from the ASCII value, we apply a.chr(See Integer#chr). I think in Ruby 1.9, we will get the character itself as the output of getc, but for earlier versions, we get the ASCII value as the output. The first getc command reads the first character and moves the position of I/O stream after the first character.

    f.seek(5, IO::SEEK_CUR)
    

    seek is a method of I/O stream which changes the position of the I/O stream by an offset of the first parameter from the second parameter. IO::SEEK_CUR is a constant which gives the current position of the I/O stream. So f.seek(5, IO::SEEK_CUR) moves the position to 5 places from the current position. (See IO#seek)

    This will continue till a = f.getc becomes a false condition(here at the end of file, f.getc becomes nil, which is a falsey value in Ruby(false and nil are the only falsy values in Ruby, all others are truth values))

    Use IRB to study and experiment with Ruby.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following ruby code, from a learning to program book. I understand
I am learning Ruby and found this code sample in some documentation: require 'find'
I am learning ruby from this guide http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:deploying , and I am trying to
I am learning Ruby on Rails so I'm sure I'll find this out sooner
I'm just learning ruby and trying to understand the scope of code executed in
I'm currently learning Ruby and RoR and I stumbled across this declaration: link_to_remote(name, options
I'm learning Ruby (using the Pickaxe book) and I've encountered a little confusion with
I am learning rais from the rails tutorial book: http://ruby.railstutorial.org/chapters/sign-in-sign-out#fnref:9.14 I am working on
I'm learning Ruby and I like playing with irb to discover new features and
I am learning ruby from a book I got called The Pragmatic Programmers Guide

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.