Get the user ID by the name. If the user is not found, ArgumentError
will be raised.
Process::UID.from_name("root") #=> 0 Process::UID.from_name("nosuchuser") #=> can't find user for nosuchuser (ArgumentError)
Get the group ID by the name. If the group is not found, ArgumentError
will be raised.
Process::GID.from_name("wheel") #=> 0 Process::GID.from_name("nosuchgroup") #=> can't find group for nosuchgroup (ArgumentError)
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.
The index to insert activated gem paths into the $LOAD_PATH. The activated gem’s paths are inserted before site lib directory by default.
Add a list of paths to the $LOAD_PATH at the proper place.
With a block given, calls the block with each repeated permutation of length size
of the elements of self
; each permutation is an array; returns self
. The order of the permutations is indeterminate.
If a positive integer argument size
is given, calls the block with each size
-tuple repeated permutation of the elements of self
. The number of permutations is self.size**size
.
Examples:
size
is 1:
p = [] [0, 1, 2].repeated_permutation(1) {|permutation| p.push(permutation) } p # => [[0], [1], [2]]
size
is 2:
p = [] [0, 1, 2].repeated_permutation(2) {|permutation| p.push(permutation) } p # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
If size
is zero, calls the block once with an empty array.
If size
is negative, does not call the block:
[0, 1, 2].repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
With no block given, returns a new Enumerator
.
Related: see Methods for Combining.
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