Sha256: 7a3da69e50172b016a60aaa772715ecd37088cbdcb2625e580dc0013d2e75e8a

Contents?: true

Size: 1.2 KB

Versions: 183

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

class Module
  # Marks the named method as intended to be redefined, if it exists.
  # Suppresses the Ruby method redefinition warning. Prefer
  # #redefine_method where possible.
  def silence_redefinition_of_method(method)
    if method_defined?(method) || private_method_defined?(method)
      # This suppresses the "method redefined" warning; the self-alias
      # looks odd, but means we don't need to generate a unique name
      alias_method method, method
    end
  end

  # Replaces the existing method definition, if there is one, with the passed
  # block as its body.
  def redefine_method(method, &block)
    visibility = method_visibility(method)
    silence_redefinition_of_method(method)
    define_method(method, &block)
    send(visibility, method)
  end

  # Replaces the existing singleton method definition, if there is one, with
  # the passed block as its body.
  def redefine_singleton_method(method, &block)
    singleton_class.redefine_method(method, &block)
  end

  def method_visibility(method) # :nodoc:
    case
    when private_method_defined?(method)
      :private
    when protected_method_defined?(method)
      :protected
    else
      :public
    end
  end
end

Version data entries

183 entries across 175 versions & 23 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/module/redefine_method.rb
activesupport-8.0.1 lib/active_support/core_ext/module/redefine_method.rb
activesupport-8.0.0.1 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.2.2.1 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.1.5.1 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.0.8.7 lib/active_support/core_ext/module/redefine_method.rb
activesupport-8.0.0 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.2.2 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.1.5 lib/active_support/core_ext/module/redefine_method.rb
activesupport-8.0.0.rc2 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.2.1.2 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.1.4.2 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.0.8.6 lib/active_support/core_ext/module/redefine_method.rb
activesupport-6.1.7.10 lib/active_support/core_ext/module/redefine_method.rb
activesupport-8.0.0.rc1 lib/active_support/core_ext/module/redefine_method.rb
activesupport-6.1.7.9 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.2.1.1 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.1.4.1 lib/active_support/core_ext/module/redefine_method.rb
activesupport-7.0.8.5 lib/active_support/core_ext/module/redefine_method.rb
activesupport-8.0.0.beta1 lib/active_support/core_ext/module/redefine_method.rb