DEV Community

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

Posted on

3 1

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay