DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on

Overwrite a gem method by inserting a module in your inheritance chain

tl;tr;

  • create a module and define the method
  • include it after the gem
  • check it by self.class.ancestors to see the inheritance chain (碙承チェーン)

πŸ€” Situation



class PointsController < ApplicationController
  include MyModule
  include Banken

  def index
    banken_user #=> current_user πŸ€”
  end
end

module MyModule
  def banken_user
    # custom curent_user
  end
end

# gem
module Banken
  def banken_user
    current_user
  end
end


Enter fullscreen mode Exit fullscreen mode

πŸ‘ 解決



class PointsController < ApplicationController
+ include Banken
  include MyModule
- include Banken

  def index
    banken_user #=> custome current_user πŸ‘
  end
end


Enter fullscreen mode Exit fullscreen mode

πŸ”— Parent Note

Top comments (0)