What does this snippet mean?


Here’s a code snippet. What it actually does?

def rewind(f)

f.seek(0, IO::SEEK_SET)


It’s from here : Learn Ruby the Hard Way

Any help would be appreciated.

Peace be upon you –

Junayeed Ahnaf N.

Twitter - @Nirjhor http://twitter.com/nirjhor

On 10/14/2011 12:15, Junayeed Ahnaf N. wrote:

f.seek(0, IO::SEEK_SET)


The parameter f is assumed to be an IO object of some kind. Those
objects include a method named seek that allows you to move the file
pointer for the IO object around without performing read or write
operations in order to do it.

In case you don’t know what a file pointer is, it is the location in a
file’s data from which read or write operations will start. When the
file pointer is 0, it is at the beginning of the file’s data.

The rewind method above is setting the file pointer using an absolute
position of 0. The IO::SEEK_SET argument to the seek method is what
tells seek that the first argument of 0 is absolute.

For more information, see the documentation for IO#seek:



On Fri, Oct 14, 2011 at 13:15, Junayeed Ahnaf N.
[email protected] wrote:

Here’s a code snippet. What it actually does?

def rewind(f)
f.seek(0, IO::SEEK_SET)

Jeremy already gave you that answer, but I’ll help you find many more

When trying to figure out what something does, a great way to find out
is to Google together as many of the significant words as you can. In
this case, if you Google seek and IO::SEEK_SET, the very first hit is
http://rubylearning.com/satishtalim/read_write_files.html which
explains all about reading and writing text files – in Ruby. If you
omit the IO part, you wind up first with hits about other languages –
but at least you would probably be able to figure out the general idea
of what Ruby’s “seek” probably does.

(And even that’s without including Ruby as a search term. Googling
seek, SEEK_SET, and Ruby, gets you Satish’s site again.)

Google-Fu is a very important skill to develop…