Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/module.rb

Instance Method Summary collapse

Instance Method Details

#method_visibility(method) ⇒ Object

:nodoc:



30
31
32
33
34
35
36
37
38
# File 'lib/ext/module.rb', line 30

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

#redefine_method(method, &block) ⇒ Object

Replaces the existing method definition, if there is one, with the passed block as its body.



17
18
19
20
21
22
# File 'lib/ext/module.rb', line 17

def redefine_method(method, &block)
  visibility = method_visibility(method)
  silence_redefinition_of_method(method)
  define_method(method, &block)
  send(visibility, method)
end

#redefine_singleton_method(method, &block) ⇒ Object

Replaces the existing singleton method definition, if there is one, with the passed block as its body.



26
27
28
# File 'lib/ext/module.rb', line 26

def redefine_singleton_method(method, &block)
  singleton_class.redefine_method(method, &block)
end

#silence_redefinition_of_method(method) ⇒ Object

Marks the named method as intended to be redefined, if it exists. Suppresses the Ruby method redefinition warning. Prefer #redefine_method where possible.



7
8
9
10
11
12
13
# File 'lib/ext/module.rb', line 7

def silence_redefinition_of_method(method)
  return unless 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