Returns the list of protected methods accessible to obj. If the all parameter is set to false
, only those methods in the receiver will be listed.
Returns the list of public methods accessible to obj. If the all parameter is set to false
, only those methods in the receiver will be listed.
Similar to method, searches public method only.
Similar to method, searches singleton method only.
class Demo def initialize(n) @iv = n end def hello() "Hello, @iv = #{@iv}" end end k = Demo.new(99) def k.hi "Hi, @iv = #{@iv}" end m = k.singleton_method(:hi) m.call #=> "Hi, @iv = 99" m = k.singleton_method(:hello) #=> NameError
Invoked as a callback whenever an instance method is added to the receiver.
module Chatty def self.method_added(method_name) puts "Adding #{method_name.inspect}" end def self.some_class_method() end def some_instance_method() end end
produces:
Adding :some_instance_method
Invoked as a callback whenever an instance method is removed from the receiver.
module Chatty def self.method_removed(method_name) puts "Removing #{method_name.inspect}" end def self.some_class_method() end def some_instance_method() end class << self remove_method :some_class_method end remove_method :some_instance_method end
produces:
Removing :some_instance_method
Invoked as a callback whenever an instance method is undefined from the receiver.
module Chatty def self.method_undefined(method_name) puts "Undefining #{method_name.inspect}" end def self.some_class_method() end def some_instance_method() end class << self undef_method :some_class_method end undef_method :some_instance_method end
produces:
Undefining :some_instance_method
Returns an array containing the names of the public and protected instance methods in the receiver. For a module, these are the public and protected methods; for a class, they are the instance (not singleton) methods. If the optional parameter is false
, the methods of any ancestors are not included.
module A def method1() end end class B include A def method2() end end class C < B def method3() end end A.instance_methods(false) #=> [:method1] B.instance_methods(false) #=> [:method2] B.instance_methods(true).include?(:method1) #=> true C.instance_methods(false) #=> [:method3] C.instance_methods.include?(:method2) #=> true
Note that method visibility changes in the current class, as well as aliases, are considered as methods of the current class by this method:
class C < B alias method4 method2 protected :method2 end C.instance_methods(false).sort #=> [:method2, :method3, :method4]
Returns an UnboundMethod
representing the given instance method in mod.
class Interpreter def do_a() print "there, "; end def do_d() print "Hello "; end def do_e() print "!\n"; end def do_v() print "Dave"; end Dispatcher = { "a" => instance_method(:do_a), "d" => instance_method(:do_d), "e" => instance_method(:do_e), "v" => instance_method(:do_v) } def interpret(string) string.each_char {|b| Dispatcher[b].bind(self).call } end end interpreter = Interpreter.new interpreter.interpret('dave')
produces:
Hello there, Dave!
Defines an instance method in the receiver. The method parameter can be a Proc
, a Method
or an UnboundMethod
object. If a block is specified, it is used as the method body. If a block or the method parameter has parameters, they’re used as method parameters. This block is evaluated using instance_eval
.
class A def fred puts "In Fred" end def create_method(name, &block) self.class.define_method(name, &block) end define_method(:wilma) { puts "Charge it!" } define_method(:flint) {|name| puts "I'm #{name}!"} end class B < A define_method(:barney, instance_method(:fred)) end a = B.new a.barney a.wilma a.flint('Dino') a.create_method(:betty) { p self } a.betty
produces:
In Fred Charge it! I'm Dino! #<B:0x401b39e8>
Removes the method identified by symbol from the current class. For an example, see Module#undef_method
. String
arguments are converted to symbols.
Prevents the current class from responding to calls to the named method. Contrast this with remove_method
, which deletes the method from the particular class; Ruby will still search superclasses and mixed-in modules for a possible receiver. String
arguments are converted to symbols.
class Parent def hello puts "In parent" end end class Child < Parent def hello puts "In child" end end c = Child.new c.hello class Child remove_method :hello # remove from child, still in parent end c.hello class Child undef_method :hello # prevent any calls to 'hello' end c.hello
produces:
In child In parent prog.rb:23: undefined method 'hello' for #<Child:0x401b3bb4> (NoMethodError)
Makes new_name a new copy of the method old_name. This can be used to retain access to methods that are overridden.
module Mod alias_method :orig_exit, :exit #=> :orig_exit def exit(code=0) puts "Exiting with code #{code}" orig_exit(code) end end include Mod exit(99)
produces:
Exiting with code 99
Returns true
if the named method is defined by mod. If inherit is set, the lookup will also search mod’s ancestors. Public and protected methods are matched. String
arguments are converted to symbols.
module A def method1() end def protected_method1() end protected :protected_method1 end class B def method2() end def private_method2() end private :private_method2 end class C < B include A def method3() end end A.method_defined? :method1 #=> true C.method_defined? "method1" #=> true C.method_defined? "method2" #=> true C.method_defined? "method2", true #=> true C.method_defined? "method2", false #=> false C.method_defined? "method3" #=> true C.method_defined? "protected_method1" #=> true C.method_defined? "method4" #=> false C.method_defined? "private_method2" #=> false
Returns a DateTime
whose value is the same as self
:
Date.new(2001, 2, 3).to_datetime # => #<DateTime: 2001-02-03T00:00:00+00:00>
Returns self.
Returns a DateTime
object which denotes self.
Returns true for IPv4-compatible IPv6 address (::/80). It returns false otherwise.
Calls WIN32OLE#invoke
method.
Returns the array of WIN32OLE::Method
object. The element is OLE method of WIN32OLE
object.
excel = WIN32OLE.new('Excel.Application') methods = excel.ole_methods
Returns WIN32OLE::Method
object corresponding with method specified by 1st argument.
excel = WIN32OLE.new('Excel.Application') method = excel.ole_method_help('Quit')
Invoked by Ruby when obj is sent a message it cannot handle. symbol is the symbol for the method called, and args are any arguments that were passed to it. By default, the interpreter raises an error when this method is called. However, it is possible to override the method to provide more dynamic behavior. If it is decided that a particular method should not be handled, then super should be called, so that ancestors can pick up the missing method. The example below creates a class Roman
, which responds to methods with names consisting of roman numerals, returning the corresponding integer values.
class Roman def roman_to_int(str) # ... end def method_missing(symbol, *args) str = symbol.id2name begin roman_to_int(str) rescue super(symbol, *args) end end end r = Roman.new r.iv #=> 4 r.xxiii #=> 23 r.mm #=> 2000 r.foo #=> NoMethodError
Returns the methods available to this delegate object as the union of this object’s and _getobj_ public methods.
Returns the methods available to this delegate object as the union of this object’s and _getobj_ protected methods.