Menu
Coddy logo textTech

Working With Files

Part of the Object Oriented Programming section of Coddy's Ruby journey. Lesson 1 of 57.

Real programs are split into several files. A Ruby file loads another one with require_relative, followed by the file's path relative to the current file, usually without the .rb ending. After that line, the methods and classes of the loaded file can be used:

# greeting.rb
def greeting(name)
  "Hello, #{name}!"
end

# main.rb
require_relative 'greeting'
puts greeting("Ada")

Running ruby main.rb prints Hello, Ada!: main.rb loads greeting.rb and then calls its method.

The program starts in the file you run, here main.rb, and Ruby executes it from top to bottom. require_relative runs the other file at that point, and only the first time: a second require_relative 'greeting' returns false and does nothing. A name can only be used after the file that defines it has been loaded:

# main.rb
# puts greeting("Bo")          # NoMethodError: greeting.rb is not loaded yet
require_relative 'greeting'     # runs greeting.rb, returns true
require_relative 'greeting'     # already loaded, returns false
puts greeting("Bo")

The usual layout is one class per file, with the file named after the class in snake_case: class Dog lives in dog.rb, class BankAccount in bank_account.rb. In this section's challenges, main.rb holds the supplied input/output code and starts with one require_relative line per file, in load order: a class that another class builds on is loaded first.

dog.rb            # class Dog ... end
bank_account.rb   # class BankAccount ... end
functions.rb      # helper methods
main.rb           # require_relative lines, then the input/output code

What a file shares depends on the kind of name. Methods defined with def at the top of a file, classes and constants can be used by every file loaded after it. A local variable belongs to its own file only:

# settings.rb
limit = 3                  # local variable: only settings.rb sees it
MAX_ITEMS = 3              # constant: visible after loading
def limit_text
  "at most #{MAX_ITEMS}"
end

# main.rb
require_relative 'settings'
puts limit_text            # at most 3
puts MAX_ITEMS             # 3
# puts limit               # NameError: undefined local variable
challenge icon

Challenge

Easy

The program has two files. main.rb loads greeting.rb, reads names and prints the result of greeting(name). Complete greeting in greeting.rb so it returns Hello, Ada! for the name Ada, or Hello, friend! when the name is empty.

Your code goes in greeting.rb. main.rb holds the supplied input/output code and cannot be edited.

Try it yourself

require_relative 'greeting'

# Supplied input/output code: keep it as it is
input = []
while (line = gets)
  input << line.chomp
end
input.each { |name| puts greeting(name) }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming

Practice on your own: Online Ruby compiler