Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
90: Method Objects and UnboundMethod
Up until now, we've treated methods as things you call. You send a message to an object, the method executes, and you get a result. But in Ruby, methods can be treated as first-class objects. This means you can grab a method, put it in a variable, pass it as an argument to another function, or even store it in an array. It's an incredibly powerful tool for building flexible plugins or dynamic dispatch systems.
Turning a method into a handle
Let's say we're building a simple PriceCalculator. Depending on the customer's region, we might want to apply different tax strategies. Instead of writing a giant if/else block every time we need to calculate a total, I want to be able to "pick" a method and pass it around.
class PriceCalculator
def flat_tax(amount)
amount + 5.0
end
def percentage_tax(amount)
amount * 1.15
end
end
calc = PriceCalculator.new
# Instead of calling the method, we capture it
tax_strategy = calc.method(:percentage_tax)
puts tax_strategy.call(100) # => 115.0
By calling method(:percentage_tax), I've created a Method object. Notice that I used .call(100) instead of just calling the method name. This tax_strategy variable now holds a reference to that specific method, bound to that specific calc instance.
The "Unbound" trap
Now, here is where things get a bit weirder. Sometimes you don't want a method tied to a specific instance; you want the definition of the method from the class itself. This is where instance_method comes in. I'll show you where I usually trip up when I first dive back into this.
I might think, "I don't have an instance of PriceCalculator yet, but I know I want to use the flat_tax logic." So, I try this:
# Attempting to grab the method from the class
generic_tax = PriceCalculator.instance_method(:flat_tax)
# I try to use it immediately...
generic_tax.call(100)
# RuntimeError: UnboundMethod cannot be called without a target receiver
I just hit a wall. Why did that fail? Because instance_method returns an UnboundMethod. Think of it like a recipe that hasn't been assigned to a chef. The logic is there, but there's no self for the method to act upon. Since flat_tax is an instance method, it expects to belong to an object of PriceCalculator.
Binding the logic to an object
To fix this, we have to "bind" the UnboundMethod to an actual instance. This is useful when you have a pool of different objects and you want to apply the same piece of logic to all of them, regardless of how those objects were initialized.
calc_a = PriceCalculator.new
calc_b = PriceCalculator.new
# Grab the unbound method from the class
unbound_tax = PriceCalculator.instance_method(:flat_tax)
# Bind it to calc_a and call it
puts unbound_tax.bind(calc_a).call(100) # => 105.0
# Bind the exact same method object to calc_b
puts unbound_tax.bind(calc_b).call(100) # => 105.0
In this specific example, flat_tax doesn't use any internal state (like @tax_rate), so binding it to different instances doesn't change the result. However, if the method accessed self or an instance variable, .bind() would ensure the method operates on the data inside that specific object. It's a level of indirection that lets you separate the "what to do" from the "who to do it to."
📋 Practical Task
Building a Dynamic Data Sanitizer
Create a class called DataSanitizer with three methods: strip_whitespace, downcase_all, and remove_digits. Each method should take a string and return the modified version.
- Create an instance of
DataSanitizer. - Use the
methodmethod to capturestrip_whitespaceandremove_digitsinto an array calledpipeline. - Given a dirty string (e.g.,
" User123 "), iterate through yourpipelinearray, calling each method object on the result of the previous one. - Finally, use
DataSanitizer.instance_method(:downcase_all)to get anUnboundMethod, bind it to your sanitizer instance, and apply it to the final result. - Print the final cleaned string.
There are no comments for now.