Skip to content

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

Description

@ohta-rh

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を変えたくない場合とかにつかえそうです。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions