Skip to content

Chapter 7 of 7

Metaprogramming and the Ruby Ecosystem

One of Ruby's most distinctive features is metaprogramming, the ability to write code that writes or modifies code at runtime. The define_method method dynamically defines an instance method on a class, allowing patterns like iterating over a list of command names and generating a method for each one. The method_missing hook intercepts calls to undefined methods, so an object can respond to arbitrary messages by name and arguments; pairing it with respond_to_missing? keeps reflection honest. The send method invokes any method by name, even private ones, which is powerful but should be used carefully; public_send offers a safer alternative that respects visibility.

When a flexible data structure is needed, OpenStruct from the standard ostruct library allows objects to be created with arbitrary attributes defined at runtime, accessed and assigned with normal dot notation. To prevent accidental mutation, Ruby provides the freeze method, which makes an object immutable and raises a FrozenError on any attempt to modify it. A common modern practice is to add a comment at the top of a source file declaring frozen_string_literal: true, which freezes all string literals in that file for performance and safety. Ruby also distinguishes three flavors of equality: == checks value equality and can be overridden, eql? additionally requires the same type and is used by Hash to compare keys, and equal? checks whether two references point to the very same object.

The wider Ruby ecosystem revolves around RubyGems and Bundler. A gem is a packaged Ruby library or application distributed through RubyGems, installable with a simple install command. For real projects, dependencies are listed in a Gemfile, and Bundler resolves and installs the correct versions, recording them in a Gemfile.lock file. The bundle install command installs dependencies and bundle exec runs commands within the bundle context so that the right gem versions are always used. Together, these tools form a mature ecosystem that supports everything from small scripts to large web applications, and they are part of what makes Ruby both expressive and practical for production use.

All chapters
  1. 1Core Data Structures
  2. 2Blocks, Procs, and Lambdas
  3. 3Classes, Inheritance, and Modules
  4. 4Iterators and Functional Collections
  5. 5Exception Handling and File I/O
  6. 6Strings, Patterns, and Operators
  7. 7Metaprogramming and the Ruby Ecosystem

Drill it

Reading is not remembering. These come from the Ruby Programming deck:

Q

What is a Symbol in Ruby?

A Symbol is a lightweight, immutable identifier prefixed with a colon, e.g. :name. Symbols are stored in memory only once, making them more efficient than strin...

Q

How do you create an Array in Ruby?

An Array is an ordered collection created with square brackets or Array.new.Examples:nums = [1, 2, 3]words = %w[hello world]empty = Array.new(5, 0)

Q

How do you create a Hash in Ruby?

A Hash is a collection of key-value pairs.Examples:h = { name: "Alice", age: 30 }h = Hash.new(0)Access values with h[:name].

Q

What is a block in Ruby?

A block is an anonymous chunk of code enclosed in do...end or curly braces { } that can be passed to a method.Example:[1,2,3].each { |n| puts n }