Confused one Ex:20. ri File

So in exercise 20 I’m going through the study drills and it tells me to look up what the “seek” function does. I know that this function will seek to a position of our file, setting it to 0 seeks to the beginning of the file.

What I am confused about is when it starts talking about ri File and ri “File#seek”. I have not been able to find anything online about these things. I saw something about ri being “Ruby Index” but I found no correlation to “seek” or anything.

Would someone mind explaining to me what these two commands do? And if they’re used in command line or in the script themselves?

ri indeed stands for Ruby Index. It’s a program included with Ruby that lets you view the Ruby documentation from your command line.

$ ri File means "give me the documentation for the File class.

$ ri File#seek means "show me the documentation for the #seek method of the File class. # is used in documentation to indicate an instance method.

Here’s an example of what you should see when running $ ri "File#seek":

= File#seek

(from ruby core)
=== Implementation from IO
------------------------------------------------------------------------------
  ios.seek(amount, whence=IO::SEEK_SET)  -> 0

------------------------------------------------------------------------------

Seeks to a given offset anInteger in the stream according to
the value of whence:

  :CUR or IO::SEEK_CUR  | Seeks to _amount_ plus current position
  ----------------------+--------------------------------------------------
  :END or IO::SEEK_END  | Seeks to _amount_ plus end of stream (you
                        | probably want a negative value for _amount_)
  ----------------------+--------------------------------------------------
  :SET or IO::SEEK_SET  | Seeks to the absolute location given by _amount_

Example:

  f = File.new("testfile")
  f.seek(-13, IO::SEEK_END)   #=> 0
  f.readline                  #=> "And so on...\n"
1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.