[Talking-Ruby] SimpleDelegator: A concrete implementation of Delegator in Ruby

class SimpleDelegator

A concrete implementation of Delegator, this class provides the means to delegate all supported method calls to the object passed into the constructor and even to change the object being delegated to at a later time with #__setobj__.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class User
def born_on
Date.new(1989, 9, 10)
end
end

require 'delegate'

class UserDecorator < SimpleDelegator
def birth_year
born_on.year
end
end

decorated_user = UserDecorator.new(User.new)
decorated_user.birth_year #=> 1989
decorated_user.__getobj__ #=> #<User: ...>

A SimpleDelegator instance can take advantage of the fact that SimpleDelegator is a subclass of Delegator to call super to have methods called on the object being delegated to.

1
2
3
4
5
6
7
class SuperArray < SimpleDelegator
def [](*args)
super + 1
end
end

SuperArray.new([1])[0] #=> 2

Here’s a simple example that takes advantage of the fact that SimpleDelegator’s delegation object can be changed at any time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Stats
def initialize
@source = SimpleDelegator.new([])
end

def stats(records)
@source.__setobj__(records)

"Elements: #{@source.size}\n" +
" Non-Nil: #{@source.compact.size}\n" +
" Unique: #{@source.uniq.size}\n"
end
end

s = Stats.new
puts s.stats(%w{James Edward Gray II})
puts
puts s.stats([1, 2, 3, nil, 4, 5, 1, 2])

Prints:

1
2
3
4
5
6
7
Elements:  4
Non-Nil: 4
Unique: 4

Elements: 8
Non-Nil: 7
Unique: 6

Public Instance Methods

getobj() { || … }

Returns the current object method calls are being delegated to.

setobj(obj)

Changes the delegate object to obj.

It’s important to note that this does not cause SimpleDelegator’s methods to change. Because of this, you probably only want to change delegation to objects of the same type as the original delegate.

Here’s an example of changing the delegation object.

1
2
3
4
names = SimpleDelegator.new(%w{James Edward Gray II})
puts names[1] # => Edward
names.__setobj__(%w{Gavin Sinclair})
puts names[1] # => Sinclair

References

[1] class SimpleDelegator - Documentation for Ruby 3.0.0 - https://docs.ruby-lang.org/en/3.0.0/SimpleDelegator.html

[2] Ruby | SimpleDelegator Methods. There are a good number of posts about… | by Derek Dyer | Medium - https://medium.com/@derek_dyer/ruby-simpledelegator-methods-c799e07ae108

[3] Ruby’s Hidden Gems: Delegator and Forwardable | AppSignal Blog - https://blog.appsignal.com/2019/04/30/ruby-magic-hidden-gems-delegator-forwardable.html

[4] Module: Forwardable (Ruby 3.0.0) - https://ruby-doc.org/stdlib-3.0.0/libdoc/forwardable/rdoc/Forwardable.html

[5] Module | Ruby on Rails 6.1.4 Class - https://api.rubyonrails.org/classes/Module.html#method-i-delegate

[6] saturnflyer/casting: Delegate methods in Ruby and preserve self. Add behaviors to your objects without altering their superclass hierarchy. - https://github.com/saturnflyer/casting

[7] How To Delegate Methods in Ruby & Ruby on Rails - RubyGuides - https://www.rubyguides.com/2018/10/delegate-methods-in-ruby/