Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alias_method_chainの多発を防ぐ為に+仕様変更対応 #83

Open
ohta-rh opened this issue Mar 29, 2014 · 0 comments
Open

alias_method_chainの多発を防ぐ為に+仕様変更対応 #83

ohta-rh opened this issue Mar 29, 2014 · 0 comments
Labels

Comments

@ohta-rh
Copy link
Contributor

ohta-rh commented Mar 29, 2014

prependという機能がruby2.0から入っています。

class Hoge
   attr_accessor :hoge_flag # 仕様変更で急に動作を変えるケースが出たのでフラグを追加した
   def hoge
      p "original"
   end

  module PrependHoge
    def hoge
       if hoge_flag
         p "hoggable"
       else
         super # 既存の処理を呼ぶ
       end
    end
  end
  prepend PrependHoge
end



Hoge.new.hoge
=> "original"

hoge = Hoge.new
hoge.hoge_flag = true
hoge.hoge
=> "hoggable"

仕様変更が突然来たけど、I/Fを変えたくない場合とかにつかえそうです。

@ohta-rh ohta-rh added the Ruby label Mar 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant