in foo | bar
Return the best specification that contains the file matching path
amongst the specs that are not activated.
Return the best specification in the record that contains the file matching path
amongst the specs that are not activated.
Calls the block with each repeated permutation of length n
of the elements of self
; each permutation is an Array
; returns self
. The order of the permutations is indeterminate.
When a block and a positive Integer
argument n
are given, calls the block with each n
-tuple repeated permutation of the elements of self
. The number of permutations is self.size**n
.
n
= 1:
a = [0, 1, 2] a.repeated_permutation(1) {|permutation| p permutation }
Output:
[0] [1] [2]
n
= 2:
a.repeated_permutation(2) {|permutation| p permutation }
Output:
[0, 0] [0, 1] [0, 2] [1, 0] [1, 1] [1, 2] [2, 0] [2, 1] [2, 2]
If n
is zero, calls the block once with an empty Array
.
If n
is negative, does not call the block:
a.repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
Returns a new Enumerator
if no block given:
a = [0, 1, 2] a.repeated_permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
Using Enumerators, it’s convenient to show the permutations and counts for some values of n
:
e = a.repeated_permutation(0) e.size # => 1 e.to_a # => [[]] e = a.repeated_permutation(1) e.size # => 3 e.to_a # => [[0], [1], [2]] e = a.repeated_permutation(2) e.size # => 9 e.to_a # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
Imports methods from modules. Unlike Module#include
, Refinement#import_methods
copies methods and adds them into the refinement, so the refinement is activated in the imported methods.
Note that due to method copying, only methods defined in Ruby code can be imported.
module StrUtils def indent(level) ' ' * level + self end end module M refine String do import_methods StrUtils end end using M "foo".indent(3) #=> " foo" module M refine String do import_methods Enumerable # Can't import method which is not defined with Ruby code: Enumerable#drop end end
Returns whether ASCII-compatible or not.
Encoding::UTF_8.ascii_compatible? #=> true Encoding::UTF_16BE.ascii_compatible? #=> false
Returns an array of the names of singleton methods for obj. If the optional all parameter is true, the list will include methods in modules included in obj. Only public and protected singleton methods are returned.
module Other def three() end end class Single def Single.four() end end a = Single.new def a.one() end class << a include Other def two() end end Single.singleton_methods #=> [:four] a.singleton_methods(false) #=> [:two, :one] a.singleton_methods #=> [:two, :one, :three]
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.