Challenge 2: require vs require_relative — Solution # lib/greeting.rb module Greeting def self.hello(name) "Hello, #{name}!" end end # main.rb require_relative "lib/greeting" puts Greeting.hello("Philip") # Why require (not require_relative) would be wrong here: # require resolves against Ruby's load path (gems and the standard # library), NOT relative to the current file's location. If main.rb used # require "lib/greeting" instead, it would only work by accident, and # only if the program happened to be run from the exact directory # containing main.rb -- run it from anywhere else, or move main.rb into a # subfolder, and require "lib/greeting" would fail to find the file at # all. require_relative always resolves relative to greeting.rb's actual # location on disk, regardless of the current working directory the # program was launched from, which is exactly the guarantee a local # project file needs. =begin Notes: - Greeting is a module (not a class) since hello is meant to be called directly on Greeting itself, similar to a static/utility method in PHP or Python, not as an instance method on individual objects. - require_relative "lib/greeting" in main.rb resolves relative to main.rb itself, so this works correctly no matter what directory the ruby main.rb command is actually run from. - The .rb extension is optional in both require and require_relative -- "lib/greeting" and "lib/greeting.rb" both resolve to the same file. =end