Skip to content

Instantly share code, notes, and snippets.

@rafaelsales
Last active June 22, 2016 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelsales/86ca62c45108abe7162a87813fea8e7e to your computer and use it in GitHub Desktop.
Save rafaelsales/86ca62c45108abe7162a87813fea8e7e to your computer and use it in GitHub Desktop.
What I dislike in SimpleDelegator
class Funky
def good_method
"I'm good"
end
def method_missing(*args)
"I'm bad"
end
end
> Funky.new.good_method
=> "I'm good"
Funky.new.bad_method
=> "I'm bad"
> SimpleDelegator.new(Funky.new).good_method
=> "I'm good"
> SimpleDelegator.new(Funky.new).bad_method
NoMethodError: undefined method `bad_method' for #<Funky:0x007f88c593e6d0>
class MyDecorator < SimpleDelegator
def name
"** #{super.upcase} **"
end
end
> MyDecorator.new(OpenStruct.new(name: 'Rafael')).name
=> "** RAFAEL **"
> MyDecorator.new(OpenStruct.new(name: 'Rafael'))&.name
=> "** RAFAEL **"
> MyDecorator.new(OpenStruct.new(name: 'Rafael')).try(:name)
=> "Rafael"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment