Paths and Utils in Ruby
Work with file system paths using Pathname, __FILE__, __dir__, and utility modules in Ruby.
Path Constants & Magic Variables
Ruby provides two magic constants for the current file's location: `__FILE__` returns the current file's path, and `__dir__` returns the directory containing it. These are resolved at load time — essential for building paths that work no matter where your script is run from.
1. __FILE__ and __dir__
The foundation of reliable relative paths in Ruby.
2. $LOAD_PATH & require_relative
Control where Ruby looks for required files.
File Class — Path Operations
Ruby's `File` class has a comprehensive set of class methods for path manipulation — no separate path module needed. All methods work cross-platform (Unix `/` and Windows `\`).
1. File.join & File.expand_path
Build and resolve paths safely.
2. Parsing Path Components
Extract basename, dirname, extension, and filename without extension.
3. File Predicates — Existence & Type Checks
Check if a path exists, is a file, directory, or symlink.
Pathname — Object-Oriented Paths
`Pathname` wraps a path string in an object with methods for every `File` and `Dir` operation. It makes complex path operations readable through method chaining instead of nested `File.` calls.
1. Pathname Basics
Create and navigate paths with Pathname.
2. File System Operations with Pathname
Read, write, glob, and iterate with Pathname.
Useful Ruby Utility Methods
Ruby's standard library includes utility modules and methods that every Ruby developer should know — for debugging, benchmarking, deep comparison, and object introspection.
1. pp & inspect — Variable Inspection
Pretty-print any object for debugging.
2. Deep Comparison & Object Identity
== vs eql? vs equal? — know the difference.
3. Benchmark — Measuring Performance
Time code blocks with the standard Benchmark module.
4. ObjectSpace — Runtime Object Introspection
Count live objects and find all instances of a class.