Learn Ruby First

Section Three

In section three, we'll explore how ruby interacts with files and the native operating system. We will also explore the world of regular expressions and see the many ways in which they can be used.

Ruby has a very rich 'File' class compared to other languages. It's easy to start working with the 'File' class, many of the method names describe their actions. Let's create a file in the Home directory called, 'file.txt':

touch file.txt

>> File.file?('file.txt')

# the file? method takes one parameter, the name of a file
>> File.size?('file.txt')
# the size is returned in bytes

Other notable methods include:

  • directory?
  • exists?
  • readable?
  • writeable?

Each of these methods asks their respective question to ruby and return a boolean.

Next, learn how to open a file with Ruby's File class.

At the end of this section there are a series of problems. Try each problem before looking at the solution. It is important to be able to take what you've learned and apply it to the problem at hand. Reading the problem and then looking at the solution won't help you improve as a developer.

results matching ""

    No results matching ""