I try my best, but English is not my mother-language and the additional
abstraction effort may render my explanation complicated.
all_there_is << File.read(f) if File::file?(f)
all_there_is is an Array. As such, it accepts objects of any arbitrary
type (or class) as its elements. The shift-operator “<<” is doing the
same as the push() method. If you know container-classes from other
programming languages, this may sound familiar. In the above line of
code, an object is pushed “on the end of the array”, meaning “appended”
to it.
The object itself is the result of the call to the read() method of the
File-class. read() takes a String as an argument. Here, the String “f”
is the name of a file-system object (file, directory, link, …
whatever…, pipe, etc). File.read (or File::read) will return the
content of a text-file as an object of type String. This object is then
appended to the Array as a new element.
It would not make sense to read all kinds of file-system objects in this
way, hence the restriction “if File::file?(f)”. Only if the file with
the file-name “f” is an “ordinary” file, the previously noted code is
executed. In a real-world program, this restriction is insufficient. But
in this example it has to serve merely as a reminder: “be careful!”
In Ruby, you can write the if-clause (or unless, while, until) after the
one line of code (or block) that it shall control. This notation allows
a
more straight-forward reflection which often is more natural to the
human brain, than the other way round. Once coded, there is no
difference. I even admit, that such an “inverted” control-structure can
initially confuse a reader.
all_there_is.each_with_index {|f, i| puts “file #%d:\n----%s” %[i, f]}
This line just proves, that the previously read file-content is really
stored in the array all_there_is.
For each element in the array, the content of the element (the content
of a file) and its index in the array are retained, then used to form a
textual output. See “ri format” or “ri sprintf” if you are not familiar
with the printf() syntax.
Bye,
Michael