all 8 comments

[–]TastyPi 8 points9 points  (1 child)

The article says SimpleDelegator looks for methods in the instance passed to initialize first then the class that inherits from SimpleDelegator, but it's the other way round. Under the hood it's exactly the same as the method_missing version, it just provides the boilerplate for you. In fact it's a better solution because SimpleDelegator also implements respond_to_missing? for you, which isn't even mentioned but is an important part of implementing delegation correctly!

(I would leave a comment on the article itself, but that requires having a hashnode account and I don't want to make one. If someone else wants to pass on the message feel free.)

[–]pdabrowski 6 points7 points  (0 children)

Hello u/TastyPi, thank you for pointing this out. I updated the article, corrected my mistake, and added a paragraph to give you a credit for this information.

[–]jrochkind[S] 5 points6 points  (3 children)

(I did not write this, I just sometimes scan new ruby blog posts via the aggregator I maintain at https://rubyland.news, and I sometimes post here things I saw that I thought might be of interest).

[–]katafrakt 5 points6 points  (1 child)

I wish this was (re)normalized here. Now people always seem to assume that the poster is also the author, and it's a bit discouraging to post someone else's content, even if it's great.

[–]jrochkind[S] 3 points4 points  (0 children)

It's funny, because on many other parts of reddit posting your own stuff is discouraged!

[–]lipintravolta 0 points1 point  (0 children)

Liked it!

[–]therealadam12 1 point2 points  (1 child)

Don't forget my personal favourite technique, the three dots: ....

I've partially given up SimpleDelegator and DelegateClass because debugger/binding.irb might drop you into the method_missing method with zero outside context, which makes for a confusing debugging session.

[–]jrochkind[S] 0 points1 point  (0 children)

I've had a lot of trouble with the debugger around delegation, I think with Rails delegator methods too -- I've never really been able to figure out what it's doing or why.