Looks up all hostnames for address
.
Closes the file. If unlink_now
is true, then the file will be unlinked (deleted) after closing. Of course, you can choose to later call unlink
if you do not unlink it now.
If you don’t explicitly unlink the temporary file, the removal will be delayed until the object is finalized.
Closes and unlinks (deletes) the file. Has the same effect as called close(true)
.
Returns the parameter information of this proc. If the lambda keyword is provided and not nil, treats the proc as a lambda if true and as a non-lambda if false.
prc = proc{|x, y=42, *other|} prc.parameters #=> [[:opt, :x], [:opt, :y], [:rest, :other]] prc = lambda{|x, y=42, *other|} prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]] prc = proc{|x, y=42, *other|} prc.parameters(lambda: true) #=> [[:req, :x], [:opt, :y], [:rest, :other]] prc = lambda{|x, y=42, *other|} prc.parameters(lambda: false) #=> [[:opt, :x], [:opt, :y], [:rest, :other]]
Returns a clone of this method.
class A def foo return "bar" end end m = A.new.method(:foo) m.call # => "bar" n = m.clone.call # => "bar"
Returns the bound receiver of the method object.
(1..3).method(:map).receiver # => 1..3
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]]
Returns a clone of this method.
class A def foo return "bar" end end m = A.new.method(:foo) m.call # => "bar" n = m.clone.call # => "bar"
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.
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 =~ /^q/i # ... 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 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. 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 _const_ (which may be a String or a symbol) is accessed. autoload(:MyModule, "/usr/local/lib/modules/my_module.rb")
If const is defined as autoload, the file name to be loaded is replaced with filename. If const is defined but not as autoload, does nothing.
Returns filename to be loaded if name is registered as autoload
in the current namespace or one of its ancestors.
autoload(:B, "b") autoload?(:B) #=> "b" module C autoload(:D, "d") autoload?(:D) #=> "d" autoload?(:B) #=> nil end class E autoload(:F, "f") autoload?(:F) #=> "f" autoload?(:B) #=> "b" end
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.