Last active
June 22, 2016 21:29
-
-
Save rafaelsales/86ca62c45108abe7162a87813fea8e7e to your computer and use it in GitHub Desktop.
What I dislike in SimpleDelegator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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