Returns the class or module on which this method is defined. In other words,
meth.owner.instance_methods(false).include?(meth.name) # => true
holds as long as the method is not removed/undefined/replaced, (with private_instance_methods instead of instance_methods if the method is private).
See also Method#receiver
.
(1..3).method(:map).owner #=> Enumerable
Returns the parameter information of this method.
def foo(bar); end method(:foo).parameters #=> [[:req, :bar]] def foo(bar, baz, bat, &blk); end method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:req, :bat], [:block, :blk]] def foo(bar, *args); end method(:foo).parameters #=> [[:req, :bar], [:rest, :args]] def foo(bar, baz, *args, &blk); end method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:rest, :args], [:block, :blk]]
Prevents threads from being added to or removed from the receiving ThreadGroup
.
New threads can still be started in an enclosed ThreadGroup
.
ThreadGroup::Default.enclose #=> #<ThreadGroup:0x4029d914> thr = Thread.new { Thread.stop } #=> #<Thread:0x402a7210 sleep> tg = ThreadGroup.new #=> #<ThreadGroup:0x402752d4> tg.add thr #=> ThreadError: can't move from the enclosed thread group
Returns true
if the thgrp
is enclosed. See also ThreadGroup#enclose
.
Terminates thr
and schedules another thread to be run, returning the terminated Thread
. If this is the main thread, or the last thread, exits the process.
Return the parameters definition of the method or block that the current hook belongs to. Format is the same as for Method#parameters
Returns (and assigns to $_
) the next line from the list of files in ARGV
(or $*
), or from standard input if no files are present on the command line. Returns nil
at end of file. The optional argument specifies the record separator. The separator is included with the contents of each record. A separator of nil
reads the entire contents, and a zero-length separator reads the input one paragraph at a time, where paragraphs are divided by two consecutive newlines. If the first argument is an integer, or optional second argument is given, the returning string would not be longer than the given value in bytes. If multiple filenames are present in ARGV
, gets(nil)
will read the contents one file at a time.
ARGV << "testfile" print while gets
produces:
This is line one This is line two This is line three And so on...
The style of programming using $_
as an implicit parameter is gradually losing favor in the Ruby community.
Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. clone
copies the frozen value state of obj, unless the :freeze
keyword argument is given with a false or true value. See also the discussion under Object#dup
.
class Klass attr_accessor :str end s1 = Klass.new #=> #<Klass:0x401b3a38> s1.str = "Hello" #=> "Hello" s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello"> s2.str[1,4] = "i" #=> "i" s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">" s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
This method may have class-specific behavior. If so, that behavior will be documented under the #initialize_copy
method of the class.
Returns arg converted to a float. Numeric
types are converted directly, and with exception to String
and nil
the rest are converted using arg.to_f
. Converting a String
with invalid characters will result in a ArgumentError
. Converting nil
generates a TypeError
. Exceptions can be suppressed by passing exception: false
.
Float(1) #=> 1.0 Float("123.456") #=> 123.456 Float("123.0_badstring") #=> ArgumentError: invalid value for Float(): "123.0_badstring" Float(nil) #=> TypeError: can't convert nil into Float Float("123.0_badstring", exception: false) #=> nil
Use Kernel#gem
to activate a specific version of gem_name
.
requirements
is a list of version requirements that the specified gem must match, most commonly “= example.version.number”. See Gem::Requirement
for how to specify a version requirement.
If you will be activating the latest version of a gem, there is no need to call Kernel#gem
, Kernel#require
will do the right thing for you.
Kernel#gem
returns true if the gem was activated, otherwise false. If the gem could not be found, didn’t match the version requirements, or a different version was already activated, an exception will be raised.
Kernel#gem
should be called before any require statements (otherwise RubyGems may load a conflicting library version).
Kernel#gem
only loads prerelease versions when prerelease requirements
are given:
gem 'rake', '>= 1.1.a', '< 2'
In older RubyGems versions, the environment variable GEM_SKIP could be used to skip activation of specified gems, for example to test out changes that haven’t been installed yet. Now RubyGems defers to -I and the RUBYLIB environment variable to skip activation of a gem.
Example:
GEM_SKIP=libA:libB ruby -I../libA -I../libB ./mycode.rb
Loads and executes the Ruby program in the file filename.
If the filename is an absolute path (e.g. starts with ‘/’), the file will be loaded directly using the absolute path.
If the filename is an explicit relative path (e.g. starts with ‘./’ or ‘../’), the file will be loaded using the relative path from the current directory.
Otherwise, the file will be searched for in the library directories listed in $LOAD_PATH
($:
). If the file is found in a directory, it will attempt to load the file relative to that directory. If the file is not found in any of the directories in $LOAD_PATH
, the file will be loaded using the relative path from the current directory.
If the file doesn’t exist when there is an attempt to load it, a LoadError
will be raised.
If the optional wrap parameter is true
, the loaded script will be executed under an anonymous module, protecting the calling program’s global namespace. If the optional wrap parameter is a module, the loaded script will be executed under the given module. In no circumstance will any local variables in the loaded file be propagated to the loading environment.
Registers filename to be loaded (using Kernel::require) the first time that module (which may be a String
or a symbol) is accessed.
autoload(:MyModule, "/usr/local/lib/modules/my_module.rb")
Returns filename to be loaded if name is registered as autoload
.
autoload(:B, "b") autoload?(:B) #=> "b"
Returns the current execution stack—an array containing strings in the form file:line
or file:line: in `method'
.
The optional start parameter determines the number of initial stack entries to omit from the top of the stack.
A second optional length
parameter can be used to limit how many entries are returned from the stack.
Returns nil
if start is greater than the size of current execution stack.
Optionally you can pass a range, which will return an array containing the entries within the specified range.
def a(skip) caller(skip) end def b(skip) a(skip) end def c(skip) b(skip) end c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10:in `<main>'"] c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11:in `<main>'"] c(2) #=> ["prog:8:in `c'", "prog:12:in `<main>'"] c(3) #=> ["prog:13:in `<main>'"] c(4) #=> [] c(5) #=> nil
Deprecated. Use block_given? instead.
Repeatedly executes the block.
If no block is given, an enumerator is returned instead.
loop do print "Input: " line = gets break if !line or line =~ /^qQ/ # ... end
StopIteration
raised in the block breaks the loop. In this case, loop returns the “result” value stored in the exception.
enum = Enumerator.new { |y| y << "one" y << "two" :ok } result = loop { puts enum.next } #=> :ok
Returns an array containing elements selected by the block.
With a block given, calls the block with successive elements; returns an array of those elements for which the block returns a truthy value:
(0..9).select {|element| element % 3 == 0 } # => [0, 3, 6, 9] a = {foo: 0, bar: 1, baz: 2}.select {|key, value| key.start_with?('b') } a # => {:bar=>1, :baz=>2}
With no block given, returns an Enumerator.
Related: reject
.
Returns whether for any element object == element
:
(1..4).include?(2) # => true (1..4).include?(5) # => false (1..4).include?('2') # => false %w[a b c d].include?('b') # => true %w[a b c d].include?('2') # => false {foo: 0, bar: 1, baz: 2}.include?(:foo) # => true {foo: 0, bar: 1, baz: 2}.include?('foo') # => false {foo: 0, bar: 1, baz: 2}.include?(0) # => false
Enumerable#member?
is an alias for Enumerable#include?
.
Returns the /etc/passwd information for the user with the given integer uid
.
The information is returned as a Passwd
struct.
If uid
is omitted, the value from Passwd[:uid]
is returned instead.
See the unix manpage for getpwuid(3)
for more detail.
Etc.getpwuid(0) #=> #<struct Etc::Passwd name="root", passwd="x", uid=0, gid=0, gecos="root",dir="/root", shell="/bin/bash">
Returns the /etc/passwd information for the user with specified login name
.
The information is returned as a Passwd
struct.
See the unix manpage for getpwnam(3)
for more detail.
Etc.getpwnam('root') #=> #<struct Etc::Passwd name="root", passwd="x", uid=0, gid=0, gecos="root",dir="/root", shell="/bin/bash">
Returns an entry from the /etc/passwd file.
The first time it is called it opens the file and returns the first entry; each successive call returns the next entry, or nil
if the end of the file has been reached.
To close the file when processing is complete, call ::endpwent
.
Each entry is returned as a Passwd
struct.
Returns information about the group with specified integer group_id
, as found in /etc/group.
The information is returned as a Group
struct.
See the unix manpage for getgrgid(3)
for more detail.
Etc.getgrgid(100) #=> #<struct Etc::Group name="users", passwd="x", gid=100, mem=["meta", "root"]>
Returns information about the group with specified name
, as found in /etc/group.
The information is returned as a Group
struct.
See the unix manpage for getgrnam(3)
for more detail.
Etc.getgrnam('users') #=> #<struct Etc::Group name="users", passwd="x", gid=100, mem=["meta", "root"]>
Returns an entry from the /etc/group file.
The first time it is called it opens the file and returns the first entry; each successive call returns the next entry, or nil
if the end of the file has been reached.
To close the file when processing is complete, call ::endgrent
.
Each entry is returned as a Group
struct
Allocate size
bytes of memory and return the integer memory address for the allocated memory.