From: "Jesús Gabriel y Galán" Date: 2012-05-24T17:51:54+09:00 Subject: Re: Simple IO question On Thu, May 24, 2012 at 10:34 AM, Kaye Ng wrote: > This prints every fifth character in a file: > > f = File.new("test.txt", "r") > while a = f.getc >  puts a.chr >  f.seek(5, IO::SEEK_CUR) > end > > I don't quite get the meaning of it.  Is it like, "while the variable > 'a' has a value or contains something, it should be printed onto the > screen"? > > Why is it not "while a == f.getc" ? ( == instead of = ) > > What does getc here mean anyway? I know it reads a single character, > correct? a = f.getc assigns to the variable a the next character from the file f. You need to know also, that f.getc will return nil when the end of file is reached. The assignment returns the assigned value as the result of the expression. This code uses this value as the condition of the while. So it keeps reading a char from the file and assigning to variable a, until f.getc returns nil, at which point a gets assigned nil, the expression returns nil, the while condition is not met and the loop ends. Jesus.