File: C:/Ruby27-x64/share/doc/ruby/html/Kernel.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>module Kernel - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
var index_rel_prefix = "./";
</script>
<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>
<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="module">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="./index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="./table_of_contents.html#pages">Pages</a>
<a href="./table_of_contents.html#classes">Classes</a>
<a href="./table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-URI">::URI</a>
<li ><a href="#method-c-open">::open</a>
<li ><a href="#method-c-open_uri_original_open">::open_uri_original_open</a>
<li ><a href="#method-c-pp">::pp</a>
<li ><a href="#method-i-Array">#Array</a>
<li ><a href="#method-i-BigDecimal">#BigDecimal</a>
<li ><a href="#method-i-Complex">#Complex</a>
<li ><a href="#method-i-Float">#Float</a>
<li ><a href="#method-i-Hash">#Hash</a>
<li ><a href="#method-i-Integer">#Integer</a>
<li ><a href="#method-i-JSON">#JSON</a>
<li ><a href="#method-i-Pathname">#Pathname</a>
<li ><a href="#method-i-Rational">#Rational</a>
<li ><a href="#method-i-String">#String</a>
<li ><a href="#method-i-URI">#URI</a>
<li ><a href="#method-i-__callee__">#__callee__</a>
<li ><a href="#method-i-__dir__">#__dir__</a>
<li ><a href="#method-i-__method__">#__method__</a>
<li ><a href="#method-i-60">#`</a>
<li ><a href="#method-i-abort">#abort</a>
<li ><a href="#method-i-at_exit">#at_exit</a>
<li ><a href="#method-i-autoload">#autoload</a>
<li ><a href="#method-i-autoload-3F">#autoload?</a>
<li ><a href="#method-i-binding">#binding</a>
<li ><a href="#method-i-block_given-3F">#block_given?</a>
<li ><a href="#method-i-callcc">#callcc</a>
<li ><a href="#method-i-caller">#caller</a>
<li ><a href="#method-i-caller_locations">#caller_locations</a>
<li ><a href="#method-i-catch">#catch</a>
<li ><a href="#method-i-chomp">#chomp</a>
<li ><a href="#method-i-chop">#chop</a>
<li ><a href="#method-i-eval">#eval</a>
<li ><a href="#method-i-exec">#exec</a>
<li ><a href="#method-i-exit">#exit</a>
<li ><a href="#method-i-exit-21">#exit!</a>
<li ><a href="#method-i-fail">#fail</a>
<li ><a href="#method-i-fork">#fork</a>
<li ><a href="#method-i-format">#format</a>
<li ><a href="#method-i-gem">#gem</a>
<li ><a href="#method-i-gem_original_require">#gem_original_require</a>
<li ><a href="#method-i-gets">#gets</a>
<li ><a href="#method-i-global_variables">#global_variables</a>
<li ><a href="#method-i-gsub">#gsub</a>
<li ><a href="#method-i-iterator-3F">#iterator?</a>
<li ><a href="#method-i-j">#j</a>
<li ><a href="#method-i-jj">#jj</a>
<li ><a href="#method-i-lambda">#lambda</a>
<li ><a href="#method-i-load">#load</a>
<li ><a href="#method-i-local_variables">#local_variables</a>
<li ><a href="#method-i-loop">#loop</a>
<li ><a href="#method-i-open">#open</a>
<li ><a href="#method-i-open_uri_original_open">#open_uri_original_open</a>
<li ><a href="#method-i-p">#p</a>
<li ><a href="#method-i-pp">#pp</a>
<li ><a href="#method-i-pretty_inspect">#pretty_inspect</a>
<li ><a href="#method-i-print">#print</a>
<li ><a href="#method-i-printf">#printf</a>
<li ><a href="#method-i-proc">#proc</a>
<li ><a href="#method-i-putc">#putc</a>
<li ><a href="#method-i-puts">#puts</a>
<li ><a href="#method-i-raise">#raise</a>
<li ><a href="#method-i-rand">#rand</a>
<li ><a href="#method-i-readline">#readline</a>
<li ><a href="#method-i-readlines">#readlines</a>
<li ><a href="#method-i-require">#require</a>
<li ><a href="#method-i-require_relative">#require_relative</a>
<li ><a href="#method-i-select">#select</a>
<li ><a href="#method-i-set_trace_func">#set_trace_func</a>
<li ><a href="#method-i-sleep">#sleep</a>
<li ><a href="#method-i-spawn">#spawn</a>
<li ><a href="#method-i-sprintf">#sprintf</a>
<li ><a href="#method-i-srand">#srand</a>
<li ><a href="#method-i-sub">#sub</a>
<li ><a href="#method-i-syscall">#syscall</a>
<li ><a href="#method-i-system">#system</a>
<li ><a href="#method-i-test">#test</a>
<li ><a href="#method-i-throw">#throw</a>
<li ><a href="#method-i-trace_var">#trace_var</a>
<li ><a href="#method-i-trap">#trap</a>
<li ><a href="#method-i-untrace_var">#untrace_var</a>
<li ><a href="#method-i-warn">#warn</a>
<li ><a href="#method-i-y">#y</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="module-Kernel">
<h1 id="module-Kernel" class="module">
module Kernel
</h1>
<section class="description">
<p>RubyGems adds the <a href="Kernel.html#method-i-gem"><code>gem</code></a> method to allow activation of specific gem versions and overrides the <a href="Kernel.html#method-i-require"><code>require</code></a> method on <a href="Kernel.html"><code>Kernel</code></a> to make gems appear as if they live on the <code>$LOAD_PATH</code>. See the documentation of these methods for further detail.</p>
<p>The <a href="Kernel.html"><code>Kernel</code></a> module is included by class <a href="Object.html"><code>Object</code></a>, so its methods are available in every Ruby object.</p>
<p>The <a href="Kernel.html"><code>Kernel</code></a> instance methods are documented in class <a href="Object.html"><code>Object</code></a> while the module methods are documented here. These methods are called without a receiver and thus can be called in functional form:</p>
<pre class="ruby"><span class="ruby-identifier">sprintf</span> <span class="ruby-string">"%.1f"</span>, <span class="ruby-value">1.234</span> <span class="ruby-comment">#=> "1.2"</span>
</pre>
<p>fronzen-string-literal: true</p>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-URI" class="method-detail ">
<div class="method-heading">
<span class="method-name">URI</span><span
class="method-args">(uri)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>uri</code> converted to an <a href="URI.html"><code>URI</code></a> object.</p>
<div class="method-source-code" id="URI-source">
<pre><span class="ruby-comment"># File lib/uri/common.rb, line 733</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">URI</span>(<span class="ruby-identifier ruby-title">uri</span>)
<span class="ruby-keyword">if</span> <span class="ruby-identifier">uri</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">URI</span><span class="ruby-operator">::</span><span class="ruby-constant">Generic</span>)
<span class="ruby-identifier">uri</span>
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">uri</span> = <span class="ruby-constant">String</span>.<span class="ruby-identifier">try_convert</span>(<span class="ruby-identifier">uri</span>)
<span class="ruby-constant">URI</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">uri</span>)
<span class="ruby-keyword">else</span>
<span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>,
<span class="ruby-string">"bad argument (expected URI object or URI string)"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-c-open" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
open(path [, mode [, perm]] [, opt]) → io or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
open(path [, mode [, perm]] [, opt]) {|io| block } → obj
</span>
</div>
<div class="method-description">
<p>Creates an <a href="IO.html"><code>IO</code></a> object connected to the given stream, file, or subprocess.</p>
<p>If <code>path</code> does not start with a pipe character (<code>|</code>), treat it as the name of a file to open using the specified mode (defaulting to “r”).</p>
<p>The <code>mode</code> is either a string or an integer. If it is an integer, it must be bitwise-or of open(2) flags, such as File::RDWR or File::EXCL. If it is a string, it is either “fmode”, “fmode:ext_enc”, or “fmode:ext_enc:int_enc”.</p>
<p>See the documentation of <a href="IO.html#method-c-new"><code>IO.new</code></a> for full documentation of the <code>mode</code> string directives.</p>
<p>If a file is being created, its initial permissions may be set using the <code>perm</code> parameter. See <a href="File.html#method-c-new"><code>File.new</code></a> and the open(2) and chmod(2) man pages for a description of permissions.</p>
<p>If a block is specified, it will be invoked with the <a href="IO.html"><code>IO</code></a> object as a parameter, and the <a href="IO.html"><code>IO</code></a> will be automatically closed when the block terminates. The call returns the value of the block.</p>
<p>If <code>path</code> starts with a pipe character (<code>"|"</code>), a subprocess is created, connected to the caller by a pair of pipes. The returned <a href="IO.html"><code>IO</code></a> object may be used to write to the standard input and read from the standard output of this subprocess.</p>
<p>If the command following the pipe is a single minus sign (<code>"|-"</code>), Ruby forks, and this subprocess is connected to the parent. If the command is not <code>"-"</code>, the subprocess runs the command.</p>
<p>When the subprocess is Ruby (opened via <code>"|-"</code>), the <code>open</code> call returns <code>nil</code>. If a block is associated with the open call, that block will run twice — once in the parent and once in the child.</p>
<p>The block parameter will be an <a href="IO.html"><code>IO</code></a> object in the parent and <code>nil</code> in the child. The parent's <code>IO</code> object will be connected to the child's $stdin and $stdout. The subprocess will be terminated at the end of the block.</p>
<h3 id="method-c-open-label-Examples">Examples<span><a href="#method-c-open-label-Examples">¶</a> <a href="#top">↑</a></span></h3>
<p>Reading from “testfile”:</p>
<pre class="ruby"><span class="ruby-identifier">open</span>(<span class="ruby-string">"testfile"</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">print</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">gets</span>
<span class="ruby-keyword">end</span>
</pre>
<p>Produces:</p>
<pre class="ruby"><span class="ruby-constant">This</span> <span class="ruby-identifier">is</span> <span class="ruby-identifier">line</span> <span class="ruby-identifier">one</span>
</pre>
<p>Open a subprocess and read its output:</p>
<pre class="ruby"><span class="ruby-identifier">cmd</span> = <span class="ruby-identifier">open</span>(<span class="ruby-string">"|date"</span>)
<span class="ruby-identifier">print</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">gets</span>
<span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">close</span>
</pre>
<p>Produces:</p>
<pre>Wed Apr 9 08:56:31 CDT 2003</pre>
<p>Open a subprocess running the same Ruby program:</p>
<pre class="ruby"><span class="ruby-identifier">f</span> = <span class="ruby-identifier">open</span>(<span class="ruby-string">"|-"</span>, <span class="ruby-string">"w+"</span>)
<span class="ruby-keyword">if</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">nil?</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"in Child"</span>
<span class="ruby-identifier">exit</span>
<span class="ruby-keyword">else</span>
<span class="ruby-identifier">puts</span> <span class="ruby-node">"Got: #{f.gets}"</span>
<span class="ruby-keyword">end</span>
</pre>
<p>Produces:</p>
<pre>Got: in Child</pre>
<p>Open a subprocess using a block to receive the <a href="IO.html"><code>IO</code></a> object:</p>
<pre class="ruby"><span class="ruby-identifier">open</span> <span class="ruby-string">"|-"</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">f</span> <span class="ruby-keyword">then</span>
<span class="ruby-comment"># parent process</span>
<span class="ruby-identifier">puts</span> <span class="ruby-node">"Got: #{f.gets}"</span>
<span class="ruby-keyword">else</span>
<span class="ruby-comment"># child process</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"in Child"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>
<p>Produces:</p>
<pre>Got: in Child</pre>
<div class="method-source-code" id="open-source">
<pre>static VALUE
rb_f_open(int argc, VALUE *argv, VALUE _)
{
ID to_open = 0;
int redirect = FALSE;
if (argc >= 1) {
CONST_ID(to_open, "to_open");
if (rb_respond_to(argv[0], to_open)) {
redirect = TRUE;
}
else {
VALUE tmp = argv[0];
FilePathValue(tmp);
if (NIL_P(tmp)) {
redirect = TRUE;
}
else {
VALUE cmd = check_pipe_command(tmp);
if (!NIL_P(cmd)) {
argv[0] = cmd;
return rb_io_s_popen(argc, argv, rb_cIO);
}
}
}
}
if (redirect) {
VALUE io = rb_funcallv_kw(argv[0], to_open, argc-1, argv+1, RB_PASS_CALLED_KEYWORDS);
if (rb_block_given_p()) {
return rb_ensure(rb_yield, io, io_close, io);
}
return io;
}
return rb_io_s_open(argc, argv, rb_cFile);
}</pre>
</div>
</div>
<div class="aliases">
Also aliased as: <a href="Kernel.html#method-i-open_uri_original_open">open_uri_original_open</a>, <a href="Kernel.html#method-c-open_uri_original_open">open_uri_original_open</a>
</div>
</div>
<div id="method-c-pp" class="method-detail ">
<div class="method-heading">
<span class="method-name">pp</span><span
class="method-args">(*objs)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>prints arguments in pretty form.</p>
<p>pp returns argument(s).</p>
<div class="method-source-code" id="pp-source">
<pre><span class="ruby-comment"># File lib/pp.rb, line 597</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pp</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">objs</span>)
<span class="ruby-identifier">objs</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">obj</span><span class="ruby-operator">|</span>
<span class="ruby-constant">PP</span>.<span class="ruby-identifier">pp</span>(<span class="ruby-identifier">obj</span>)
}
<span class="ruby-identifier">objs</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator"><=</span> <span class="ruby-value">1</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">objs</span>.<span class="ruby-identifier">first</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">objs</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
<div class="aliases">
Also aliased as: pp
</div>
</div>
</section>
<section id="private-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Private Class Methods</h3>
</header>
<div id="method-c-open_uri_original_open" class="method-detail method-alias">
<div class="method-heading">
<span class="method-name">open_uri_original_open</span><span
class="method-args">(*args)</span>
</div>
<div class="method-description">
</div>
<div class="aliases">
Alias for: <a href="Kernel.html#method-c-open">open</a>
</div>
</div>
</section>
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Instance Methods</h3>
</header>
<div id="method-i-Array" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Array(arg) → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>arg</code> as an <a href="Array.html"><code>Array</code></a>.</p>
<p>First tries to call <code>to_ary</code> on <code>arg</code>, then <code>to_a</code>. If <code>arg</code> does not respond to <code>to_ary</code> or <code>to_a</code>, returns an <a href="Array.html"><code>Array</code></a> of length 1 containing <code>arg</code>.</p>
<p>If <code>to_ary</code> or <code>to_a</code> returns something other than an <a href="Array.html"><code>Array</code></a>, raises a <a href="TypeError.html"><code>TypeError</code></a>.</p>
<pre class="ruby"><span class="ruby-constant">Array</span>([<span class="ruby-string">"a"</span>, <span class="ruby-string">"b"</span>]) <span class="ruby-comment">#=> ["a", "b"]</span>
<span class="ruby-constant">Array</span>(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">5</span>) <span class="ruby-comment">#=> [1, 2, 3, 4, 5]</span>
<span class="ruby-constant">Array</span>(<span class="ruby-value">key:</span> <span class="ruby-value">:value</span>) <span class="ruby-comment">#=> [[:key, :value]]</span>
<span class="ruby-constant">Array</span>(<span class="ruby-keyword">nil</span>) <span class="ruby-comment">#=> []</span>
<span class="ruby-constant">Array</span>(<span class="ruby-value">1</span>) <span class="ruby-comment">#=> [1]</span>
</pre>
<div class="method-source-code" id="Array-source">
<pre>static VALUE
rb_f_array(VALUE obj, VALUE arg)
{
return rb_Array(arg);
}</pre>
</div>
</div>
</div>
<div id="method-i-BigDecimal" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
BigDecimal(initial, digits, exception: true)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Create a new <a href="BigDecimal.html"><code>BigDecimal</code></a> object.</p>
<dl class="rdoc-list note-list"><dt>initial
<dd>
<p>The initial value, as an <a href="Integer.html"><code>Integer</code></a>, a <a href="Float.html"><code>Float</code></a>, a <a href="Rational.html"><code>Rational</code></a>, a <a href="BigDecimal.html"><code>BigDecimal</code></a>, or a <a href="String.html"><code>String</code></a>.</p>
<p>If it is a <a href="String.html"><code>String</code></a>, spaces are ignored and unrecognized characters terminate the value.</p>
</dd><dt>digits
<dd>
<p>The number of significant digits, as an <a href="Integer.html"><code>Integer</code></a>. If omitted or 0, the number of significant digits is determined from the initial value.</p>
<p>The actual number of significant digits used in computation is usually larger than the specified number.</p>
</dd><dt>exception
<dd>
<p>Whether an exception should be raised on invalid arguments. <code>true</code> by default, if passed <code>false</code>, just returns <code>nil</code> for invalid.</p>
</dd></dl>
<h4 id="method-i-BigDecimal-label-Exceptions">Exceptions<span><a href="#method-i-BigDecimal-label-Exceptions">¶</a> <a href="#top">↑</a></span></h4>
<dl class="rdoc-list note-list"><dt><a href="TypeError.html"><code>TypeError</code></a>
<dd>
<p>If the <code>initial</code> type is neither <a href="Integer.html"><code>Integer</code></a>, <a href="Float.html"><code>Float</code></a>, <a href="Rational.html"><code>Rational</code></a>, nor <a href="BigDecimal.html"><code>BigDecimal</code></a>, this exception is raised.</p>
</dd><dt><a href="TypeError.html"><code>TypeError</code></a>
<dd>
<p>If the <code>digits</code> is not an <a href="Integer.html"><code>Integer</code></a>, this exception is raised.</p>
</dd><dt><a href="ArgumentError.html"><code>ArgumentError</code></a>
<dd>
<p>If <code>initial</code> is a <a href="Float.html"><code>Float</code></a>, and the <code>digits</code> is larger than Float::DIG + 1, this exception is raised.</p>
</dd><dt><a href="ArgumentError.html"><code>ArgumentError</code></a>
<dd>
<p>If the <code>initial</code> is a <a href="Float.html"><code>Float</code></a> or <a href="Rational.html"><code>Rational</code></a>, and the <code>digits</code> value is omitted, this exception is raised.</p>
</dd></dl>
<div class="method-source-code" id="BigDecimal-source">
<pre>static VALUE
f_BigDecimal(int argc, VALUE *argv, VALUE self)
{
ENTER(1);
Real *pv;
VALUE obj;
if (argc > 0 && CLASS_OF(argv[0]) == rb_cBigDecimal) {
if (argc == 1 || (argc == 2 && RB_TYPE_P(argv[1], T_HASH))) return argv[0];
}
obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, 0);
pv = VpNewVarArg(argc, argv);
if (pv == NULL) return Qnil;
SAVE(pv);
if (ToValue(pv)) pv = VpCopy(NULL, pv);
RTYPEDDATA_DATA(obj) = pv;
RB_OBJ_FREEZE(obj);
return pv->obj = obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-Complex" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Complex(x[, y], exception: true) → numeric or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns x+i*y;</p>
<pre class="ruby"><span class="ruby-constant">Complex</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>) <span class="ruby-comment">#=> (1+2i)</span>
<span class="ruby-constant">Complex</span>(<span class="ruby-string">'1+2i'</span>) <span class="ruby-comment">#=> (1+2i)</span>
<span class="ruby-constant">Complex</span>(<span class="ruby-keyword">nil</span>) <span class="ruby-comment">#=> TypeError</span>
<span class="ruby-constant">Complex</span>(<span class="ruby-value">1</span>, <span class="ruby-keyword">nil</span>) <span class="ruby-comment">#=> TypeError</span>
<span class="ruby-constant">Complex</span>(<span class="ruby-value">1</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-value">exception:</span> <span class="ruby-keyword">false</span>) <span class="ruby-comment">#=> nil</span>
<span class="ruby-constant">Complex</span>(<span class="ruby-string">'1+2'</span>, <span class="ruby-value">exception:</span> <span class="ruby-keyword">false</span>) <span class="ruby-comment">#=> nil</span>
</pre>
<p>Syntax of string form:</p>
<pre>string form = extra spaces , complex , extra spaces ;
complex = real part | [ sign ] , imaginary part
| real part , sign , imaginary part
| rational , "@" , rational ;
real part = rational ;
imaginary part = imaginary unit | unsigned rational , imaginary unit ;
rational = [ sign ] , unsigned rational ;
unsigned rational = numerator | numerator , "/" , denominator ;
numerator = integer part | fractional part | integer part , fractional part ;
denominator = digits ;
integer part = digits ;
fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ;
imaginary unit = "i" | "I" | "j" | "J" ;
sign = "-" | "+" ;
digits = digit , { digit | "_" , digit };
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
extra spaces = ? \s* ? ;</pre>
<p>See <a href="String.html#method-i-to_c"><code>String#to_c</code></a>.</p>
<div class="method-source-code" id="Complex-source">
<pre>static VALUE
nucomp_f_complex(int argc, VALUE *argv, VALUE klass)
{
VALUE a1, a2, opts = Qnil;
int raise = TRUE;
if (rb_scan_args(argc, argv, "11:", &a1, &a2, &opts) == 1) {
a2 = Qundef;
}
if (!NIL_P(opts)) {
raise = rb_opts_exception_p(opts, raise);
}
if (argc > 0 && CLASS_OF(a1) == rb_cComplex && a2 == Qundef) {
return a1;
}
return nucomp_convert(rb_cComplex, a1, a2, raise);
}</pre>
</div>
</div>
</div>
<div id="method-i-Float" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Float(arg, exception: true) → float or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <em>arg</em> converted to a float. <a href="Numeric.html"><code>Numeric</code></a> types are converted directly, and with exception to <a href="String.html"><code>String</code></a> and <code>nil</code> the rest are converted using <em>arg</em><code>.to_f</code>. Converting a <a href="String.html"><code>String</code></a> with invalid characters will result in a <a href="ArgumentError.html"><code>ArgumentError</code></a>. Converting <code>nil</code> generates a <a href="TypeError.html"><code>TypeError</code></a>. Exceptions can be suppressed by passing <code>exception: false</code>.</p>
<pre class="ruby"><span class="ruby-constant">Float</span>(<span class="ruby-value">1</span>) <span class="ruby-comment">#=> 1.0</span>
<span class="ruby-constant">Float</span>(<span class="ruby-string">"123.456"</span>) <span class="ruby-comment">#=> 123.456</span>
<span class="ruby-constant">Float</span>(<span class="ruby-string">"123.0_badstring"</span>) <span class="ruby-comment">#=> ArgumentError: invalid value for Float(): "123.0_badstring"</span>
<span class="ruby-constant">Float</span>(<span class="ruby-keyword">nil</span>) <span class="ruby-comment">#=> TypeError: can't convert nil into Float</span>
<span class="ruby-constant">Float</span>(<span class="ruby-string">"123.0_badstring"</span>, <span class="ruby-value">exception:</span> <span class="ruby-keyword">false</span>) <span class="ruby-comment">#=> nil</span>
</pre>
<div class="method-source-code" id="Float-source">
<pre>static VALUE
rb_f_float(int argc, VALUE *argv, VALUE obj)
{
VALUE arg = Qnil, opts = Qnil;
rb_scan_args(argc, argv, "1:", &arg, &opts);
return rb_convert_to_float(arg, opts_exception_p(opts));
}</pre>
</div>
</div>
</div>
<div id="method-i-Hash" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Hash(arg) → hash
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Converts <em>arg</em> to a <a href="Hash.html"><code>Hash</code></a> by calling <em>arg</em><code>.to_hash</code>. Returns an empty <a href="Hash.html"><code>Hash</code></a> when <em>arg</em> is <code>nil</code> or <code>[]</code>.</p>
<pre class="ruby"><span class="ruby-constant">Hash</span>([]) <span class="ruby-comment">#=> {}</span>
<span class="ruby-constant">Hash</span>(<span class="ruby-keyword">nil</span>) <span class="ruby-comment">#=> {}</span>
<span class="ruby-constant">Hash</span>(<span class="ruby-value">key:</span> <span class="ruby-value">:value</span>) <span class="ruby-comment">#=> {:key => :value}</span>
<span class="ruby-constant">Hash</span>([<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>]) <span class="ruby-comment">#=> TypeError</span>
</pre>
<div class="method-source-code" id="Hash-source">
<pre>static VALUE
rb_f_hash(VALUE obj, VALUE arg)
{
return rb_Hash(arg);
}</pre>
</div>
</div>
</div>
<div id="method-i-Integer" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Integer(arg, base=0, exception: true) → integer or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Converts <em>arg</em> to an <a href="Integer.html"><code>Integer</code></a>. <a href="Numeric.html"><code>Numeric</code></a> types are converted directly (with floating point numbers being truncated). <em>base</em> (0, or between 2 and 36) is a base for integer string representation. If <em>arg</em> is a <a href="String.html"><code>String</code></a>, when <em>base</em> is omitted or equals zero, radix indicators (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored. In any case, strings should be strictly conformed to numeric representation. This behavior is different from that of <a href="String.html#method-i-to_i"><code>String#to_i</code></a>. Non string values will be converted by first trying <code>to_int</code>, then <code>to_i</code>.</p>
<p>Passing <code>nil</code> raises a <a href="TypeError.html"><code>TypeError</code></a>, while passing a <a href="String.html"><code>String</code></a> that does not conform with numeric representation raises an <a href="ArgumentError.html"><code>ArgumentError</code></a>. This behavior can be altered by passing <code>exception: false</code>, in this case a not convertible value will return <code>nil</code>.</p>
<pre class="ruby"><span class="ruby-constant">Integer</span>(<span class="ruby-value">123.999</span>) <span class="ruby-comment">#=> 123</span>
<span class="ruby-constant">Integer</span>(<span class="ruby-string">"0x1a"</span>) <span class="ruby-comment">#=> 26</span>
<span class="ruby-constant">Integer</span>(<span class="ruby-constant">Time</span>.<span class="ruby-identifier">new</span>) <span class="ruby-comment">#=> 1204973019</span>
<span class="ruby-constant">Integer</span>(<span class="ruby-string">"0930"</span>, <span class="ruby-value">10</span>) <span class="ruby-comment">#=> 930</span>
<span class="ruby-constant">Integer</span>(<span class="ruby-string">"111"</span>, <span class="ruby-value">2</span>) <span class="ruby-comment">#=> 7</span>
<span class="ruby-constant">Integer</span>(<span class="ruby-keyword">nil</span>) <span class="ruby-comment">#=> TypeError: can't convert nil into Integer</span>
<span class="ruby-constant">Integer</span>(<span class="ruby-string">"x"</span>) <span class="ruby-comment">#=> ArgumentError: invalid value for Integer(): "x"</span>
<span class="ruby-constant">Integer</span>(<span class="ruby-string">"x"</span>, <span class="ruby-value">exception:</span> <span class="ruby-keyword">false</span>) <span class="ruby-comment">#=> nil</span>
</pre>
<div class="method-source-code" id="Integer-source">
<pre>static VALUE
rb_f_integer(int argc, VALUE *argv, VALUE obj)
{
VALUE arg = Qnil, opts = Qnil;
int base = 0;
if (argc > 1) {
int narg = 1;
VALUE vbase = rb_check_to_int(argv[1]);
if (!NIL_P(vbase)) {
base = NUM2INT(vbase);
narg = 2;
}
if (argc > narg) {
VALUE hash = rb_check_hash_type(argv[argc-1]);
if (!NIL_P(hash)) {
opts = rb_extract_keywords(&hash);
if (!hash) --argc;
}
}
}
rb_check_arity(argc, 1, 2);
arg = argv[0];
return rb_convert_to_integer(arg, base, opts_exception_p(opts));
}</pre>
</div>
</div>
</div>
<div id="method-i-Pathname" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Pathname(path) → pathname
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Creates a new <a href="Pathname.html"><code>Pathname</code></a> object from the given string, <code>path</code>, and returns pathname object.</p>
<p>In order to use this constructor, you must first require the <a href="Pathname.html"><code>Pathname</code></a> standard library extension.</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'pathname'</span>
<span class="ruby-constant">Pathname</span>(<span class="ruby-string">"/home/zzak"</span>)
<span class="ruby-comment">#=> #<Pathname:/home/zzak></span>
</pre>
<p>See also <a href="Pathname.html#method-c-new"><code>Pathname::new</code></a> for more information.</p>
<div class="method-source-code" id="Pathname-source">
<pre>static VALUE
path_f_pathname(VALUE self, VALUE str)
{
if (CLASS_OF(str) == rb_cPathname)
return str;
return rb_class_new_instance(1, &str, rb_cPathname);
}</pre>
</div>
</div>
</div>
<div id="method-i-Rational" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Rational(x, y, exception: true) → rational or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Rational(arg, exception: true) → rational or nil
</span>
</div>
<div class="method-description">
<p>Returns <code>x/y</code> or <code>arg</code> as a <a href="Rational.html"><code>Rational</code></a>.</p>
<pre class="ruby"><span class="ruby-constant">Rational</span>(<span class="ruby-value">2</span>, <span class="ruby-value">3</span>) <span class="ruby-comment">#=> (2/3)</span>
<span class="ruby-constant">Rational</span>(<span class="ruby-value">5</span>) <span class="ruby-comment">#=> (5/1)</span>
<span class="ruby-constant">Rational</span>(<span class="ruby-value">0.5</span>) <span class="ruby-comment">#=> (1/2)</span>
<span class="ruby-constant">Rational</span>(<span class="ruby-value">0.3</span>) <span class="ruby-comment">#=> (5404319552844595/18014398509481984)</span>
<span class="ruby-constant">Rational</span>(<span class="ruby-string">"2/3"</span>) <span class="ruby-comment">#=> (2/3)</span>
<span class="ruby-constant">Rational</span>(<span class="ruby-string">"0.3"</span>) <span class="ruby-comment">#=> (3/10)</span>
<span class="ruby-constant">Rational</span>(<span class="ruby-string">"10 cents"</span>) <span class="ruby-comment">#=> ArgumentError</span>
<span class="ruby-constant">Rational</span>(<span class="ruby-keyword">nil</span>) <span class="ruby-comment">#=> TypeError</span>
<span class="ruby-constant">Rational</span>(<span class="ruby-value">1</span>, <span class="ruby-keyword">nil</span>) <span class="ruby-comment">#=> TypeError</span>
<span class="ruby-constant">Rational</span>(<span class="ruby-string">"10 cents"</span>, <span class="ruby-value">exception:</span> <span class="ruby-keyword">false</span>) <span class="ruby-comment">#=> nil</span>
</pre>
<p>Syntax of the string form:</p>
<pre>string form = extra spaces , rational , extra spaces ;
rational = [ sign ] , unsigned rational ;
unsigned rational = numerator | numerator , "/" , denominator ;
numerator = integer part | fractional part | integer part , fractional part ;
denominator = digits ;
integer part = digits ;
fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ;
sign = "-" | "+" ;
digits = digit , { digit | "_" , digit } ;
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
extra spaces = ? \s* ? ;</pre>
<p>See also <a href="String.html#method-i-to_r"><code>String#to_r</code></a>.</p>
<div class="method-source-code" id="Rational-source">
<pre>static VALUE
nurat_f_rational(int argc, VALUE *argv, VALUE klass)
{
VALUE a1, a2, opts = Qnil;
int raise = TRUE;
if (rb_scan_args(argc, argv, "11:", &a1, &a2, &opts) == 1) {
a2 = Qundef;
}
if (!NIL_P(opts)) {
raise = rb_opts_exception_p(opts, raise);
}
return nurat_convert(rb_cRational, a1, a2, raise);
}</pre>
</div>
</div>
</div>
<div id="method-i-String" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
String(arg) → string
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <em>arg</em> as a <a href="String.html"><code>String</code></a>.</p>
<p>First tries to call its <code>to_str</code> method, then its <code>to_s</code> method.</p>
<pre class="ruby"><span class="ruby-constant">String</span>(<span class="ruby-keyword">self</span>) <span class="ruby-comment">#=> "main"</span>
<span class="ruby-constant">String</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>) <span class="ruby-comment">#=> "Object"</span>
<span class="ruby-constant">String</span>(<span class="ruby-value">123456</span>) <span class="ruby-comment">#=> "123456"</span>
</pre>
<div class="method-source-code" id="String-source">
<pre>static VALUE
rb_f_string(VALUE obj, VALUE arg)
{
return rb_String(arg);
}</pre>
</div>
</div>
</div>
<div id="method-i-__callee__" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
__callee__ → symbol
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the called name of the current method as a <a href="Symbol.html"><code>Symbol</code></a>. If called outside of a method, it returns <code>nil</code>.</p>
<div class="method-source-code" id="__callee__-source">
<pre>static VALUE
rb_f_callee_name(VALUE _)
{
ID fname = prev_frame_callee(); /* need *callee* ID */
if (fname) {
return ID2SYM(fname);
}
else {
return Qnil;
}
}</pre>
</div>
</div>
</div>
<div id="method-i-__dir__" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
__dir__ → string
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the canonicalized absolute path of the directory of the file from which this method is called. It means symlinks in the path is resolved. If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>. The return value equals to <code>File.dirname(File.realpath(__FILE__))</code>.</p>
<div class="method-source-code" id="__dir__-source">
<pre>static VALUE
f_current_dirname(VALUE _)
{
VALUE base = rb_current_realfilepath();
if (NIL_P(base)) {
return Qnil;
}
base = rb_file_dirname(base);
return base;
}</pre>
</div>
</div>
</div>
<div id="method-i-__method__" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
__method__ → symbol
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the name at the definition of the current method as a <a href="Symbol.html"><code>Symbol</code></a>. If called outside of a method, it returns <code>nil</code>.</p>
<div class="method-source-code" id="__method__-source">
<pre>static VALUE
rb_f_method_name(VALUE _)
{
ID fname = prev_frame_func(); /* need *method* ID */
if (fname) {
return ID2SYM(fname);
}
else {
return Qnil;
}
}</pre>
</div>
</div>
</div>
<div id="method-i-60" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
`cmd` → string
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the standard output of running <em>cmd</em> in a subshell. The built-in syntax <code>%x{...}</code> uses this method. Sets <code>$?</code> to the process status.</p>
<pre class="ruby"><span class="ruby-string">`date`</span> <span class="ruby-comment">#=> "Wed Apr 9 08:56:30 CDT 2003\n"</span>
<span class="ruby-string">`ls testdir`</span>.<span class="ruby-identifier">split</span>[<span class="ruby-value">1</span>] <span class="ruby-comment">#=> "main.rb"</span>
<span class="ruby-string">`echo oops && exit 99`</span> <span class="ruby-comment">#=> "oops\n"</span>
<span class="ruby-identifier">$?</span>.<span class="ruby-identifier">exitstatus</span> <span class="ruby-comment">#=> 99</span>
</pre>
<div class="method-source-code" id="60-source">
<pre>static VALUE
rb_f_backquote(VALUE obj, VALUE str)
{
VALUE port;
VALUE result;
rb_io_t *fptr;
SafeStringValue(str);
rb_last_status_clear();
port = pipe_open_s(str, "r", FMODE_READABLE|DEFAULT_TEXTMODE, NULL);
if (NIL_P(port)) return rb_str_new(0,0);
GetOpenFile(port, fptr);
result = read_all(fptr, remain_size(fptr), Qnil);
rb_io_close(port);
RFILE(port)->fptr = NULL;
rb_io_fptr_finalize(fptr);
rb_gc_force_recycle(port); /* also guards from premature GC */
return result;
}</pre>
</div>
</div>
</div>
<div id="method-i-abort" class="method-detail ">
<div class="method-heading">
<span class="method-name">abort</span><span
class="method-args">(*args)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<div class="method-source-code" id="abort-source">
<pre>static VALUE
f_abort(int c, const VALUE *a, VALUE _)
{
return rb_f_abort(c, a);
}</pre>
</div>
</div>
</div>
<div id="method-i-at_exit" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
at_exit { block } → proc
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Converts <em>block</em> to a <code>Proc</code> object (and therefore binds it at the point of call) and registers it for execution when the program exits. If multiple handlers are registered, they are executed in reverse order of registration.</p>
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">do_at_exit</span>(<span class="ruby-identifier">str1</span>)
<span class="ruby-identifier">at_exit</span> { <span class="ruby-identifier">print</span> <span class="ruby-identifier">str1</span> }
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">at_exit</span> { <span class="ruby-identifier">puts</span> <span class="ruby-string">"cruel world"</span> }
<span class="ruby-identifier">do_at_exit</span>(<span class="ruby-string">"goodbye "</span>)
<span class="ruby-identifier">exit</span>
</pre>
<p><em>produces:</em></p>
<pre class="ruby"><span class="ruby-identifier">goodbye</span> <span class="ruby-identifier">cruel</span> <span class="ruby-identifier">world</span>
</pre>
<div class="method-source-code" id="at_exit-source">
<pre>static VALUE
rb_f_at_exit(VALUE _)
{
VALUE proc;
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "called without a block");
}
proc = rb_block_proc();
rb_set_end_proc(rb_call_end_proc, proc);
return proc;
}</pre>
</div>
</div>
</div>
<div id="method-i-autoload" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
autoload(module, filename) → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Registers <em>filename</em> to be loaded (using Kernel::require) the first time that <em>module</em> (which may be a <a href="String.html"><code>String</code></a> or a symbol) is accessed.</p>
<pre class="ruby"><span class="ruby-identifier">autoload</span>(<span class="ruby-value">:MyModule</span>, <span class="ruby-string">"/usr/local/lib/modules/my_module.rb"</span>)
</pre>
<div class="method-source-code" id="autoload-source">
<pre>static VALUE
rb_f_autoload(VALUE obj, VALUE sym, VALUE file)
{
VALUE klass = rb_class_real(rb_vm_cbase());
if (NIL_P(klass)) {
rb_raise(rb_eTypeError, "Can not set autoload on singleton class");
}
return rb_mod_autoload(klass, sym, file);
}</pre>
</div>
</div>
</div>
<div id="method-i-autoload-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
autoload?(name, inherit=true) → String or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <em>filename</em> to be loaded if <em>name</em> is registered as <code>autoload</code>.</p>
<pre class="ruby"><span class="ruby-identifier">autoload</span>(<span class="ruby-value">:B</span>, <span class="ruby-string">"b"</span>)
<span class="ruby-identifier">autoload?</span>(<span class="ruby-value">:B</span>) <span class="ruby-comment">#=> "b"</span>
</pre>
<div class="method-source-code" id="autoload-3F-source">
<pre>static VALUE
rb_f_autoload_p(int argc, VALUE *argv, VALUE obj)
{
/* use rb_vm_cbase() as same as rb_f_autoload. */
VALUE klass = rb_vm_cbase();
if (NIL_P(klass)) {
return Qnil;
}
return rb_mod_autoload_p(argc, argv, klass);
}</pre>
</div>
</div>
</div>
<div id="method-i-binding" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
binding → a_binding
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns a <code>Binding</code> object, describing the variable and method bindings at the point of call. This object can be used when calling <code>eval</code> to execute the evaluated command in this environment. See also the description of class <code>Binding</code>.</p>
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get_binding</span>(<span class="ruby-identifier">param</span>)
<span class="ruby-identifier">binding</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">b</span> = <span class="ruby-identifier">get_binding</span>(<span class="ruby-string">"hello"</span>)
<span class="ruby-identifier">eval</span>(<span class="ruby-string">"param"</span>, <span class="ruby-identifier">b</span>) <span class="ruby-comment">#=> "hello"</span>
</pre>
<div class="method-source-code" id="binding-source">
<pre>static VALUE
rb_f_binding(VALUE self)
{
return rb_binding_new();
}</pre>
</div>
</div>
</div>
<div id="method-i-block_given-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
block_given? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if <code>yield</code> would execute a block in the current context. The <code>iterator?</code> form is mildly deprecated.</p>
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">try</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
<span class="ruby-keyword">yield</span>
<span class="ruby-keyword">else</span>
<span class="ruby-string">"no block"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">try</span> <span class="ruby-comment">#=> "no block"</span>
<span class="ruby-identifier">try</span> { <span class="ruby-string">"hello"</span> } <span class="ruby-comment">#=> "hello"</span>
<span class="ruby-identifier">try</span> <span class="ruby-keyword">do</span> <span class="ruby-string">"hello"</span> <span class="ruby-keyword">end</span> <span class="ruby-comment">#=> "hello"</span>
</pre>
<div class="method-source-code" id="block_given-3F-source">
<pre>static VALUE
rb_f_block_given_p(VALUE _)
{
rb_execution_context_t *ec = GET_EC();
rb_control_frame_t *cfp = ec->cfp;
cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
if (cfp != NULL && VM_CF_BLOCK_HANDLER(cfp) != VM_BLOCK_HANDLER_NONE) {
return Qtrue;
}
else {
return Qfalse;
}
}</pre>
</div>
</div>
</div>
<div id="method-i-callcc" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
callcc {|cont| block } → obj
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Generates a <a href="Continuation.html"><code>Continuation</code></a> object, which it passes to the associated block. You need to <code>require 'continuation'</code> before using this method. Performing a <em>cont</em><code>.call</code> will cause the <a href="Kernel.html#method-i-callcc"><code>callcc</code></a> to return (as will falling through the end of the block). The value returned by the <a href="Kernel.html#method-i-callcc"><code>callcc</code></a> is the value of the block, or the value passed to <em>cont</em><code>.call</code>. See class <a href="Continuation.html"><code>Continuation</code></a> for more details. Also see <a href="Kernel.html#method-i-throw"><code>Kernel#throw</code></a> for an alternative mechanism for unwinding a call stack.</p>
<div class="method-source-code" id="callcc-source">
<pre>static VALUE
rb_callcc(VALUE self)
{
volatile int called;
volatile VALUE val = cont_capture(&called);
if (called) {
return val;
}
else {
return rb_yield(val);
}
}</pre>
</div>
</div>
</div>
<div id="method-i-caller" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
caller(start=1, length=nil) → array or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
caller(range) → array or nil
</span>
</div>
<div class="method-description">
<p>Returns the current execution stack—an array containing strings in the form <code>file:line</code> or <code>file:line: in `method'</code>.</p>
<p>The optional <em>start</em> parameter determines the number of initial stack entries to omit from the top of the stack.</p>
<p>A second optional <code>length</code> parameter can be used to limit how many entries are returned from the stack.</p>
<p>Returns <code>nil</code> if <em>start</em> is greater than the size of current execution stack.</p>
<p>Optionally you can pass a range, which will return an array containing the entries within the specified range.</p>
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">a</span>(<span class="ruby-identifier">skip</span>)
<span class="ruby-identifier">caller</span>(<span class="ruby-identifier">skip</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">b</span>(<span class="ruby-identifier">skip</span>)
<span class="ruby-identifier">a</span>(<span class="ruby-identifier">skip</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">c</span>(<span class="ruby-identifier">skip</span>)
<span class="ruby-identifier">b</span>(<span class="ruby-identifier">skip</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">c</span>(<span class="ruby-value">0</span>) <span class="ruby-comment">#=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10:in `<main>'"]</span>
<span class="ruby-identifier">c</span>(<span class="ruby-value">1</span>) <span class="ruby-comment">#=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11:in `<main>'"]</span>
<span class="ruby-identifier">c</span>(<span class="ruby-value">2</span>) <span class="ruby-comment">#=> ["prog:8:in `c'", "prog:12:in `<main>'"]</span>
<span class="ruby-identifier">c</span>(<span class="ruby-value">3</span>) <span class="ruby-comment">#=> ["prog:13:in `<main>'"]</span>
<span class="ruby-identifier">c</span>(<span class="ruby-value">4</span>) <span class="ruby-comment">#=> []</span>
<span class="ruby-identifier">c</span>(<span class="ruby-value">5</span>) <span class="ruby-comment">#=> nil</span>
</pre>
<div class="method-source-code" id="caller-source">
<pre>static VALUE
rb_f_caller(int argc, VALUE *argv, VALUE _)
{
return ec_backtrace_to_ary(GET_EC(), argc, argv, 1, 1, 1);
}</pre>
</div>
</div>
</div>
<div id="method-i-caller_locations" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
caller_locations(start=1, length=nil) → array or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
caller_locations(range) → array or nil
</span>
</div>
<div class="method-description">
<p>Returns the current execution stack—an array containing backtrace location objects.</p>
<p>See <a href="Thread/Backtrace/Location.html"><code>Thread::Backtrace::Location</code></a> for more information.</p>
<p>The optional <em>start</em> parameter determines the number of initial stack entries to omit from the top of the stack.</p>
<p>A second optional <code>length</code> parameter can be used to limit how many entries are returned from the stack.</p>
<p>Returns <code>nil</code> if <em>start</em> is greater than the size of current execution stack.</p>
<p>Optionally you can pass a range, which will return an array containing the entries within the specified range.</p>
<div class="method-source-code" id="caller_locations-source">
<pre>static VALUE
rb_f_caller_locations(int argc, VALUE *argv, VALUE _)
{
return ec_backtrace_to_ary(GET_EC(), argc, argv, 1, 1, 0);
}</pre>
</div>
</div>
</div>
<div id="method-i-catch" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
catch([tag]) {|tag| block } → obj
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p><code>catch</code> executes its block. If <code>throw</code> is not called, the block executes normally, and <code>catch</code> returns the value of the last expression evaluated.</p>
<pre class="ruby"><span class="ruby-identifier">catch</span>(<span class="ruby-value">1</span>) { <span class="ruby-value">123</span> } <span class="ruby-comment"># => 123</span>
</pre>
<p>If <code>throw(tag2, val)</code> is called, Ruby searches up its stack for a <code>catch</code> block whose <code>tag</code> has the same <code>object_id</code> as <em>tag2</em>. When found, the block stops executing and returns <em>val</em> (or <code>nil</code> if no second argument was given to <code>throw</code>).</p>
<pre class="ruby"><span class="ruby-identifier">catch</span>(<span class="ruby-value">1</span>) { <span class="ruby-identifier">throw</span>(<span class="ruby-value">1</span>, <span class="ruby-value">456</span>) } <span class="ruby-comment"># => 456</span>
<span class="ruby-identifier">catch</span>(<span class="ruby-value">1</span>) { <span class="ruby-identifier">throw</span>(<span class="ruby-value">1</span>) } <span class="ruby-comment"># => nil</span>
</pre>
<p>When <code>tag</code> is passed as the first argument, <code>catch</code> yields it as the parameter of the block.</p>
<pre class="ruby"><span class="ruby-identifier">catch</span>(<span class="ruby-value">1</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">x</span> <span class="ruby-operator">+</span> <span class="ruby-value">2</span> } <span class="ruby-comment"># => 3</span>
</pre>
<p>When no <code>tag</code> is given, <code>catch</code> yields a new unique object (as from <code>Object.new</code>) as the block parameter. This object can then be used as the argument to <code>throw</code>, and will match the correct <code>catch</code> block.</p>
<pre class="ruby"><span class="ruby-identifier">catch</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">obj_A</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">catch</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">obj_B</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">throw</span>(<span class="ruby-identifier">obj_B</span>, <span class="ruby-value">123</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-string">"This puts is not reached"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"This puts is displayed"</span>
<span class="ruby-value">456</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># => 456</span>
<span class="ruby-identifier">catch</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">obj_A</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">catch</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">obj_B</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">throw</span>(<span class="ruby-identifier">obj_A</span>, <span class="ruby-value">123</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-string">"This puts is still not reached"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"Now this puts is also not reached"</span>
<span class="ruby-value">456</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># => 123</span>
</pre>
<div class="method-source-code" id="catch-source">
<pre>static VALUE
rb_f_catch(int argc, VALUE *argv, VALUE self)
{
VALUE tag = rb_check_arity(argc, 0, 1) ? argv[0] : rb_obj_alloc(rb_cObject);
return rb_catch_obj(tag, catch_i, 0);
}</pre>
</div>
</div>
</div>
<div id="method-i-chomp" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
chomp → $_
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
chomp(string) → $_
</span>
</div>
<div class="method-description">
<p>Equivalent to <code>$_ = $_.chomp(<em>string</em>)</code>. See <a href="String.html#method-i-chomp"><code>String#chomp</code></a>. Available only when -p/-n command line option specified.</p>
<div class="method-source-code" id="chomp-source">
<pre>static VALUE
rb_f_chomp(int argc, VALUE *argv, VALUE _)
{
VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("chomp"), argc, argv);
rb_lastline_set(str);
return str;
}</pre>
</div>
</div>
</div>
<div id="method-i-chop" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
chop → $_
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Equivalent to <code>($_.dup).chop!</code>, except <code>nil</code> is never returned. See <a href="String.html#method-i-chop-21"><code>String#chop!</code></a>. Available only when -p/-n command line option specified.</p>
<div class="method-source-code" id="chop-source">
<pre>static VALUE
rb_f_chop(VALUE _)
{
VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("chop"), 0, 0);
rb_lastline_set(str);
return str;
}</pre>
</div>
</div>
</div>
<div id="method-i-eval" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
eval(string [, binding [, filename [,lineno]]]) → obj
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Evaluates the Ruby expression(s) in <em>string</em>. If <em>binding</em> is given, which must be a <a href="Binding.html"><code>Binding</code></a> object, the evaluation is performed in its context. If the optional <em>filename</em> and <em>lineno</em> parameters are present, they will be used when reporting syntax errors.</p>
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get_binding</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-keyword">return</span> <span class="ruby-identifier">binding</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> = <span class="ruby-string">"hello"</span>
<span class="ruby-identifier">eval</span> <span class="ruby-string">"str + ' Fred'"</span> <span class="ruby-comment">#=> "hello Fred"</span>
<span class="ruby-identifier">eval</span> <span class="ruby-string">"str + ' Fred'"</span>, <span class="ruby-identifier">get_binding</span>(<span class="ruby-string">"bye"</span>) <span class="ruby-comment">#=> "bye Fred"</span>
</pre>
<div class="method-source-code" id="eval-source">
<pre>VALUE
rb_f_eval(int argc, const VALUE *argv, VALUE self)
{
VALUE src, scope, vfile, vline;
VALUE file = Qundef;
int line = 1;
rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
SafeStringValue(src);
if (argc >= 3) {
StringValue(vfile);
}
if (argc >= 4) {
line = NUM2INT(vline);
}
if (!NIL_P(vfile))
file = vfile;
if (NIL_P(scope))
return eval_string_with_cref(self, src, NULL, file, line);
else
return eval_string_with_scope(scope, src, file, line);
}</pre>
</div>
</div>
</div>
<div id="method-i-exec" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
exec([env,] command... [,options])
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Replaces the current process by running the given external <em>command</em>, which can take one of the following forms:</p>
<dl class="rdoc-list label-list"><dt><code>exec(commandline)</code>
<dd>
<p>command line string which is passed to the standard shell</p>
</dd><dt><code>exec(cmdname, arg1, ...)</code>
<dd>
<p>command name and one or more arguments (no shell)</p>
</dd><dt><code>exec([cmdname, argv0], arg1, ...)</code>
<dd>
<p>command name, <a href="0">argv</a> and zero or more arguments (no shell)</p>
</dd></dl>
<p>In the first form, the string is taken as a command line that is subject to shell expansion before being executed.</p>
<p>The standard shell always means <code>"/bin/sh"</code> on Unix-like systems, same as <code>ENV["RUBYSHELL"]</code> (or <code>ENV["COMSPEC"]</code> on Windows NT series), and similar.</p>
<p>If the string from the first form (<code>exec("command")</code>) follows these simple rules:</p>
<ul><li>
<p>no meta characters</p>
</li><li>
<p>no shell reserved word and no special built-in</p>
</li><li>
<p>Ruby invokes the command directly without shell</p>
</li></ul>
<p>You can force shell invocation by adding “;” to the string (because “;” is a meta character).</p>
<p>Note that this behavior is observable by pid obtained (return value of spawn() and <a href="IO.html#method-i-pid"><code>IO#pid</code></a> for <a href="IO.html#method-c-popen"><code>IO.popen</code></a>) is the pid of the invoked command, not shell.</p>
<p>In the second form (<code>exec("command1", "arg1", ...)</code>), the first is taken as a command name and the rest are passed as parameters to command with no shell expansion.</p>
<p>In the third form (<code>exec(["command", "argv0"], "arg1", ...)</code>), starting a two-element array at the beginning of the command, the first element is the command to be executed, and the second argument is used as the <code>argv[0]</code> value, which may show up in process listings.</p>
<p>In order to execute the command, one of the <code>exec(2)</code> system calls are used, so the running command may inherit some of the environment of the original program (including open file descriptors).</p>
<p>This behavior is modified by the given <code>env</code> and <code>options</code> parameters. See ::spawn for details.</p>
<p>If the command fails to execute (typically Errno::ENOENT when it was not found) a <a href="SystemCallError.html"><code>SystemCallError</code></a> exception is raised.</p>
<p>This method modifies process attributes according to given <code>options</code> before <code>exec(2)</code> system call. See ::spawn for more details about the given <code>options</code>.</p>
<p>The modified attributes may be retained when <code>exec(2)</code> system call fails.</p>
<p>For example, hard resource limits are not restorable.</p>
<p>Consider to create a child process using ::spawn or <a href="Kernel.html#method-i-system"><code>Kernel#system</code></a> if this is not acceptable.</p>
<pre class="ruby"><span class="ruby-identifier">exec</span> <span class="ruby-string">"echo *"</span> <span class="ruby-comment"># echoes list of files in current directory</span>
<span class="ruby-comment"># never get here</span>
<span class="ruby-identifier">exec</span> <span class="ruby-string">"echo"</span>, <span class="ruby-string">"*"</span> <span class="ruby-comment"># echoes an asterisk</span>
<span class="ruby-comment"># never get here</span>
</pre>
<div class="method-source-code" id="exec-source">
<pre>static VALUE
f_exec(int c, const VALUE *a, VALUE _)
{
return rb_f_exec(c, a);
}</pre>
</div>
</div>
</div>
<div id="method-i-exit" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
exit(status=true)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Kernel::exit(status=true)
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::exit(status=true)
</span>
</div>
<div class="method-description">
<p>Initiates the termination of the Ruby script by raising the <a href="SystemExit.html"><code>SystemExit</code></a> exception. This exception may be caught. The optional parameter is used to return a status code to the invoking environment. <code>true</code> and <code>FALSE</code> of <em>status</em> means success and failure respectively. The interpretation of other integer values are system dependent.</p>
<pre class="ruby"><span class="ruby-keyword">begin</span>
<span class="ruby-identifier">exit</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"never get here"</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">SystemExit</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"rescued a SystemExit exception"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"after begin block"</span>
</pre>
<p><em>produces:</em></p>
<pre>rescued a SystemExit exception
after begin block</pre>
<p>Just prior to termination, Ruby executes any <code>at_exit</code> functions (see Kernel::at_exit) and runs any object finalizers (see <a href="ObjectSpace.html#method-c-define_finalizer"><code>ObjectSpace::define_finalizer</code></a>).</p>
<pre class="ruby"><span class="ruby-identifier">at_exit</span> { <span class="ruby-identifier">puts</span> <span class="ruby-string">"at_exit function"</span> }
<span class="ruby-constant">ObjectSpace</span>.<span class="ruby-identifier">define_finalizer</span>(<span class="ruby-string">"string"</span>, <span class="ruby-identifier">proc</span> { <span class="ruby-identifier">puts</span> <span class="ruby-string">"in finalizer"</span> })
<span class="ruby-identifier">exit</span>
</pre>
<p><em>produces:</em></p>
<pre>at_exit function
in finalizer</pre>
<div class="method-source-code" id="exit-source">
<pre>static VALUE
f_exit(int c, const VALUE *a, VALUE _)
{
return rb_f_exit(c, a);
}</pre>
</div>
</div>
</div>
<div id="method-i-exit-21" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
exit!(status=false)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Exits the process immediately. No exit handlers are run. <em>status</em> is returned to the underlying system as the exit status.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">exit!</span>(<span class="ruby-keyword">true</span>)
</pre>
<div class="method-source-code" id="exit-21-source">
<pre>static VALUE
rb_f_exit_bang(int argc, VALUE *argv, VALUE obj)
{
int istatus;
if (rb_check_arity(argc, 0, 1) == 1) {
istatus = exit_status_code(argv[0]);
}
else {
istatus = EXIT_FAILURE;
}
_exit(istatus);
UNREACHABLE_RETURN(Qnil);
}</pre>
</div>
</div>
</div>
<div id="method-i-fail" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
fail
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
fail(string, cause: $!)
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
fail(exception [, string [, array]], cause: $!)
</span>
</div>
<div class="method-description">
<p>With no arguments, raises the exception in <code>$!</code> or raises a <a href="RuntimeError.html"><code>RuntimeError</code></a> if <code>$!</code> is <code>nil</code>. With a single <code>String</code> argument, raises a <code>RuntimeError</code> with the string as a message. Otherwise, the first parameter should be an <code>Exception</code> class (or another object that returns an <code>Exception</code> object when sent an <code>exception</code> message). The optional second parameter sets the message associated with the exception (accessible via <a href="Exception.html#method-i-message"><code>Exception#message</code></a>), and the third parameter is an array of callback information (accessible via <a href="Exception.html#method-i-backtrace"><code>Exception#backtrace</code></a>). The <code>cause</code> of the generated exception (accessible via <a href="Exception.html#method-i-cause"><code>Exception#cause</code></a>) is automatically set to the “current” exception (<code>$!</code>), if any. An alternative value, either an <code>Exception</code> object or <code>nil</code>, can be specified via the <code>:cause</code> argument.</p>
<p>Exceptions are caught by the <code>rescue</code> clause of <code>begin...end</code> blocks.</p>
<pre class="ruby"><span class="ruby-identifier">raise</span> <span class="ruby-string">"Failed to create socket"</span>
<span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">"No parameters"</span>, <span class="ruby-identifier">caller</span>
</pre>
<div class="method-source-code" id="fail-source">
<pre>static VALUE
f_raise(int c, VALUE *v, VALUE _)
{
return rb_f_raise(c, v);
}</pre>
</div>
</div>
</div>
<div id="method-i-fork" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
fork [{ block }] → integer or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
fork [{ block }] → integer or nil
</span>
</div>
<div class="method-description">
<p>Creates a subprocess. If a block is specified, that block is run in the subprocess, and the subprocess terminates with a status of zero. Otherwise, the <code>fork</code> call returns twice, once in the parent, returning the process ID of the child, and once in the child, returning <em>nil</em>. The child process can exit using <a href="Kernel.html#method-i-exit-21"><code>Kernel.exit!</code></a> to avoid running any <code>at_exit</code> functions. The parent process should use <a href="Process.html#method-c-wait"><code>Process.wait</code></a> to collect the termination statuses of its children or use <a href="Process.html#method-c-detach"><code>Process.detach</code></a> to register disinterest in their status; otherwise, the operating system may accumulate zombie processes.</p>
<p>The thread calling fork is the only thread in the created child process. fork doesn't copy other threads.</p>
<p>If fork is not usable, Process.respond_to?(:fork) returns false.</p>
<p>Note that fork(2) is not available on some platforms like Windows and NetBSD 4. Therefore you should use spawn() instead of fork().</p>
<div class="method-source-code" id="fork-source">
<pre>static VALUE
rb_f_fork(VALUE obj)
{
rb_pid_t pid;
switch (pid = rb_fork_ruby(NULL)) {
case 0:
rb_thread_atfork();
if (rb_block_given_p()) {
int status;
rb_protect(rb_yield, Qundef, &status);
ruby_stop(status);
}
return Qnil;
case -1:
rb_sys_fail("fork(2)");
return Qnil;
default:
return PIDT2NUM(pid);
}
}</pre>
</div>
</div>
</div>
<div id="method-i-format" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
format(format_string [, arguments...] ) → string
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the string resulting from applying <em>format_string</em> to any additional arguments. Within the format string, any characters other than format sequences are copied to the result.</p>
<p>The syntax of a format sequence is as follows.</p>
<pre>%[flags][width][.precision]type</pre>
<p>A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character. The field type controls how the corresponding <code>sprintf</code> argument is to be interpreted, while the flags modify that interpretation.</p>
<p>The field type characters are:</p>
<pre>Field | Integer Format
------+--------------------------------------------------------------
b | Convert argument as a binary number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..1'.
B | Equivalent to `b', but uses an uppercase 0B for prefix
| in the alternative format by #.
d | Convert argument as a decimal number.
i | Identical to `d'.
o | Convert argument as an octal number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..7'.
u | Identical to `d'.
x | Convert argument as a hexadecimal number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..f' (representing an infinite string of
| leading 'ff's).
X | Equivalent to `x', but uses uppercase letters.
Field | Float Format
------+--------------------------------------------------------------
e | Convert floating point argument into exponential notation
| with one digit before the decimal point as [-]d.dddddde[+-]dd.
| The precision specifies the number of digits after the decimal
| point (defaulting to six).
E | Equivalent to `e', but uses an uppercase E to indicate
| the exponent.
f | Convert floating point argument as [-]ddd.dddddd,
| where the precision specifies the number of digits after
| the decimal point.
g | Convert a floating point number using exponential form
| if the exponent is less than -4 or greater than or
| equal to the precision, or in dd.dddd form otherwise.
| The precision specifies the number of significant digits.
G | Equivalent to `g', but use an uppercase `E' in exponent form.
a | Convert floating point argument as [-]0xh.hhhhp[+-]dd,
| which is consisted from optional sign, "0x", fraction part
| as hexadecimal, "p", and exponential part as decimal.
A | Equivalent to `a', but use uppercase `X' and `P'.
Field | Other Format
------+--------------------------------------------------------------
c | Argument is the numeric code for a single character or
| a single character string itself.
p | The valuing of argument.inspect.
s | Argument is a string to be substituted. If the format
| sequence contains a precision, at most that many characters
| will be copied.
% | A percent sign itself will be displayed. No argument taken.</pre>
<p>The flags modifies the behavior of the formats. The flag characters are:</p>
<pre>Flag | Applies to | Meaning
---------+---------------+-----------------------------------------
space | bBdiouxX | Leave a space at the start of
| aAeEfgG | non-negative numbers.
| (numeric fmt) | For `o', `x', `X', `b' and `B', use
| | a minus sign with absolute value for
| | negative values.
---------+---------------+-----------------------------------------
(digit)$ | all | Specifies the absolute argument number
| | for this field. Absolute and relative
| | argument numbers cannot be mixed in a
| | sprintf string.
---------+---------------+-----------------------------------------
# | bBoxX | Use an alternative format.
| aAeEfgG | For the conversions `o', increase the precision
| | until the first digit will be `0' if
| | it is not formatted as complements.
| | For the conversions `x', `X', `b' and `B'
| | on non-zero, prefix the result with ``0x'',
| | ``0X'', ``0b'' and ``0B'', respectively.
| | For `a', `A', `e', `E', `f', `g', and 'G',
| | force a decimal point to be added,
| | even if no digits follow.
| | For `g' and 'G', do not remove trailing zeros.
---------+---------------+-----------------------------------------
+ | bBdiouxX | Add a leading plus sign to non-negative
| aAeEfgG | numbers.
| (numeric fmt) | For `o', `x', `X', `b' and `B', use
| | a minus sign with absolute value for
| | negative values.
---------+---------------+-----------------------------------------
- | all | Left-justify the result of this conversion.
---------+---------------+-----------------------------------------
0 (zero) | bBdiouxX | Pad with zeros, not spaces.
| aAeEfgG | For `o', `x', `X', `b' and `B', radix-1
| (numeric fmt) | is used for negative numbers formatted as
| | complements.
---------+---------------+-----------------------------------------
* | all | Use the next argument as the field width.
| | If negative, left-justify the result. If the
| | asterisk is followed by a number and a dollar
| | sign, use the indicated argument as the width.</pre>
<p>Examples of flags:</p>
<pre class="ruby"><span class="ruby-comment"># `+' and space flag specifies the sign of non-negative numbers.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "+123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"% d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 123"</span>
<span class="ruby-comment"># `#' flag for `o' increases number of digits to show `0'.</span>
<span class="ruby-comment"># `+' and space flag changes format of negative numbers.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%o"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "173"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#o"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "0173"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+o"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "-173"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%o"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "..7605"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#o"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "..7605"</span>
<span class="ruby-comment"># `#' flag for `x' add a prefix `0x' for non-zero numbers.</span>
<span class="ruby-comment"># `+' and space flag disables complements for negative numbers.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%x"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "7b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#x"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "0x7b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "-7b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "..f85"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "0x..f85"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#x"</span>, <span class="ruby-value">0</span>) <span class="ruby-comment">#=> "0"</span>
<span class="ruby-comment"># `#' for `X' uses the prefix `0X'.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%X"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "7B"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#X"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "0X7B"</span>
<span class="ruby-comment"># `#' flag for `b' add a prefix `0b' for non-zero numbers.</span>
<span class="ruby-comment"># `+' and space flag disables complements for negative numbers.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%b"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "1111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#b"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "0b1111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+b"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "-1111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%b"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "..10000101"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#b"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "0b..10000101"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#b"</span>, <span class="ruby-value">0</span>) <span class="ruby-comment">#=> "0"</span>
<span class="ruby-comment"># `#' for `B' uses the prefix `0B'.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%B"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "1111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#B"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "0B1111011"</span>
<span class="ruby-comment"># `#' for `e' forces to show the decimal point.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%.0e"</span>, <span class="ruby-value">1</span>) <span class="ruby-comment">#=> "1e+00"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#.0e"</span>, <span class="ruby-value">1</span>) <span class="ruby-comment">#=> "1.e+00"</span>
<span class="ruby-comment"># `#' for `f' forces to show the decimal point.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%.0f"</span>, <span class="ruby-value">1234</span>) <span class="ruby-comment">#=> "1234"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#.0f"</span>, <span class="ruby-value">1234</span>) <span class="ruby-comment">#=> "1234."</span>
<span class="ruby-comment"># `#' for `g' forces to show the decimal point.</span>
<span class="ruby-comment"># It also disables stripping lowest zeros.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%g"</span>, <span class="ruby-value">123.4</span>) <span class="ruby-comment">#=> "123.4"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#g"</span>, <span class="ruby-value">123.4</span>) <span class="ruby-comment">#=> "123.400"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%g"</span>, <span class="ruby-value">123456</span>) <span class="ruby-comment">#=> "123456"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#g"</span>, <span class="ruby-value">123456</span>) <span class="ruby-comment">#=> "123456."</span>
</pre>
<p>The field width is an optional integer, followed optionally by a period and a precision. The width specifies the minimum number of characters that will be written to the result for this field.</p>
<p>Examples of width:</p>
<pre class="ruby"><span class="ruby-comment"># padding is done by spaces, width=20</span>
<span class="ruby-comment"># 0 or radix-1. <------------------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+20d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " +123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%020d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "00000000000000000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+020d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "+0000000000000000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"% 020d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 0000000000000000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%-20d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "123 "</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%-+20d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "+123 "</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%- 20d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 123 "</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%020x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "..ffffffffffffffff85"</span>
</pre>
<p>For numeric fields, the precision controls the number of decimal places displayed. For string fields, the precision determines the maximum number of characters to be copied from the string. (Thus, the format sequence <code>%10.10s</code> will always contribute exactly ten characters to the result.)</p>
<p>Examples of precisions:</p>
<pre class="ruby"><span class="ruby-comment"># precision for `d', 'o', 'x' and 'b' is</span>
<span class="ruby-comment"># minimum number of digits <------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 00000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8o"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 00000173"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8x"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 0000007b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8b"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 01111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8d"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " -00000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8o"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " ..777605"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " ..ffff85"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8b"</span>, <span class="ruby-value">-11</span>) <span class="ruby-comment">#=> " ..110101"</span>
<span class="ruby-comment"># "0x" and "0b" for `#x' and `#b' is not counted for</span>
<span class="ruby-comment"># precision but "0" for `#o' is counted. <------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 00000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8o"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 00000173"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8x"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 0x0000007b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8b"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 0b01111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8d"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " -00000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8o"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " ..777605"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " 0x..ffff85"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8b"</span>, <span class="ruby-value">-11</span>) <span class="ruby-comment">#=> " 0b..110101"</span>
<span class="ruby-comment"># precision for `e' is number of</span>
<span class="ruby-comment"># digits after the decimal point <------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8e"</span>, <span class="ruby-value">1234.56789</span>) <span class="ruby-comment">#=> " 1.23456789e+03"</span>
<span class="ruby-comment"># precision for `f' is number of</span>
<span class="ruby-comment"># digits after the decimal point <------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8f"</span>, <span class="ruby-value">1234.56789</span>) <span class="ruby-comment">#=> " 1234.56789000"</span>
<span class="ruby-comment"># precision for `g' is number of</span>
<span class="ruby-comment"># significant digits <-------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8g"</span>, <span class="ruby-value">1234.56789</span>) <span class="ruby-comment">#=> " 1234.5679"</span>
<span class="ruby-comment"># <-------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8g"</span>, <span class="ruby-value">123456789</span>) <span class="ruby-comment">#=> " 1.2345679e+08"</span>
<span class="ruby-comment"># precision for `s' is</span>
<span class="ruby-comment"># maximum number of characters <------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8s"</span>, <span class="ruby-string">"string test"</span>) <span class="ruby-comment">#=> " string t"</span>
</pre>
<p>Examples:</p>
<pre class="ruby"><span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%d %04x"</span>, <span class="ruby-value">123</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "123 007b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%08b '%4s'"</span>, <span class="ruby-value">123</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "01111011 ' 123'"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%1$*2$s %2$d %1$s"</span>, <span class="ruby-string">"hello"</span>, <span class="ruby-value">8</span>) <span class="ruby-comment">#=> " hello 8 hello"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%1$*2$s %2$d"</span>, <span class="ruby-string">"hello"</span>, <span class="ruby-value">-8</span>) <span class="ruby-comment">#=> "hello -8"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+g:% g:%-g"</span>, <span class="ruby-value">1.23</span>, <span class="ruby-value">1.23</span>, <span class="ruby-value">1.23</span>) <span class="ruby-comment">#=> "+1.23: 1.23:1.23"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%u"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "-123"</span>
</pre>
<p>For more complex formatting, Ruby supports a reference by name. %<name>s style uses format style, but %{name} style doesn't.</p>
<p>Examples:</p>
<pre class="ruby"><span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%<foo>d : %<bar>f"</span>, { <span class="ruby-value">:foo</span> <span class="ruby-operator">=></span> <span class="ruby-value">1</span>, <span class="ruby-value">:bar</span> <span class="ruby-operator">=></span> <span class="ruby-value">2</span> })
<span class="ruby-comment">#=> 1 : 2.000000</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%{foo}f"</span>, { <span class="ruby-value">:foo</span> <span class="ruby-operator">=></span> <span class="ruby-value">1</span> })
<span class="ruby-comment"># => "1f"</span>
</pre>
<div class="method-source-code" id="format-source">
<pre>static VALUE
f_sprintf(int c, const VALUE *v, VALUE _)
{
return rb_f_sprintf(c, v);
}</pre>
</div>
</div>
</div>
<div id="method-i-gem_original_require" class="method-detail method-alias">
<div class="method-heading">
<span class="method-name">gem_original_require</span><span
class="method-args">(path)</span>
</div>
<div class="method-description">
</div>
<div class="aliases">
Alias for: <a href="Kernel.html#method-i-require">require</a>
</div>
</div>
<div id="method-i-gets" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
gets(sep=$/ [, getline_args]) → string or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
gets(limit [, getline_args]) → string or nil
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
gets(sep, limit [, getline_args]) → string or nil
</span>
</div>
<div class="method-description">
<p>Returns (and assigns to <code>$_</code>) the next line from the list of files in <code>ARGV</code> (or <code>$*</code>), or from standard input if no files are present on the command line. Returns <code>nil</code> at end of file. The optional argument specifies the record separator. The separator is included with the contents of each record. A separator of <code>nil</code> 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 <code>ARGV</code>, <code>gets(nil)</code> will read the contents one file at a time.</p>
<pre class="ruby"><span class="ruby-constant">ARGV</span> <span class="ruby-operator"><<</span> <span class="ruby-string">"testfile"</span>
<span class="ruby-identifier">print</span> <span class="ruby-keyword">while</span> <span class="ruby-identifier">gets</span>
</pre>
<p><em>produces:</em></p>
<pre class="ruby"><span class="ruby-constant">This</span> <span class="ruby-identifier">is</span> <span class="ruby-identifier">line</span> <span class="ruby-identifier">one</span>
<span class="ruby-constant">This</span> <span class="ruby-identifier">is</span> <span class="ruby-identifier">line</span> <span class="ruby-identifier">two</span>
<span class="ruby-constant">This</span> <span class="ruby-identifier">is</span> <span class="ruby-identifier">line</span> <span class="ruby-identifier">three</span>
<span class="ruby-constant">And</span> <span class="ruby-identifier">so</span> <span class="ruby-identifier">on</span><span class="ruby-operator">...</span>
</pre>
<p>The style of programming using <code>$_</code> as an implicit parameter is gradually losing favor in the Ruby community.</p>
<div class="method-source-code" id="gets-source">
<pre>static VALUE
rb_f_gets(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
return argf_gets(argc, argv, argf);
}
return rb_funcallv(argf, idGets, argc, argv);
}</pre>
</div>
</div>
</div>
<div id="method-i-global_variables" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
global_variables → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an array of the names of global variables.</p>
<pre class="ruby"><span class="ruby-identifier">global_variables</span>.<span class="ruby-identifier">grep</span> <span class="ruby-regexp">/std/</span> <span class="ruby-comment">#=> [:$stdin, :$stdout, :$stderr]</span>
</pre>
<div class="method-source-code" id="global_variables-source">
<pre>static VALUE
f_global_variables(VALUE _)
{
return rb_f_global_variables();
}</pre>
</div>
</div>
</div>
<div id="method-i-gsub" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
gsub(pattern, replacement) → $_
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
gsub(pattern) {|...| block } → $_
</span>
</div>
<div class="method-description">
<p>Equivalent to <code>$_.gsub...</code>, except that <code>$_</code> will be updated if substitution occurs. Available only when -p/-n command line option specified.</p>
<div class="method-source-code" id="gsub-source">
<pre>static VALUE
rb_f_gsub(int argc, VALUE *argv, VALUE _)
{
VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("gsub"), argc, argv);
rb_lastline_set(str);
return str;
}</pre>
</div>
</div>
</div>
<div id="method-i-iterator-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
iterator? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if <code>yield</code> would execute a block in the current context. The <code>iterator?</code> form is mildly deprecated.</p>
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">try</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
<span class="ruby-keyword">yield</span>
<span class="ruby-keyword">else</span>
<span class="ruby-string">"no block"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">try</span> <span class="ruby-comment">#=> "no block"</span>
<span class="ruby-identifier">try</span> { <span class="ruby-string">"hello"</span> } <span class="ruby-comment">#=> "hello"</span>
<span class="ruby-identifier">try</span> <span class="ruby-keyword">do</span> <span class="ruby-string">"hello"</span> <span class="ruby-keyword">end</span> <span class="ruby-comment">#=> "hello"</span>
</pre>
<div class="method-source-code" id="iterator-3F-source">
<pre>static VALUE
rb_f_block_given_p(VALUE _)
{
rb_execution_context_t *ec = GET_EC();
rb_control_frame_t *cfp = ec->cfp;
cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
if (cfp != NULL && VM_CF_BLOCK_HANDLER(cfp) != VM_BLOCK_HANDLER_NONE) {
return Qtrue;
}
else {
return Qfalse;
}
}</pre>
</div>
</div>
</div>
<div id="method-i-lambda" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
lambda { |...| block } → a_proc
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Equivalent to <a href="Proc.html#method-c-new"><code>Proc.new</code></a>, except the resulting <a href="Proc.html"><code>Proc</code></a> objects check the number of parameters passed when called.</p>
<div class="method-source-code" id="lambda-source">
<pre>static VALUE
f_lambda(VALUE _)
{
return rb_block_lambda();
}</pre>
</div>
</div>
</div>
<div id="method-i-load" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
load(filename, wrap=false) → true
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Loads and executes the Ruby program in the file <em>filename</em>. If the filename does not resolve to an absolute path, the file is searched for in the library directories listed in <code>$:</code>. If the optional <em>wrap</em> parameter is <code>true</code>, the loaded script will be executed under an anonymous module, protecting the calling program's global namespace. In no circumstance will any local variables in the loaded file be propagated to the loading environment.</p>
<div class="method-source-code" id="load-source">
<pre>static VALUE
rb_f_load(int argc, VALUE *argv, VALUE _)
{
VALUE fname, wrap, path, orig_fname;
rb_scan_args(argc, argv, "11", &fname, &wrap);
orig_fname = rb_get_path_check_to_string(fname);
fname = rb_str_encode_ospath(orig_fname);
RUBY_DTRACE_HOOK(LOAD_ENTRY, RSTRING_PTR(orig_fname));
path = rb_find_file(fname);
if (!path) {
if (!rb_file_load_ok(RSTRING_PTR(fname)))
load_failed(orig_fname);
path = fname;
}
rb_load_internal(path, RTEST(wrap));
RUBY_DTRACE_HOOK(LOAD_RETURN, RSTRING_PTR(orig_fname));
return Qtrue;
}</pre>
</div>
</div>
</div>
<div id="method-i-local_variables" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
local_variables → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the names of the current local variables.</p>
<pre class="ruby"><span class="ruby-identifier">fred</span> = <span class="ruby-value">1</span>
<span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> <span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">10</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">local_variables</span> <span class="ruby-comment">#=> [:fred, :i]</span>
</pre>
<div class="method-source-code" id="local_variables-source">
<pre>static VALUE
rb_f_local_variables(VALUE _)
{
struct local_var_list vars;
rb_execution_context_t *ec = GET_EC();
rb_control_frame_t *cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp));
unsigned int i;
local_var_list_init(&vars);
while (cfp) {
if (cfp->iseq) {
for (i = 0; i < cfp->iseq->body->local_table_size; i++) {
local_var_list_add(&vars, cfp->iseq->body->local_table[i]);
}
}
if (!VM_ENV_LOCAL_P(cfp->ep)) {
/* block */
const VALUE *ep = VM_CF_PREV_EP(cfp);
if (vm_collect_local_variables_in_heap(ep, &vars)) {
break;
}
else {
while (cfp->ep != ep) {
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
}
}
}
else {
break;
}
}
return local_var_list_finish(&vars);
}</pre>
</div>
</div>
</div>
<div id="method-i-loop" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
loop { block }
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
loop → an_enumerator
</span>
</div>
<div class="method-description">
<p>Repeatedly executes the block.</p>
<p>If no block is given, an enumerator is returned instead.</p>
<pre class="ruby"><span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>
<span class="ruby-identifier">print</span> <span class="ruby-string">"Input: "</span>
<span class="ruby-identifier">line</span> = <span class="ruby-identifier">gets</span>
<span class="ruby-keyword">break</span> <span class="ruby-keyword">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">line</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">line</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/^qQ/</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>
<p><a href="StopIteration.html"><code>StopIteration</code></a> raised in the block breaks the loop. In this case, loop returns the “result” value stored in the exception.</p>
<pre class="ruby"><span class="ruby-identifier">enum</span> = <span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">new</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">y</span> <span class="ruby-operator"><<</span> <span class="ruby-string">"one"</span>
<span class="ruby-identifier">y</span> <span class="ruby-operator"><<</span> <span class="ruby-string">"two"</span>
<span class="ruby-value">:ok</span>
}
<span class="ruby-identifier">result</span> = <span class="ruby-identifier">loop</span> {
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">enum</span>.<span class="ruby-identifier">next</span>
} <span class="ruby-comment">#=> :ok</span>
</pre>
<div class="method-source-code" id="loop-source">
<pre>static VALUE
rb_f_loop(VALUE self)
{
RETURN_SIZED_ENUMERATOR(self, 0, 0, rb_f_loop_size);
return rb_rescue2(loop_i, (VALUE)0, loop_stop, (VALUE)0, rb_eStopIteration, (VALUE)0);
}</pre>
</div>
</div>
</div>
<div id="method-i-p" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
p(obj) → obj
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
p(obj1, obj2, ...) → [obj, ...]
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
p() → nil
</span>
</div>
<div class="method-description">
<p>For each object, directly writes <em>obj</em>.<code>inspect</code> followed by a newline to the program's standard output.</p>
<pre class="ruby"><span class="ruby-constant">S</span> = <span class="ruby-constant">Struct</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:name</span>, <span class="ruby-value">:state</span>)
<span class="ruby-identifier">s</span> = <span class="ruby-constant">S</span>[<span class="ruby-string">'dave'</span>, <span class="ruby-string">'TX'</span>]
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s</span>
</pre>
<p><em>produces:</em></p>
<pre class="ruby"><span class="ruby-comment">#<S name="dave", state="TX"></span>
</pre>
<div class="method-source-code" id="p-source">
<pre>static VALUE
rb_f_p(int argc, VALUE *argv, VALUE self)
{
struct rb_f_p_arg arg;
arg.argc = argc;
arg.argv = argv;
return rb_uninterruptible(rb_f_p_internal, (VALUE)&arg);
}</pre>
</div>
</div>
</div>
<div id="method-i-pretty_inspect" class="method-detail ">
<div class="method-heading">
<span class="method-name">pretty_inspect</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns a pretty printed object as a string.</p>
<p>In order to use this method you must first require the <a href="PP.html"><code>PP</code></a> module:</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'pp'</span>
</pre>
<p>See the <a href="PP.html"><code>PP</code></a> module for more information.</p>
<div class="method-source-code" id="pretty_inspect-source">
<pre><span class="ruby-comment"># File lib/pp.rb, line 590</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pretty_inspect</span>
<span class="ruby-constant">PP</span>.<span class="ruby-identifier">pp</span>(<span class="ruby-keyword">self</span>, <span class="ruby-string">''</span>.<span class="ruby-identifier">dup</span>)
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-print" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
print(obj, ...) → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Prints each object in turn to <code>$stdout</code>. If the output field separator (<code>$,</code>) is not <code>nil</code>, its contents will appear between each field. If the output record separator (<code>$\</code>) is not <code>nil</code>, it will be appended to the output. If no arguments are given, prints <code>$_</code>. Objects that aren't strings will be converted by calling their <code>to_s</code> method.</p>
<pre class="ruby"><span class="ruby-identifier">print</span> <span class="ruby-string">"cat"</span>, [<span class="ruby-value">1</span>,<span class="ruby-value">2</span>,<span class="ruby-value">3</span>], <span class="ruby-value">99</span>, <span class="ruby-string">"\n"</span>
<span class="ruby-identifier">$,</span> = <span class="ruby-string">", "</span>
<span class="ruby-identifier">$\</span> = <span class="ruby-string">"\n"</span>
<span class="ruby-identifier">print</span> <span class="ruby-string">"cat"</span>, [<span class="ruby-value">1</span>,<span class="ruby-value">2</span>,<span class="ruby-value">3</span>], <span class="ruby-value">99</span>
</pre>
<p><em>produces:</em></p>
<pre>cat12399
cat, 1, 2, 3, 99</pre>
<div class="method-source-code" id="print-source">
<pre>static VALUE
rb_f_print(int argc, const VALUE *argv, VALUE _)
{
rb_io_print(argc, argv, rb_stdout);
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-printf" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
printf(io, string [, obj ... ]) → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
printf(string [, obj ... ]) → nil
</span>
</div>
<div class="method-description">
<p>Equivalent to:</p>
<pre>io.write(sprintf(string, obj, ...))</pre>
<p>or</p>
<pre>$stdout.write(sprintf(string, obj, ...))</pre>
<div class="method-source-code" id="printf-source">
<pre>static VALUE
rb_f_printf(int argc, VALUE *argv, VALUE _)
{
VALUE out;
if (argc == 0) return Qnil;
if (RB_TYPE_P(argv[0], T_STRING)) {
out = rb_stdout;
}
else {
out = argv[0];
argv++;
argc--;
}
rb_io_write(out, rb_f_sprintf(argc, argv));
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-proc" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
proc { |...| block } → a_proc
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Equivalent to <a href="Proc.html#method-c-new"><code>Proc.new</code></a>.</p>
<div class="method-source-code" id="proc-source">
<pre>static VALUE
f_proc(VALUE _)
{
return proc_new(rb_cProc, FALSE, TRUE);
}</pre>
</div>
</div>
</div>
<div id="method-i-putc" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
putc(int) → int
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Equivalent to:</p>
<pre class="ruby"><span class="ruby-identifier">$stdout</span>.<span class="ruby-identifier">putc</span>(<span class="ruby-identifier">int</span>)
</pre>
<p>Refer to the documentation for <a href="IO.html#method-i-putc"><code>IO#putc</code></a> for important information regarding multi-byte characters.</p>
<div class="method-source-code" id="putc-source">
<pre>static VALUE
rb_f_putc(VALUE recv, VALUE ch)
{
if (recv == rb_stdout) {
return rb_io_putc(recv, ch);
}
return rb_funcallv(rb_stdout, rb_intern("putc"), 1, &ch);
}</pre>
</div>
</div>
</div>
<div id="method-i-puts" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
puts(obj, ...) → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Equivalent to</p>
<pre>$stdout.puts(obj, ...)</pre>
<div class="method-source-code" id="puts-source">
<pre>static VALUE
rb_f_puts(int argc, VALUE *argv, VALUE recv)
{
if (recv == rb_stdout) {
return rb_io_puts(argc, argv, recv);
}
return rb_funcallv(rb_stdout, rb_intern("puts"), argc, argv);
}</pre>
</div>
</div>
</div>
<div id="method-i-raise" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
raise
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
raise(string, cause: $!)
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
raise(exception [, string [, array]], cause: $!)
</span>
</div>
<div class="method-description">
<p>With no arguments, raises the exception in <code>$!</code> or raises a <a href="RuntimeError.html"><code>RuntimeError</code></a> if <code>$!</code> is <code>nil</code>. With a single <code>String</code> argument, raises a <code>RuntimeError</code> with the string as a message. Otherwise, the first parameter should be an <code>Exception</code> class (or another object that returns an <code>Exception</code> object when sent an <code>exception</code> message). The optional second parameter sets the message associated with the exception (accessible via <a href="Exception.html#method-i-message"><code>Exception#message</code></a>), and the third parameter is an array of callback information (accessible via <a href="Exception.html#method-i-backtrace"><code>Exception#backtrace</code></a>). The <code>cause</code> of the generated exception (accessible via <a href="Exception.html#method-i-cause"><code>Exception#cause</code></a>) is automatically set to the “current” exception (<code>$!</code>), if any. An alternative value, either an <code>Exception</code> object or <code>nil</code>, can be specified via the <code>:cause</code> argument.</p>
<p>Exceptions are caught by the <code>rescue</code> clause of <code>begin...end</code> blocks.</p>
<pre class="ruby"><span class="ruby-identifier">raise</span> <span class="ruby-string">"Failed to create socket"</span>
<span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">"No parameters"</span>, <span class="ruby-identifier">caller</span>
</pre>
<div class="method-source-code" id="raise-source">
<pre>static VALUE
f_raise(int c, VALUE *v, VALUE _)
{
return rb_f_raise(c, v);
}</pre>
</div>
</div>
</div>
<div id="method-i-rand" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
rand(max=0) → number
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>If called without an argument, or if <code>max.to_i.abs == 0</code>, rand returns a pseudo-random floating point number between 0.0 and 1.0, including 0.0 and excluding 1.0.</p>
<pre class="ruby"><span class="ruby-identifier">rand</span> <span class="ruby-comment">#=> 0.2725926052826416</span>
</pre>
<p>When <code>max.abs</code> is greater than or equal to 1, <code>rand</code> returns a pseudo-random integer greater than or equal to 0 and less than <code>max.to_i.abs</code>.</p>
<pre class="ruby"><span class="ruby-identifier">rand</span>(<span class="ruby-value">100</span>) <span class="ruby-comment">#=> 12</span>
</pre>
<p>When <code>max</code> is a <a href="Range.html"><code>Range</code></a>, <code>rand</code> returns a random number where range.member?(number) == true.</p>
<p>Negative or floating point values for <code>max</code> are allowed, but may give surprising results.</p>
<pre class="ruby"><span class="ruby-identifier">rand</span>(<span class="ruby-value">-100</span>) <span class="ruby-comment"># => 87</span>
<span class="ruby-identifier">rand</span>(<span class="ruby-value">-0.5</span>) <span class="ruby-comment"># => 0.8130921818028143</span>
<span class="ruby-identifier">rand</span>(<span class="ruby-value">1.9</span>) <span class="ruby-comment"># equivalent to rand(1), which is always 0</span>
</pre>
<p><a href="Kernel.html#method-i-srand"><code>Kernel.srand</code></a> may be used to ensure that sequences of random numbers are reproducible between different runs of a program.</p>
<p>See also <a href="Random.html#method-c-rand"><code>Random.rand</code></a>.</p>
<div class="method-source-code" id="rand-source">
<pre>static VALUE
rb_f_rand(int argc, VALUE *argv, VALUE obj)
{
VALUE vmax;
rb_random_t *rnd = rand_start(&default_rand);
if (rb_check_arity(argc, 0, 1) && !NIL_P(vmax = argv[0])) {
VALUE v = rand_range(Qnil, rnd, vmax);
if (v != Qfalse) return v;
vmax = rb_to_int(vmax);
if (vmax != INT2FIX(0)) {
v = rand_int(Qnil, rnd, vmax, 0);
if (!NIL_P(v)) return v;
}
}
return DBL2NUM(genrand_real(&rnd->mt));
}</pre>
</div>
</div>
</div>
<div id="method-i-readline" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
readline(sep=$/) → string
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
readline(limit) → string
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
readline(sep, limit) → string
</span>
</div>
<div class="method-description">
<p>Equivalent to Kernel::gets, except <code>readline</code> raises <code>EOFError</code> at end of file.</p>
<div class="method-source-code" id="readline-source">
<pre>static VALUE
rb_f_readline(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
return argf_readline(argc, argv, argf);
}
return rb_funcallv(argf, rb_intern("readline"), argc, argv);
}</pre>
</div>
</div>
</div>
<div id="method-i-readlines" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
readlines(sep=$/) → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
readlines(limit) → array
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
readlines(sep, limit) → array
</span>
</div>
<div class="method-description">
<p>Returns an array containing the lines returned by calling <code>Kernel.gets(<em>sep</em>)</code> until the end of file.</p>
<div class="method-source-code" id="readlines-source">
<pre>static VALUE
rb_f_readlines(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
return argf_readlines(argc, argv, argf);
}
return rb_funcallv(argf, rb_intern("readlines"), argc, argv);
}</pre>
</div>
</div>
</div>
<div id="method-i-require_relative" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
require_relative(string) → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Ruby tries to load the library named <em>string</em> relative to the requiring file's path. If the file's path cannot be determined a <a href="LoadError.html"><code>LoadError</code></a> is raised. If a file is loaded <code>true</code> is returned and false otherwise.</p>
<div class="method-source-code" id="require_relative-source">
<pre>VALUE
rb_f_require_relative(VALUE obj, VALUE fname)
{
VALUE base = rb_current_realfilepath();
if (NIL_P(base)) {
rb_loaderror("cannot infer basepath");
}
base = rb_file_dirname(base);
return rb_require_string(rb_file_absolute_path(fname, base));
}</pre>
</div>
</div>
</div>
<div id="method-i-select" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
select(read_array [, write_array [, error_array [, timeout]]]) → array or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Calls select(2) system call. It monitors given arrays of <a href="IO.html"><code>IO</code></a> objects, waits until one or more of <a href="IO.html"><code>IO</code></a> objects are ready for reading, are ready for writing, and have pending exceptions respectively, and returns an array that contains arrays of those <a href="IO.html"><code>IO</code></a> objects. It will return <code>nil</code> if optional <em>timeout</em> value is given and no <a href="IO.html"><code>IO</code></a> object is ready in <em>timeout</em> seconds.</p>
<p><a href="IO.html#method-c-select"><code>IO.select</code></a> peeks the buffer of <a href="IO.html"><code>IO</code></a> objects for testing readability. If the <a href="IO.html"><code>IO</code></a> buffer is not empty, <a href="IO.html#method-c-select"><code>IO.select</code></a> immediately notifies readability. This “peek” only happens for <a href="IO.html"><code>IO</code></a> objects. It does not happen for IO-like objects such as <a href="OpenSSL/SSL/SSLSocket.html"><code>OpenSSL::SSL::SSLSocket</code></a>.</p>
<p>The best way to use <a href="IO.html#method-c-select"><code>IO.select</code></a> is invoking it after nonblocking methods such as read_nonblock, write_nonblock, etc. The methods raise an exception which is extended by <a href="IO/WaitReadable.html"><code>IO::WaitReadable</code></a> or <a href="IO/WaitWritable.html"><code>IO::WaitWritable</code></a>. The modules notify how the caller should wait with <a href="IO.html#method-c-select"><code>IO.select</code></a>. If <a href="IO/WaitReadable.html"><code>IO::WaitReadable</code></a> is raised, the caller should wait for reading. If <a href="IO/WaitWritable.html"><code>IO::WaitWritable</code></a> is raised, the caller should wait for writing.</p>
<p>So, blocking read (readpartial) can be emulated using read_nonblock and <a href="IO.html#method-c-select"><code>IO.select</code></a> as follows:</p>
<pre class="ruby"><span class="ruby-keyword">begin</span>
<span class="ruby-identifier">result</span> = <span class="ruby-identifier">io_like</span>.<span class="ruby-identifier">read_nonblock</span>(<span class="ruby-identifier">maxlen</span>)
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">IO</span><span class="ruby-operator">::</span><span class="ruby-constant">WaitReadable</span>
<span class="ruby-constant">IO</span>.<span class="ruby-identifier">select</span>([<span class="ruby-identifier">io_like</span>])
<span class="ruby-keyword">retry</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">IO</span><span class="ruby-operator">::</span><span class="ruby-constant">WaitWritable</span>
<span class="ruby-constant">IO</span>.<span class="ruby-identifier">select</span>(<span class="ruby-keyword">nil</span>, [<span class="ruby-identifier">io_like</span>])
<span class="ruby-keyword">retry</span>
<span class="ruby-keyword">end</span>
</pre>
<p>Especially, the combination of nonblocking methods and <a href="IO.html#method-c-select"><code>IO.select</code></a> is preferred for <a href="IO.html"><code>IO</code></a> like objects such as <a href="OpenSSL/SSL/SSLSocket.html"><code>OpenSSL::SSL::SSLSocket</code></a>. It has to_io method to return underlying <a href="IO.html"><code>IO</code></a> object. <a href="IO.html#method-c-select"><code>IO.select</code></a> calls to_io to obtain the file descriptor to wait.</p>
<p>This means that readability notified by <a href="IO.html#method-c-select"><code>IO.select</code></a> doesn't mean readability from <a href="OpenSSL/SSL/SSLSocket.html"><code>OpenSSL::SSL::SSLSocket</code></a> object.</p>
<p>The most likely situation is that <a href="OpenSSL/SSL/SSLSocket.html"><code>OpenSSL::SSL::SSLSocket</code></a> buffers some data. <a href="IO.html#method-c-select"><code>IO.select</code></a> doesn't see the buffer. So <a href="IO.html#method-c-select"><code>IO.select</code></a> can block when <a href="OpenSSL/Buffering.html#method-i-readpartial"><code>OpenSSL::SSL::SSLSocket#readpartial</code></a> doesn't block.</p>
<p>However, several more complicated situations exist.</p>
<p>SSL is a protocol which is sequence of records. The record consists of multiple bytes. So, the remote side of SSL sends a partial record, <a href="IO.html#method-c-select"><code>IO.select</code></a> notifies readability but <a href="OpenSSL/SSL/SSLSocket.html"><code>OpenSSL::SSL::SSLSocket</code></a> cannot decrypt a byte and <a href="OpenSSL/Buffering.html#method-i-readpartial"><code>OpenSSL::SSL::SSLSocket#readpartial</code></a> will block.</p>
<p>Also, the remote side can request SSL renegotiation which forces the local SSL engine to write some data. This means <a href="OpenSSL/Buffering.html#method-i-readpartial"><code>OpenSSL::SSL::SSLSocket#readpartial</code></a> may invoke write system call and it can block. In such a situation, <a href="OpenSSL/Buffering.html#method-i-read_nonblock"><code>OpenSSL::SSL::SSLSocket#read_nonblock</code></a> raises <a href="IO/WaitWritable.html"><code>IO::WaitWritable</code></a> instead of blocking. So, the caller should wait for ready for writability as above example.</p>
<p>The combination of nonblocking methods and <a href="IO.html#method-c-select"><code>IO.select</code></a> is also useful for streams such as tty, pipe socket socket when multiple processes read from a stream.</p>
<p>Finally, Linux kernel developers don't guarantee that readability of select(2) means readability of following read(2) even for a single process. See select(2) manual on GNU/Linux system.</p>
<p>Invoking <a href="IO.html#method-c-select"><code>IO.select</code></a> before <a href="IO.html#method-i-readpartial"><code>IO#readpartial</code></a> works well as usual. However it is not the best way to use <a href="IO.html#method-c-select"><code>IO.select</code></a>.</p>
<p>The writability notified by select(2) doesn't show how many bytes are writable. <a href="IO.html#method-i-write"><code>IO#write</code></a> method blocks until given whole string is written. So, <code>IO#write(two or more bytes)</code> can block after writability is notified by <a href="IO.html#method-c-select"><code>IO.select</code></a>. <a href="IO.html#method-i-write_nonblock"><code>IO#write_nonblock</code></a> is required to avoid the blocking.</p>
<p>Blocking write (write) can be emulated using write_nonblock and <a href="IO.html#method-c-select"><code>IO.select</code></a> as follows: <a href="IO/WaitReadable.html"><code>IO::WaitReadable</code></a> should also be rescued for SSL renegotiation in <a href="OpenSSL/SSL/SSLSocket.html"><code>OpenSSL::SSL::SSLSocket</code></a>.</p>
<pre class="ruby"><span class="ruby-keyword">while</span> <span class="ruby-value">0</span> <span class="ruby-operator"><</span> <span class="ruby-identifier">string</span>.<span class="ruby-identifier">bytesize</span>
<span class="ruby-keyword">begin</span>
<span class="ruby-identifier">written</span> = <span class="ruby-identifier">io_like</span>.<span class="ruby-identifier">write_nonblock</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">IO</span><span class="ruby-operator">::</span><span class="ruby-constant">WaitReadable</span>
<span class="ruby-constant">IO</span>.<span class="ruby-identifier">select</span>([<span class="ruby-identifier">io_like</span>])
<span class="ruby-keyword">retry</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">IO</span><span class="ruby-operator">::</span><span class="ruby-constant">WaitWritable</span>
<span class="ruby-constant">IO</span>.<span class="ruby-identifier">select</span>(<span class="ruby-keyword">nil</span>, [<span class="ruby-identifier">io_like</span>])
<span class="ruby-keyword">retry</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">string</span> = <span class="ruby-identifier">string</span>.<span class="ruby-identifier">byteslice</span>(<span class="ruby-identifier">written</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>)
<span class="ruby-keyword">end</span>
</pre>
<h3 id="method-i-select-label-Parameters">Parameters<span><a href="#method-i-select-label-Parameters">¶</a> <a href="#top">↑</a></span></h3>
<dl class="rdoc-list note-list"><dt>read_array
<dd>
<p>an array of <a href="IO.html"><code>IO</code></a> objects that wait until ready for read</p>
</dd><dt>write_array
<dd>
<p>an array of <a href="IO.html"><code>IO</code></a> objects that wait until ready for write</p>
</dd><dt>error_array
<dd>
<p>an array of <a href="IO.html"><code>IO</code></a> objects that wait for exceptions</p>
</dd><dt>timeout
<dd>
<p>a numeric value in second</p>
</dd></dl>
<h3 id="method-i-select-label-Example">Example<span><a href="#method-i-select-label-Example">¶</a> <a href="#top">↑</a></span></h3>
<pre class="ruby"><span class="ruby-identifier">rp</span>, <span class="ruby-identifier">wp</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
<span class="ruby-identifier">mesg</span> = <span class="ruby-string">"ping "</span>
<span class="ruby-value">100</span>.<span class="ruby-identifier">times</span> {
<span class="ruby-comment"># IO.select follows IO#read. Not the best way to use IO.select.</span>
<span class="ruby-identifier">rs</span>, <span class="ruby-identifier">ws</span>, = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">select</span>([<span class="ruby-identifier">rp</span>], [<span class="ruby-identifier">wp</span>])
<span class="ruby-keyword">if</span> <span class="ruby-identifier">r</span> = <span class="ruby-identifier">rs</span>[<span class="ruby-value">0</span>]
<span class="ruby-identifier">ret</span> = <span class="ruby-identifier">r</span>.<span class="ruby-identifier">read</span>(<span class="ruby-value">5</span>)
<span class="ruby-identifier">print</span> <span class="ruby-identifier">ret</span>
<span class="ruby-keyword">case</span> <span class="ruby-identifier">ret</span>
<span class="ruby-keyword">when</span> <span class="ruby-regexp">/ping/</span>
<span class="ruby-identifier">mesg</span> = <span class="ruby-string">"pong\n"</span>
<span class="ruby-keyword">when</span> <span class="ruby-regexp">/pong/</span>
<span class="ruby-identifier">mesg</span> = <span class="ruby-string">"ping "</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">w</span> = <span class="ruby-identifier">ws</span>[<span class="ruby-value">0</span>]
<span class="ruby-identifier">w</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">mesg</span>)
<span class="ruby-keyword">end</span>
}
</pre>
<p><em>produces:</em></p>
<pre class="ruby"><span class="ruby-identifier">ping</span> <span class="ruby-identifier">pong</span>
<span class="ruby-identifier">ping</span> <span class="ruby-identifier">pong</span>
<span class="ruby-identifier">ping</span> <span class="ruby-identifier">pong</span>
(<span class="ruby-identifier">snipped</span>)
<span class="ruby-identifier">ping</span>
</pre>
<div class="method-source-code" id="select-source">
<pre>static VALUE
rb_f_select(int argc, VALUE *argv, VALUE obj)
{
VALUE timeout;
struct select_args args;
struct timeval timerec;
int i;
rb_scan_args(argc, argv, "13", &args.read, &args.write, &args.except, &timeout);
if (NIL_P(timeout)) {
args.timeout = 0;
}
else {
timerec = rb_time_interval(timeout);
args.timeout = &timerec;
}
for (i = 0; i < numberof(args.fdsets); ++i)
rb_fd_init(&args.fdsets[i]);
return rb_ensure(select_call, (VALUE)&args, select_end, (VALUE)&args);
}</pre>
</div>
</div>
</div>
<div id="method-i-set_trace_func" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
set_trace_func(proc) → proc
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
set_trace_func(nil) → nil
</span>
</div>
<div class="method-description">
<p>Establishes <em>proc</em> as the handler for tracing, or disables tracing if the parameter is <code>nil</code>.</p>
<p><strong>Note:</strong> this method is obsolete, please use <a href="TracePoint.html"><code>TracePoint</code></a> instead.</p>
<p><em>proc</em> takes up to six parameters:</p>
<ul><li>
<p>an event name</p>
</li><li>
<p>a filename</p>
</li><li>
<p>a line number</p>
</li><li>
<p>an object id</p>
</li><li>
<p>a binding</p>
</li><li>
<p>the name of a class</p>
</li></ul>
<p><em>proc</em> is invoked whenever an event occurs.</p>
<p>Events are:</p>
<dl class="rdoc-list note-list"><dt><code>c-call</code>
<dd>
<p>call a C-language routine</p>
</dd><dt><code>c-return</code>
<dd>
<p>return from a C-language routine</p>
</dd><dt><code>call</code>
<dd>
<p>call a Ruby method</p>
</dd><dt><code>class</code>
<dd>
<p>start a class or module definition</p>
</dd><dt><code>end</code>
<dd>
<p>finish a class or module definition</p>
</dd><dt><code>line</code>
<dd>
<p>execute code on a new line</p>
</dd><dt><code>raise</code>
<dd>
<p>raise an exception</p>
</dd><dt><code>return</code>
<dd>
<p>return from a Ruby method</p>
</dd></dl>
<p>Tracing is disabled within the context of <em>proc</em>.</p>
<pre> class Test
def test
a = 1
b = 2
end
end
set_trace_func proc { |event, file, line, id, binding, classname|
printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
}
t = Test.new
t.test
line prog.rb:11 false
c-call prog.rb:11 new Class
c-call prog.rb:11 initialize Object
c-return prog.rb:11 initialize Object
c-return prog.rb:11 new Class
line prog.rb:12 false
call prog.rb:2 test Test
line prog.rb:3 test Test
line prog.rb:4 test Test
return prog.rb:4 test Test</pre>
<div class="method-source-code" id="set_trace_func-source">
<pre>static VALUE
set_trace_func(VALUE obj, VALUE trace)
{
rb_remove_event_hook(call_trace_func);
if (NIL_P(trace)) {
return Qnil;
}
if (!rb_obj_is_proc(trace)) {
rb_raise(rb_eTypeError, "trace_func needs to be Proc");
}
rb_add_event_hook(call_trace_func, RUBY_EVENT_ALL, trace);
return trace;
}</pre>
</div>
</div>
</div>
<div id="method-i-sleep" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
sleep([duration]) → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Suspends the current thread for <em>duration</em> seconds (which may be any number, including a <code>Float</code> with fractional seconds). Returns the actual number of seconds slept (rounded), which may be less than that asked for if another thread calls <a href="Thread.html#method-i-run"><code>Thread#run</code></a>. Called without an argument, sleep() will sleep forever.</p>
<pre class="ruby"><span class="ruby-constant">Time</span>.<span class="ruby-identifier">new</span> <span class="ruby-comment">#=> 2008-03-08 19:56:19 +0900</span>
<span class="ruby-identifier">sleep</span> <span class="ruby-value">1.2</span> <span class="ruby-comment">#=> 1</span>
<span class="ruby-constant">Time</span>.<span class="ruby-identifier">new</span> <span class="ruby-comment">#=> 2008-03-08 19:56:20 +0900</span>
<span class="ruby-identifier">sleep</span> <span class="ruby-value">1.9</span> <span class="ruby-comment">#=> 2</span>
<span class="ruby-constant">Time</span>.<span class="ruby-identifier">new</span> <span class="ruby-comment">#=> 2008-03-08 19:56:22 +0900</span>
</pre>
<div class="method-source-code" id="sleep-source">
<pre>static VALUE
rb_f_sleep(int argc, VALUE *argv, VALUE _)
{
time_t beg, end;
beg = time(0);
if (argc == 0) {
rb_thread_sleep_forever();
}
else {
rb_check_arity(argc, 0, 1);
rb_thread_wait_for(rb_time_interval(argv[0]));
}
end = time(0) - beg;
return INT2FIX(end);
}</pre>
</div>
</div>
</div>
<div id="method-i-spawn" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
spawn([env,] command... [,options]) → pid
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
spawn([env,] command... [,options]) → pid
</span>
</div>
<div class="method-description">
<p>spawn executes specified command and return its pid.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-string">"tar xf ruby-2.0.0-p195.tar.bz2"</span>)
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">wait</span> <span class="ruby-identifier">pid</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-constant">RbConfig</span>.<span class="ruby-identifier">ruby</span>, <span class="ruby-string">"-eputs'Hello, world!'"</span>)
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">wait</span> <span class="ruby-identifier">pid</span>
</pre>
<p>This method is similar to <a href="Kernel.html#method-i-system"><code>Kernel#system</code></a> but it doesn't wait for the command to finish.</p>
<p>The parent process should use <a href="Process.html#method-c-wait"><code>Process.wait</code></a> to collect the termination status of its child or use <a href="Process.html#method-c-detach"><code>Process.detach</code></a> to register disinterest in their status; otherwise, the operating system may accumulate zombie processes.</p>
<p>spawn has bunch of options to specify process attributes:</p>
<pre>env: hash
name => val : set the environment variable
name => nil : unset the environment variable
the keys and the values except for +nil+ must be strings.
command...:
commandline : command line string which is passed to the standard shell
cmdname, arg1, ... : command name and one or more arguments (This form does not use the shell. See below for caveats.)
[cmdname, argv0], arg1, ... : command name, argv[0] and zero or more arguments (no shell)
options: hash
clearing environment variables:
:unsetenv_others => true : clear environment variables except specified by env
:unsetenv_others => false : don't clear (default)
process group:
:pgroup => true or 0 : make a new process group
:pgroup => pgid : join the specified process group
:pgroup => nil : don't change the process group (default)
create new process group: Windows only
:new_pgroup => true : the new process is the root process of a new process group
:new_pgroup => false : don't create a new process group (default)
resource limit: resourcename is core, cpu, data, etc. See Process.setrlimit.
:rlimit_resourcename => limit
:rlimit_resourcename => [cur_limit, max_limit]
umask:
:umask => int
redirection:
key:
FD : single file descriptor in child process
[FD, FD, ...] : multiple file descriptor in child process
value:
FD : redirect to the file descriptor in parent process
string : redirect to file with open(string, "r" or "w")
[string] : redirect to file with open(string, File::RDONLY)
[string, open_mode] : redirect to file with open(string, open_mode, 0644)
[string, open_mode, perm] : redirect to file with open(string, open_mode, perm)
[:child, FD] : redirect to the redirected file descriptor
:close : close the file descriptor in child process
FD is one of follows
:in : the file descriptor 0 which is the standard input
:out : the file descriptor 1 which is the standard output
:err : the file descriptor 2 which is the standard error
integer : the file descriptor of specified the integer
io : the file descriptor specified as io.fileno
file descriptor inheritance: close non-redirected non-standard fds (3, 4, 5, ...) or not
:close_others => false : inherit
current directory:
:chdir => str</pre>
<p>The <code>cmdname, arg1, ...</code> form does not use the shell. However, on different OSes, different things are provided as built-in commands. An example of this is +'echo'+, which is a built-in on Windows, but is a normal program on Linux and Mac OS X. This means that <code>Process.spawn 'echo', '%Path%'</code> will display the contents of the <code>%Path%</code> environment variable on Windows, but <code>Process.spawn 'echo', '$PATH'</code> prints the literal <code>$PATH</code>.</p>
<p>If a hash is given as <code>env</code>, the environment is updated by <code>env</code> before <code>exec(2)</code> in the child process. If a pair in <code>env</code> has nil as the value, the variable is deleted.</p>
<pre class="ruby"><span class="ruby-comment"># set FOO as BAR and unset BAZ.</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>({<span class="ruby-string">"FOO"</span><span class="ruby-operator">=></span><span class="ruby-string">"BAR"</span>, <span class="ruby-string">"BAZ"</span><span class="ruby-operator">=></span><span class="ruby-keyword">nil</span>}, <span class="ruby-identifier">command</span>)
</pre>
<p>If a hash is given as <code>options</code>, it specifies process group, create new process group, resource limit, current directory, umask and redirects for the child process. Also, it can be specified to clear environment variables.</p>
<p>The <code>:unsetenv_others</code> key in <code>options</code> specifies to clear environment variables, other than specified by <code>env</code>.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:unsetenv_others</span><span class="ruby-operator">=></span><span class="ruby-keyword">true</span>) <span class="ruby-comment"># no environment variable</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>({<span class="ruby-string">"FOO"</span><span class="ruby-operator">=></span><span class="ruby-string">"BAR"</span>}, <span class="ruby-identifier">command</span>, <span class="ruby-value">:unsetenv_others</span><span class="ruby-operator">=></span><span class="ruby-keyword">true</span>) <span class="ruby-comment"># FOO only</span>
</pre>
<p>The <code>:pgroup</code> key in <code>options</code> specifies a process group. The corresponding value should be true, zero, a positive integer, or nil. true and zero cause the process to be a process leader of a new process group. A non-zero positive integer causes the process to join the provided process group. The default value, nil, causes the process to remain in the same process group.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:pgroup</span><span class="ruby-operator">=></span><span class="ruby-keyword">true</span>) <span class="ruby-comment"># process leader</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:pgroup</span><span class="ruby-operator">=></span><span class="ruby-value">10</span>) <span class="ruby-comment"># belongs to the process group 10</span>
</pre>
<p>The <code>:new_pgroup</code> key in <code>options</code> specifies to pass <code>CREATE_NEW_PROCESS_GROUP</code> flag to <code>CreateProcessW()</code> that is Windows API. This option is only for Windows. true means the new process is the root process of the new process group. The new process has CTRL+C disabled. This flag is necessary for <code>Process.kill(:SIGINT, pid)</code> on the subprocess. :new_pgroup is false by default.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:new_pgroup</span><span class="ruby-operator">=></span><span class="ruby-keyword">true</span>) <span class="ruby-comment"># new process group</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:new_pgroup</span><span class="ruby-operator">=></span><span class="ruby-keyword">false</span>) <span class="ruby-comment"># same process group</span>
</pre>
<p>The <code>:rlimit_</code><em>foo</em> key specifies a resource limit. <em>foo</em> should be one of resource types such as <code>core</code>. The corresponding value should be an integer or an array which have one or two integers: same as cur_limit and max_limit arguments for <a href="Process.html#method-c-setrlimit"><code>Process.setrlimit</code></a>.</p>
<pre class="ruby"><span class="ruby-identifier">cur</span>, <span class="ruby-identifier">max</span> = <span class="ruby-constant">Process</span>.<span class="ruby-identifier">getrlimit</span>(<span class="ruby-value">:CORE</span>)
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:rlimit_core</span><span class="ruby-operator">=></span>[<span class="ruby-value">0</span>,<span class="ruby-identifier">max</span>]) <span class="ruby-comment"># disable core temporary.</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:rlimit_core</span><span class="ruby-operator">=></span><span class="ruby-identifier">max</span>) <span class="ruby-comment"># enable core dump</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:rlimit_core</span><span class="ruby-operator">=></span><span class="ruby-value">0</span>) <span class="ruby-comment"># never dump core.</span>
</pre>
<p>The <code>:umask</code> key in <code>options</code> specifies the umask.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:umask</span><span class="ruby-operator">=></span><span class="ruby-value">077</span>)
</pre>
<p>The :in, :out, :err, an integer, an <a href="IO.html"><code>IO</code></a> and an array key specifies a redirection. The redirection maps a file descriptor in the child process.</p>
<p>For example, stderr can be merged into stdout as follows:</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:err</span><span class="ruby-operator">=></span><span class="ruby-value">:out</span>)
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">2</span><span class="ruby-operator">=></span><span class="ruby-value">1</span>)
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-constant">STDERR</span><span class="ruby-operator">=></span><span class="ruby-value">:out</span>)
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-constant">STDERR</span><span class="ruby-operator">=></span><span class="ruby-constant">STDOUT</span>)
</pre>
<p>The hash keys specifies a file descriptor in the child process started by <a href="Kernel.html#method-i-spawn"><code>spawn</code></a>. :err, 2 and STDERR specifies the standard error stream (stderr).</p>
<p>The hash values specifies a file descriptor in the parent process which invokes <a href="Kernel.html#method-i-spawn"><code>spawn</code></a>. :out, 1 and STDOUT specifies the standard output stream (stdout).</p>
<p>In the above example, the standard output in the child process is not specified. So it is inherited from the parent process.</p>
<p>The standard input stream (stdin) can be specified by :in, 0 and STDIN.</p>
<p>A filename can be specified as a hash value.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:in</span><span class="ruby-operator">=></span><span class="ruby-string">"/dev/null"</span>) <span class="ruby-comment"># read mode</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span><span class="ruby-string">"/dev/null"</span>) <span class="ruby-comment"># write mode</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:err</span><span class="ruby-operator">=></span><span class="ruby-string">"log"</span>) <span class="ruby-comment"># write mode</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, [<span class="ruby-value">:out</span>, <span class="ruby-value">:err</span>]<span class="ruby-operator">=></span><span class="ruby-string">"/dev/null"</span>) <span class="ruby-comment"># write mode</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">3</span><span class="ruby-operator">=></span><span class="ruby-string">"/dev/null"</span>) <span class="ruby-comment"># read mode</span>
</pre>
<p>For stdout and stderr (and combination of them), it is opened in write mode. Otherwise read mode is used.</p>
<p>For specifying flags and permission of file creation explicitly, an array is used instead.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:in</span><span class="ruby-operator">=></span>[<span class="ruby-string">"file"</span>]) <span class="ruby-comment"># read mode is assumed</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:in</span><span class="ruby-operator">=></span>[<span class="ruby-string">"file"</span>, <span class="ruby-string">"r"</span>])
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span>[<span class="ruby-string">"log"</span>, <span class="ruby-string">"w"</span>]) <span class="ruby-comment"># 0644 assumed</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span>[<span class="ruby-string">"log"</span>, <span class="ruby-string">"w"</span>, <span class="ruby-value">0600</span>])
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span>[<span class="ruby-string">"log"</span>, <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">WRONLY</span><span class="ruby-operator">|</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">EXCL</span><span class="ruby-operator">|</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">CREAT</span>, <span class="ruby-value">0600</span>])
</pre>
<p>The array specifies a filename, flags and permission. The flags can be a string or an integer. If the flags is omitted or nil, File::RDONLY is assumed. The permission should be an integer. If the permission is omitted or nil, 0644 is assumed.</p>
<p>If an array of IOs and integers are specified as a hash key, all the elements are redirected.</p>
<pre class="ruby"><span class="ruby-comment"># stdout and stderr is redirected to log file.</span>
<span class="ruby-comment"># The file "log" is opened just once.</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, [<span class="ruby-value">:out</span>, <span class="ruby-value">:err</span>]<span class="ruby-operator">=></span>[<span class="ruby-string">"log"</span>, <span class="ruby-string">"w"</span>])
</pre>
<p>Another way to merge multiple file descriptors is [:child, fd]. [:child, fd] means the file descriptor in the child process. This is different from fd. For example, :err=>:out means redirecting child stderr to parent stdout. But :err=>[:child, :out] means redirecting child stderr to child stdout. They differ if stdout is redirected in the child process as follows.</p>
<pre class="ruby"><span class="ruby-comment"># stdout and stderr is redirected to log file.</span>
<span class="ruby-comment"># The file "log" is opened just once.</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span>[<span class="ruby-string">"log"</span>, <span class="ruby-string">"w"</span>], <span class="ruby-value">:err</span><span class="ruby-operator">=></span>[<span class="ruby-value">:child</span>, <span class="ruby-value">:out</span>])
</pre>
<p>[:child, :out] can be used to merge stderr into stdout in <a href="IO.html#method-c-popen"><code>IO.popen</code></a>. In this case, <a href="IO.html#method-c-popen"><code>IO.popen</code></a> redirects stdout to a pipe in the child process and [:child, :out] refers the redirected stdout.</p>
<pre class="ruby"><span class="ruby-identifier">io</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">popen</span>([<span class="ruby-string">"sh"</span>, <span class="ruby-string">"-c"</span>, <span class="ruby-string">"echo out; echo err >&2"</span>, <span class="ruby-value">:err</span><span class="ruby-operator">=></span>[<span class="ruby-value">:child</span>, <span class="ruby-value">:out</span>]])
<span class="ruby-identifier">p</span> <span class="ruby-identifier">io</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment">#=> "out\nerr\n"</span>
</pre>
<p>The <code>:chdir</code> key in <code>options</code> specifies the current directory.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:chdir</span><span class="ruby-operator">=></span><span class="ruby-string">"/var/tmp"</span>)
</pre>
<p>spawn closes all non-standard unspecified descriptors by default. The “standard” descriptors are 0, 1 and 2. This behavior is specified by :close_others option. :close_others doesn't affect the standard descriptors which are closed only if :close is specified explicitly.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:close_others</span><span class="ruby-operator">=></span><span class="ruby-keyword">true</span>) <span class="ruby-comment"># close 3,4,5,... (default)</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:close_others</span><span class="ruby-operator">=></span><span class="ruby-keyword">false</span>) <span class="ruby-comment"># don't close 3,4,5,...</span>
</pre>
<p>:close_others is false by default for spawn and <a href="IO.html#method-c-popen"><code>IO.popen</code></a>.</p>
<p>Note that fds which close-on-exec flag is already set are closed regardless of :close_others option.</p>
<p>So <a href="IO.html#method-c-pipe"><code>IO.pipe</code></a> and spawn can be used as <a href="IO.html#method-c-popen"><code>IO.popen</code></a>.</p>
<pre class="ruby"><span class="ruby-comment"># similar to r = IO.popen(command)</span>
<span class="ruby-identifier">r</span>, <span class="ruby-identifier">w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span><span class="ruby-identifier">w</span>) <span class="ruby-comment"># r, w is closed in the child process.</span>
<span class="ruby-identifier">w</span>.<span class="ruby-identifier">close</span>
</pre>
<p>:close is specified as a hash value to close a fd individually.</p>
<pre class="ruby"><span class="ruby-identifier">f</span> = <span class="ruby-identifier">open</span>(<span class="ruby-identifier">foo</span>)
<span class="ruby-identifier">system</span>(<span class="ruby-identifier">command</span>, <span class="ruby-identifier">f</span><span class="ruby-operator">=></span><span class="ruby-value">:close</span>) <span class="ruby-comment"># don't inherit f.</span>
</pre>
<p>If a file descriptor need to be inherited, io=>io can be used.</p>
<pre class="ruby"><span class="ruby-comment"># valgrind has --log-fd option for log destination.</span>
<span class="ruby-comment"># log_w=>log_w indicates log_w.fileno inherits to child process.</span>
<span class="ruby-identifier">log_r</span>, <span class="ruby-identifier">log_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-string">"valgrind"</span>, <span class="ruby-node">"--log-fd=#{log_w.fileno}"</span>, <span class="ruby-string">"echo"</span>, <span class="ruby-string">"a"</span>, <span class="ruby-identifier">log_w</span><span class="ruby-operator">=></span><span class="ruby-identifier">log_w</span>)
<span class="ruby-identifier">log_w</span>.<span class="ruby-identifier">close</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">log_r</span>.<span class="ruby-identifier">read</span>
</pre>
<p>It is also possible to exchange file descriptors.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span><span class="ruby-value">:err</span>, <span class="ruby-value">:err</span><span class="ruby-operator">=></span><span class="ruby-value">:out</span>)
</pre>
<p>The hash keys specify file descriptors in the child process. The hash values specifies file descriptors in the parent process. So the above specifies exchanging stdout and stderr. Internally, <code>spawn</code> uses an extra file descriptor to resolve such cyclic file descriptor mapping.</p>
<p>See <a href="Kernel.html#method-i-exec"><code>Kernel.exec</code></a> for the standard shell.</p>
<div class="method-source-code" id="spawn-source">
<pre>static VALUE
rb_f_spawn(int argc, VALUE *argv, VALUE _)
{
rb_pid_t pid;
char errmsg[CHILD_ERRMSG_BUFLEN] = { '\0' };
VALUE execarg_obj, fail_str;
struct rb_execarg *eargp;
execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
eargp = rb_execarg_get(execarg_obj);
fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
pid = rb_execarg_spawn(execarg_obj, errmsg, sizeof(errmsg));
if (pid == -1) {
int err = errno;
rb_exec_fail(eargp, err, errmsg);
RB_GC_GUARD(execarg_obj);
rb_syserr_fail_str(err, fail_str);
}
#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
return PIDT2NUM(pid);
#else
return Qnil;
#endif
}</pre>
</div>
</div>
</div>
<div id="method-i-sprintf" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
sprintf(format_string [, arguments...] ) → string
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the string resulting from applying <em>format_string</em> to any additional arguments. Within the format string, any characters other than format sequences are copied to the result.</p>
<p>The syntax of a format sequence is as follows.</p>
<pre>%[flags][width][.precision]type</pre>
<p>A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character. The field type controls how the corresponding <code>sprintf</code> argument is to be interpreted, while the flags modify that interpretation.</p>
<p>The field type characters are:</p>
<pre>Field | Integer Format
------+--------------------------------------------------------------
b | Convert argument as a binary number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..1'.
B | Equivalent to `b', but uses an uppercase 0B for prefix
| in the alternative format by #.
d | Convert argument as a decimal number.
i | Identical to `d'.
o | Convert argument as an octal number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..7'.
u | Identical to `d'.
x | Convert argument as a hexadecimal number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..f' (representing an infinite string of
| leading 'ff's).
X | Equivalent to `x', but uses uppercase letters.
Field | Float Format
------+--------------------------------------------------------------
e | Convert floating point argument into exponential notation
| with one digit before the decimal point as [-]d.dddddde[+-]dd.
| The precision specifies the number of digits after the decimal
| point (defaulting to six).
E | Equivalent to `e', but uses an uppercase E to indicate
| the exponent.
f | Convert floating point argument as [-]ddd.dddddd,
| where the precision specifies the number of digits after
| the decimal point.
g | Convert a floating point number using exponential form
| if the exponent is less than -4 or greater than or
| equal to the precision, or in dd.dddd form otherwise.
| The precision specifies the number of significant digits.
G | Equivalent to `g', but use an uppercase `E' in exponent form.
a | Convert floating point argument as [-]0xh.hhhhp[+-]dd,
| which is consisted from optional sign, "0x", fraction part
| as hexadecimal, "p", and exponential part as decimal.
A | Equivalent to `a', but use uppercase `X' and `P'.
Field | Other Format
------+--------------------------------------------------------------
c | Argument is the numeric code for a single character or
| a single character string itself.
p | The valuing of argument.inspect.
s | Argument is a string to be substituted. If the format
| sequence contains a precision, at most that many characters
| will be copied.
% | A percent sign itself will be displayed. No argument taken.</pre>
<p>The flags modifies the behavior of the formats. The flag characters are:</p>
<pre>Flag | Applies to | Meaning
---------+---------------+-----------------------------------------
space | bBdiouxX | Leave a space at the start of
| aAeEfgG | non-negative numbers.
| (numeric fmt) | For `o', `x', `X', `b' and `B', use
| | a minus sign with absolute value for
| | negative values.
---------+---------------+-----------------------------------------
(digit)$ | all | Specifies the absolute argument number
| | for this field. Absolute and relative
| | argument numbers cannot be mixed in a
| | sprintf string.
---------+---------------+-----------------------------------------
# | bBoxX | Use an alternative format.
| aAeEfgG | For the conversions `o', increase the precision
| | until the first digit will be `0' if
| | it is not formatted as complements.
| | For the conversions `x', `X', `b' and `B'
| | on non-zero, prefix the result with ``0x'',
| | ``0X'', ``0b'' and ``0B'', respectively.
| | For `a', `A', `e', `E', `f', `g', and 'G',
| | force a decimal point to be added,
| | even if no digits follow.
| | For `g' and 'G', do not remove trailing zeros.
---------+---------------+-----------------------------------------
+ | bBdiouxX | Add a leading plus sign to non-negative
| aAeEfgG | numbers.
| (numeric fmt) | For `o', `x', `X', `b' and `B', use
| | a minus sign with absolute value for
| | negative values.
---------+---------------+-----------------------------------------
- | all | Left-justify the result of this conversion.
---------+---------------+-----------------------------------------
0 (zero) | bBdiouxX | Pad with zeros, not spaces.
| aAeEfgG | For `o', `x', `X', `b' and `B', radix-1
| (numeric fmt) | is used for negative numbers formatted as
| | complements.
---------+---------------+-----------------------------------------
* | all | Use the next argument as the field width.
| | If negative, left-justify the result. If the
| | asterisk is followed by a number and a dollar
| | sign, use the indicated argument as the width.</pre>
<p>Examples of flags:</p>
<pre class="ruby"><span class="ruby-comment"># `+' and space flag specifies the sign of non-negative numbers.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "+123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"% d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 123"</span>
<span class="ruby-comment"># `#' flag for `o' increases number of digits to show `0'.</span>
<span class="ruby-comment"># `+' and space flag changes format of negative numbers.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%o"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "173"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#o"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "0173"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+o"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "-173"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%o"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "..7605"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#o"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "..7605"</span>
<span class="ruby-comment"># `#' flag for `x' add a prefix `0x' for non-zero numbers.</span>
<span class="ruby-comment"># `+' and space flag disables complements for negative numbers.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%x"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "7b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#x"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "0x7b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "-7b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "..f85"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "0x..f85"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#x"</span>, <span class="ruby-value">0</span>) <span class="ruby-comment">#=> "0"</span>
<span class="ruby-comment"># `#' for `X' uses the prefix `0X'.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%X"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "7B"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#X"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "0X7B"</span>
<span class="ruby-comment"># `#' flag for `b' add a prefix `0b' for non-zero numbers.</span>
<span class="ruby-comment"># `+' and space flag disables complements for negative numbers.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%b"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "1111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#b"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "0b1111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+b"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "-1111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%b"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "..10000101"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#b"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "0b..10000101"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#b"</span>, <span class="ruby-value">0</span>) <span class="ruby-comment">#=> "0"</span>
<span class="ruby-comment"># `#' for `B' uses the prefix `0B'.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%B"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "1111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#B"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "0B1111011"</span>
<span class="ruby-comment"># `#' for `e' forces to show the decimal point.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%.0e"</span>, <span class="ruby-value">1</span>) <span class="ruby-comment">#=> "1e+00"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#.0e"</span>, <span class="ruby-value">1</span>) <span class="ruby-comment">#=> "1.e+00"</span>
<span class="ruby-comment"># `#' for `f' forces to show the decimal point.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%.0f"</span>, <span class="ruby-value">1234</span>) <span class="ruby-comment">#=> "1234"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#.0f"</span>, <span class="ruby-value">1234</span>) <span class="ruby-comment">#=> "1234."</span>
<span class="ruby-comment"># `#' for `g' forces to show the decimal point.</span>
<span class="ruby-comment"># It also disables stripping lowest zeros.</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%g"</span>, <span class="ruby-value">123.4</span>) <span class="ruby-comment">#=> "123.4"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#g"</span>, <span class="ruby-value">123.4</span>) <span class="ruby-comment">#=> "123.400"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%g"</span>, <span class="ruby-value">123456</span>) <span class="ruby-comment">#=> "123456"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#g"</span>, <span class="ruby-value">123456</span>) <span class="ruby-comment">#=> "123456."</span>
</pre>
<p>The field width is an optional integer, followed optionally by a period and a precision. The width specifies the minimum number of characters that will be written to the result for this field.</p>
<p>Examples of width:</p>
<pre class="ruby"><span class="ruby-comment"># padding is done by spaces, width=20</span>
<span class="ruby-comment"># 0 or radix-1. <------------------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+20d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " +123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%020d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "00000000000000000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+020d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "+0000000000000000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"% 020d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 0000000000000000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%-20d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "123 "</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%-+20d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "+123 "</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%- 20d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 123 "</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%020x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "..ffffffffffffffff85"</span>
</pre>
<p>For numeric fields, the precision controls the number of decimal places displayed. For string fields, the precision determines the maximum number of characters to be copied from the string. (Thus, the format sequence <code>%10.10s</code> will always contribute exactly ten characters to the result.)</p>
<p>Examples of precisions:</p>
<pre class="ruby"><span class="ruby-comment"># precision for `d', 'o', 'x' and 'b' is</span>
<span class="ruby-comment"># minimum number of digits <------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 00000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8o"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 00000173"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8x"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 0000007b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8b"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 01111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8d"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " -00000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8o"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " ..777605"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " ..ffff85"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8b"</span>, <span class="ruby-value">-11</span>) <span class="ruby-comment">#=> " ..110101"</span>
<span class="ruby-comment"># "0x" and "0b" for `#x' and `#b' is not counted for</span>
<span class="ruby-comment"># precision but "0" for `#o' is counted. <------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8d"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 00000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8o"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 00000173"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8x"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 0x0000007b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8b"</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> " 0b01111011"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8d"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " -00000123"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8o"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " ..777605"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8x"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> " 0x..ffff85"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%#20.8b"</span>, <span class="ruby-value">-11</span>) <span class="ruby-comment">#=> " 0b..110101"</span>
<span class="ruby-comment"># precision for `e' is number of</span>
<span class="ruby-comment"># digits after the decimal point <------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8e"</span>, <span class="ruby-value">1234.56789</span>) <span class="ruby-comment">#=> " 1.23456789e+03"</span>
<span class="ruby-comment"># precision for `f' is number of</span>
<span class="ruby-comment"># digits after the decimal point <------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8f"</span>, <span class="ruby-value">1234.56789</span>) <span class="ruby-comment">#=> " 1234.56789000"</span>
<span class="ruby-comment"># precision for `g' is number of</span>
<span class="ruby-comment"># significant digits <-------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8g"</span>, <span class="ruby-value">1234.56789</span>) <span class="ruby-comment">#=> " 1234.5679"</span>
<span class="ruby-comment"># <-------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8g"</span>, <span class="ruby-value">123456789</span>) <span class="ruby-comment">#=> " 1.2345679e+08"</span>
<span class="ruby-comment"># precision for `s' is</span>
<span class="ruby-comment"># maximum number of characters <------></span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%20.8s"</span>, <span class="ruby-string">"string test"</span>) <span class="ruby-comment">#=> " string t"</span>
</pre>
<p>Examples:</p>
<pre class="ruby"><span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%d %04x"</span>, <span class="ruby-value">123</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "123 007b"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%08b '%4s'"</span>, <span class="ruby-value">123</span>, <span class="ruby-value">123</span>) <span class="ruby-comment">#=> "01111011 ' 123'"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%1$*2$s %2$d %1$s"</span>, <span class="ruby-string">"hello"</span>, <span class="ruby-value">8</span>) <span class="ruby-comment">#=> " hello 8 hello"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%1$*2$s %2$d"</span>, <span class="ruby-string">"hello"</span>, <span class="ruby-value">-8</span>) <span class="ruby-comment">#=> "hello -8"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%+g:% g:%-g"</span>, <span class="ruby-value">1.23</span>, <span class="ruby-value">1.23</span>, <span class="ruby-value">1.23</span>) <span class="ruby-comment">#=> "+1.23: 1.23:1.23"</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%u"</span>, <span class="ruby-value">-123</span>) <span class="ruby-comment">#=> "-123"</span>
</pre>
<p>For more complex formatting, Ruby supports a reference by name. %<name>s style uses format style, but %{name} style doesn't.</p>
<p>Examples:</p>
<pre class="ruby"><span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%<foo>d : %<bar>f"</span>, { <span class="ruby-value">:foo</span> <span class="ruby-operator">=></span> <span class="ruby-value">1</span>, <span class="ruby-value">:bar</span> <span class="ruby-operator">=></span> <span class="ruby-value">2</span> })
<span class="ruby-comment">#=> 1 : 2.000000</span>
<span class="ruby-identifier">sprintf</span>(<span class="ruby-string">"%{foo}f"</span>, { <span class="ruby-value">:foo</span> <span class="ruby-operator">=></span> <span class="ruby-value">1</span> })
<span class="ruby-comment"># => "1f"</span>
</pre>
<div class="method-source-code" id="sprintf-source">
<pre>static VALUE
f_sprintf(int c, const VALUE *v, VALUE _)
{
return rb_f_sprintf(c, v);
}</pre>
</div>
</div>
</div>
<div id="method-i-srand" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
srand(number = Random.new_seed) → old_seed
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Seeds the system pseudo-random number generator, Random::DEFAULT, with <code>number</code>. The previous seed value is returned.</p>
<p>If <code>number</code> is omitted, seeds the generator using a source of entropy provided by the operating system, if available (/dev/urandom on Unix systems or the RSA cryptographic provider on Windows), which is then combined with the time, the process id, and a sequence number.</p>
<p>srand may be used to ensure repeatable sequences of pseudo-random numbers between different runs of the program. By setting the seed to a known value, programs can be made deterministic during testing.</p>
<pre class="ruby"><span class="ruby-identifier">srand</span> <span class="ruby-value">1234</span> <span class="ruby-comment"># => 268519324636777531569100071560086917274</span>
[ <span class="ruby-identifier">rand</span>, <span class="ruby-identifier">rand</span> ] <span class="ruby-comment"># => [0.1915194503788923, 0.6221087710398319]</span>
[ <span class="ruby-identifier">rand</span>(<span class="ruby-value">10</span>), <span class="ruby-identifier">rand</span>(<span class="ruby-value">1000</span>) ] <span class="ruby-comment"># => [4, 664]</span>
<span class="ruby-identifier">srand</span> <span class="ruby-value">1234</span> <span class="ruby-comment"># => 1234</span>
[ <span class="ruby-identifier">rand</span>, <span class="ruby-identifier">rand</span> ] <span class="ruby-comment"># => [0.1915194503788923, 0.6221087710398319]</span>
</pre>
<div class="method-source-code" id="srand-source">
<pre>static VALUE
rb_f_srand(int argc, VALUE *argv, VALUE obj)
{
VALUE seed, old;
rb_random_t *r = &default_rand;
if (rb_check_arity(argc, 0, 1) == 0) {
seed = random_seed(obj);
}
else {
seed = rb_to_int(argv[0]);
}
old = r->seed;
r->seed = rand_init(&r->mt, seed);
return old;
}</pre>
</div>
</div>
</div>
<div id="method-i-sub" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
sub(pattern, replacement) → $_
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
sub(pattern) {|...| block } → $_
</span>
</div>
<div class="method-description">
<p>Equivalent to <code>$_.sub(<em>args</em>)</code>, except that <code>$_</code> will be updated if substitution occurs. Available only when -p/-n command line option specified.</p>
<div class="method-source-code" id="sub-source">
<pre>static VALUE
rb_f_sub(int argc, VALUE *argv, VALUE _)
{
VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("sub"), argc, argv);
rb_lastline_set(str);
return str;
}</pre>
</div>
</div>
</div>
<div id="method-i-syscall" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
syscall(num [, args...]) → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Calls the operating system function identified by <em>num</em> and returns the result of the function or raises <a href="SystemCallError.html"><code>SystemCallError</code></a> if it failed.</p>
<p>Arguments for the function can follow <em>num</em>. They must be either <code>String</code> objects or <code>Integer</code> objects. A <code>String</code> object is passed as a pointer to the byte sequence. An <code>Integer</code> object is passed as an integer whose bit size is same as a pointer. Up to nine parameters may be passed.</p>
<p>The function identified by <em>num</em> is system dependent. On some Unix systems, the numbers may be obtained from a header file called <code>syscall.h</code>.</p>
<pre class="ruby"><span class="ruby-identifier">syscall</span> <span class="ruby-value">4</span>, <span class="ruby-value">1</span>, <span class="ruby-string">"hello\n"</span>, <span class="ruby-value">6</span> <span class="ruby-comment"># '4' is write(2) on our box</span>
</pre>
<p><em>produces:</em></p>
<pre class="ruby"><span class="ruby-identifier">hello</span>
</pre>
<p>Calling <code>syscall</code> on a platform which does not have any way to an arbitrary system function just fails with <a href="NotImplementedError.html"><code>NotImplementedError</code></a>.</p>
<p><strong>Note:</strong> <code>syscall</code> is essentially unsafe and unportable. Feel free to shoot your foot. The DL (<a href="Fiddle.html"><code>Fiddle</code></a>) library is preferred for safer and a bit more portable programming.</p>
<div class="method-source-code" id="syscall-source">
<pre>static VALUE
rb_f_syscall(int argc, VALUE *argv, VALUE _)
{
VALUE arg[8];
#if SIZEOF_VOIDP == 8 && defined(HAVE___SYSCALL) && SIZEOF_INT != 8 /* mainly *BSD */
# define SYSCALL __syscall
# define NUM2SYSCALLID(x) NUM2LONG(x)
# define RETVAL2NUM(x) LONG2NUM(x)
# if SIZEOF_LONG == 8
long num, retval = -1;
# elif SIZEOF_LONG_LONG == 8
long long num, retval = -1;
# else
# error ---->> it is asserted that __syscall takes the first argument and returns retval in 64bit signed integer. <<----
# endif
#elif defined(__linux__)
# define SYSCALL syscall
# define NUM2SYSCALLID(x) NUM2LONG(x)
# define RETVAL2NUM(x) LONG2NUM(x)
/*
* Linux man page says, syscall(2) function prototype is below.
*
* int syscall(int number, ...);
*
* But, it's incorrect. Actual one takes and returned long. (see unistd.h)
*/
long num, retval = -1;
#else
# define SYSCALL syscall
# define NUM2SYSCALLID(x) NUM2INT(x)
# define RETVAL2NUM(x) INT2NUM(x)
int num, retval = -1;
#endif
int i;
if (RTEST(ruby_verbose)) {
rb_warning("We plan to remove a syscall function at future release. DL(Fiddle) provides safer alternative.");
}
if (argc == 0)
rb_raise(rb_eArgError, "too few arguments for syscall");
if (argc > numberof(arg))
rb_raise(rb_eArgError, "too many arguments for syscall");
num = NUM2SYSCALLID(argv[0]); ++argv;
for (i = argc - 1; i--; ) {
VALUE v = rb_check_string_type(argv[i]);
if (!NIL_P(v)) {
SafeStringValue(v);
rb_str_modify(v);
arg[i] = (VALUE)StringValueCStr(v);
}
else {
arg[i] = (VALUE)NUM2LONG(argv[i]);
}
}
switch (argc) {
case 1:
retval = SYSCALL(num);
break;
case 2:
retval = SYSCALL(num, arg[0]);
break;
case 3:
retval = SYSCALL(num, arg[0],arg[1]);
break;
case 4:
retval = SYSCALL(num, arg[0],arg[1],arg[2]);
break;
case 5:
retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3]);
break;
case 6:
retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4]);
break;
case 7:
retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
break;
case 8:
retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
break;
}
if (retval == -1)
rb_sys_fail(0);
return RETVAL2NUM(retval);
#undef SYSCALL
#undef NUM2SYSCALLID
#undef RETVAL2NUM
}</pre>
</div>
</div>
</div>
<div id="method-i-system" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
system([env,] command... [,options], exception: false) → true, false or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Executes <em>command…</em> in a subshell. <em>command…</em> is one of following forms.</p>
<dl class="rdoc-list label-list"><dt><code>commandline</code>
<dd>
<p>command line string which is passed to the standard shell</p>
</dd><dt><code>cmdname, arg1, ...</code>
<dd>
<p>command name and one or more arguments (no shell)</p>
</dd><dt><code>[cmdname, argv0], arg1, ...</code>
<dd>
<p>command name, <code>argv[0]</code> and zero or more arguments (no shell)</p>
</dd></dl>
<p>system returns <code>true</code> if the command gives zero exit status, <code>false</code> for non zero exit status. Returns <code>nil</code> if command execution fails. An error status is available in <code>$?</code>.</p>
<p>If the <code>exception: true</code> argument is passed, the method raises an exception instead of returning <code>false</code> or <code>nil</code>.</p>
<p>The arguments are processed in the same way as for <a href="Kernel.html#method-i-spawn"><code>Kernel#spawn</code></a>.</p>
<p>The hash arguments, env and options, are same as <a href="Kernel.html#method-i-exec"><code>exec</code></a> and <a href="Kernel.html#method-i-spawn"><code>spawn</code></a>. See <a href="Kernel.html#method-i-spawn"><code>Kernel#spawn</code></a> for details.</p>
<pre class="ruby"><span class="ruby-identifier">system</span>(<span class="ruby-string">"echo *"</span>)
<span class="ruby-identifier">system</span>(<span class="ruby-string">"echo"</span>, <span class="ruby-string">"*"</span>)
</pre>
<p><em>produces:</em></p>
<pre>config.h main.rb
*</pre>
<p><a href="Error.html"><code>Error</code></a> handling:</p>
<pre class="ruby"><span class="ruby-identifier">system</span>(<span class="ruby-string">"cat nonexistent.txt"</span>)
<span class="ruby-comment"># => false</span>
<span class="ruby-identifier">system</span>(<span class="ruby-string">"catt nonexistent.txt"</span>)
<span class="ruby-comment"># => nil</span>
<span class="ruby-identifier">system</span>(<span class="ruby-string">"cat nonexistent.txt"</span>, <span class="ruby-value">exception:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-comment"># RuntimeError (Command failed with exit 1: cat)</span>
<span class="ruby-identifier">system</span>(<span class="ruby-string">"catt nonexistent.txt"</span>, <span class="ruby-value">exception:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-comment"># Errno::ENOENT (No such file or directory - catt)</span>
</pre>
<p>See <a href="Kernel.html#method-i-exec"><code>Kernel#exec</code></a> for the standard shell.</p>
<div class="method-source-code" id="system-source">
<pre>static VALUE
rb_f_system(int argc, VALUE *argv, VALUE _)
{
/*
* n.b. using alloca for now to simplify future Thread::Light code
* when we need to use malloc for non-native Fiber
*/
struct waitpid_state *w = alloca(sizeof(struct waitpid_state));
rb_pid_t pid; /* may be different from waitpid_state.pid on exec failure */
VALUE execarg_obj;
struct rb_execarg *eargp;
int exec_errnum;
execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
eargp = rb_execarg_get(execarg_obj);
w->ec = GET_EC();
waitpid_state_init(w, 0, 0);
eargp->waitpid_state = w;
pid = rb_execarg_spawn(execarg_obj, 0, 0);
exec_errnum = pid < 0 ? errno : 0;
#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
if (w->pid > 0) {
/* `pid' (not w->pid) may be < 0 here if execve failed in child */
if (WAITPID_USE_SIGCHLD) {
rb_ensure(waitpid_sleep, (VALUE)w, waitpid_cleanup, (VALUE)w);
}
else {
waitpid_no_SIGCHLD(w);
}
rb_last_status_set(w->status, w->ret);
}
#endif
if (w->pid < 0 /* fork failure */ || pid < 0 /* exec failure */) {
if (eargp->exception) {
int err = exec_errnum ? exec_errnum : w->errnum;
VALUE command = eargp->invoke.sh.shell_script;
RB_GC_GUARD(execarg_obj);
rb_syserr_fail_str(err, command);
}
else {
return Qnil;
}
}
if (w->status == EXIT_SUCCESS) return Qtrue;
if (eargp->exception) {
VALUE command = eargp->invoke.sh.shell_script;
VALUE str = rb_str_new_cstr("Command failed with");
rb_str_cat_cstr(pst_message_status(str, w->status), ": ");
rb_str_append(str, command);
RB_GC_GUARD(execarg_obj);
rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, str));
}
else {
return Qfalse;
}
}</pre>
</div>
</div>
</div>
<div id="method-i-test" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
test(cmd, file1 [, file2] ) → obj
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Uses the character <code>cmd</code> to perform various tests on <code>file1</code> (first table below) or on <code>file1</code> and <code>file2</code> (second table).</p>
<p><a href="File.html"><code>File</code></a> tests on a single file:</p>
<pre>Cmd Returns Meaning
"A" | Time | Last access time for file1
"b" | boolean | True if file1 is a block device
"c" | boolean | True if file1 is a character device
"C" | Time | Last change time for file1
"d" | boolean | True if file1 exists and is a directory
"e" | boolean | True if file1 exists
"f" | boolean | True if file1 exists and is a regular file
"g" | boolean | True if file1 has the \CF{setgid} bit
| | set (false under NT)
"G" | boolean | True if file1 exists and has a group
| | ownership equal to the caller's group
"k" | boolean | True if file1 exists and has the sticky bit set
"l" | boolean | True if file1 exists and is a symbolic link
"M" | Time | Last modification time for file1
"o" | boolean | True if file1 exists and is owned by
| | the caller's effective uid
"O" | boolean | True if file1 exists and is owned by
| | the caller's real uid
"p" | boolean | True if file1 exists and is a fifo
"r" | boolean | True if file1 is readable by the effective
| | uid/gid of the caller
"R" | boolean | True if file is readable by the real
| | uid/gid of the caller
"s" | int/nil | If file1 has nonzero size, return the size,
| | otherwise return nil
"S" | boolean | True if file1 exists and is a socket
"u" | boolean | True if file1 has the setuid bit set
"w" | boolean | True if file1 exists and is writable by
| | the effective uid/gid
"W" | boolean | True if file1 exists and is writable by
| | the real uid/gid
"x" | boolean | True if file1 exists and is executable by
| | the effective uid/gid
"X" | boolean | True if file1 exists and is executable by
| | the real uid/gid
"z" | boolean | True if file1 exists and has a zero length</pre>
<p>Tests that take two files:</p>
<pre>"-" | boolean | True if file1 and file2 are identical
"=" | boolean | True if the modification times of file1
| | and file2 are equal
"<" | boolean | True if the modification time of file1
| | is prior to that of file2
">" | boolean | True if the modification time of file1
| | is after that of file2</pre>
<div class="method-source-code" id="test-source">
<pre>static VALUE
rb_f_test(int argc, VALUE *argv, VALUE _)
{
int cmd;
if (argc == 0) rb_check_arity(argc, 2, 3);
cmd = NUM2CHR(argv[0]);
if (cmd == 0) {
unknown:
/* unknown command */
if (ISPRINT(cmd)) {
rb_raise(rb_eArgError, "unknown command '%s%c'", cmd == '\'' || cmd == '\\' ? "\\" : "", cmd);
}
else {
rb_raise(rb_eArgError, "unknown command \"\\x%02X\"", cmd);
}
}
if (strchr("bcdefgGkloOprRsSuwWxXz", cmd)) {
CHECK(1);
switch (cmd) {
case 'b':
return rb_file_blockdev_p(0, argv[1]);
case 'c':
return rb_file_chardev_p(0, argv[1]);
case 'd':
return rb_file_directory_p(0, argv[1]);
case 'e':
return rb_file_exist_p(0, argv[1]);
case 'f':
return rb_file_file_p(0, argv[1]);
case 'g':
return rb_file_sgid_p(0, argv[1]);
case 'G':
return rb_file_grpowned_p(0, argv[1]);
case 'k':
return rb_file_sticky_p(0, argv[1]);
case 'l':
return rb_file_symlink_p(0, argv[1]);
case 'o':
return rb_file_owned_p(0, argv[1]);
case 'O':
return rb_file_rowned_p(0, argv[1]);
case 'p':
return rb_file_pipe_p(0, argv[1]);
case 'r':
return rb_file_readable_p(0, argv[1]);
case 'R':
return rb_file_readable_real_p(0, argv[1]);
case 's':
return rb_file_size_p(0, argv[1]);
case 'S':
return rb_file_socket_p(0, argv[1]);
case 'u':
return rb_file_suid_p(0, argv[1]);
case 'w':
return rb_file_writable_p(0, argv[1]);
case 'W':
return rb_file_writable_real_p(0, argv[1]);
case 'x':
return rb_file_executable_p(0, argv[1]);
case 'X':
return rb_file_executable_real_p(0, argv[1]);
case 'z':
return rb_file_zero_p(0, argv[1]);
}
}
if (strchr("MAC", cmd)) {
struct stat st;
VALUE fname = argv[1];
CHECK(1);
if (rb_stat(fname, &st) == -1) {
int e = errno;
FilePathValue(fname);
rb_syserr_fail_path(e, fname);
}
switch (cmd) {
case 'A':
return stat_atime(&st);
case 'M':
return stat_mtime(&st);
case 'C':
return stat_ctime(&st);
}
}
if (cmd == '-') {
CHECK(2);
return rb_file_identical_p(0, argv[1], argv[2]);
}
if (strchr("=<>", cmd)) {
struct stat st1, st2;
struct timespec t1, t2;
CHECK(2);
if (rb_stat(argv[1], &st1) < 0) return Qfalse;
if (rb_stat(argv[2], &st2) < 0) return Qfalse;
t1 = stat_mtimespec(&st1);
t2 = stat_mtimespec(&st2);
switch (cmd) {
case '=':
if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec) return Qtrue;
return Qfalse;
case '>':
if (t1.tv_sec > t2.tv_sec) return Qtrue;
if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec) return Qtrue;
return Qfalse;
case '<':
if (t1.tv_sec < t2.tv_sec) return Qtrue;
if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec) return Qtrue;
return Qfalse;
}
}
goto unknown;
}</pre>
</div>
</div>
</div>
<div id="method-i-throw" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
throw(tag [, obj])
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Transfers control to the end of the active <code>catch</code> block waiting for <em>tag</em>. Raises <code>UncaughtThrowError</code> if there is no <code>catch</code> block for the <em>tag</em>. The optional second parameter supplies a return value for the <code>catch</code> block, which otherwise defaults to <code>nil</code>. For examples, see Kernel::catch.</p>
<div class="method-source-code" id="throw-source">
<pre>static VALUE
rb_f_throw(int argc, VALUE *argv, VALUE _)
{
VALUE tag, value;
rb_scan_args(argc, argv, "11", &tag, &value);
rb_throw_obj(tag, value);
UNREACHABLE_RETURN(Qnil);
}</pre>
</div>
</div>
</div>
<div id="method-i-trace_var" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
trace_var(symbol, cmd ) → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
trace_var(symbol) {|val| block } → nil
</span>
</div>
<div class="method-description">
<p>Controls tracing of assignments to global variables. The parameter <code>symbol</code> identifies the variable (as either a string name or a symbol identifier). <em>cmd</em> (which may be a string or a <code>Proc</code> object) or block is executed whenever the variable is assigned. The block or <code>Proc</code> object receives the variable's new value as a parameter. Also see Kernel::untrace_var.</p>
<pre class="ruby"><span class="ruby-identifier">trace_var</span> <span class="ruby-value">:$_</span>, <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">puts</span> <span class="ruby-node">"$_ is now '#{v}'"</span> }
<span class="ruby-identifier">$_</span> = <span class="ruby-string">"hello"</span>
<span class="ruby-identifier">$_</span> = <span class="ruby-string">' there'</span>
</pre>
<p><em>produces:</em></p>
<pre>$_ is now 'hello'
$_ is now ' there'</pre>
<div class="method-source-code" id="trace_var-source">
<pre>static VALUE
f_trace_var(int c, const VALUE *a, VALUE _)
{
return rb_f_trace_var(c, a);
}</pre>
</div>
</div>
</div>
<div id="method-i-trap" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
trap( signal, command ) → obj
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
trap( signal ) {| | block } → obj
</span>
</div>
<div class="method-description">
<p>Specifies the handling of signals. The first parameter is a signal name (a string such as “SIGALRM'', “SIGUSR1'', and so on) or a signal number. The characters “SIG'' may be omitted from the signal name. The command or block specifies code to be run when the signal is raised. If the command is the string “IGNORE'' or “SIG_IGN'', the signal will be ignored. If the command is “DEFAULT'' or “SIG_DFL'', the Ruby's default handler will be invoked. If the command is “EXIT'', the script will be terminated by the signal. If the command is “SYSTEM_DEFAULT'', the operating system's default handler will be invoked. Otherwise, the given command or block will be run. The special signal name “EXIT'' or signal number zero will be invoked just prior to program termination. trap returns the previous handler for the given signal.</p>
<pre class="ruby"><span class="ruby-constant">Signal</span>.<span class="ruby-identifier">trap</span>(<span class="ruby-value">0</span>, <span class="ruby-identifier">proc</span> { <span class="ruby-identifier">puts</span> <span class="ruby-node">"Terminating: #{$$}"</span> })
<span class="ruby-constant">Signal</span>.<span class="ruby-identifier">trap</span>(<span class="ruby-string">"CLD"</span>) { <span class="ruby-identifier">puts</span> <span class="ruby-string">"Child died"</span> }
<span class="ruby-identifier">fork</span> <span class="ruby-operator">&&</span> <span class="ruby-constant">Process</span>.<span class="ruby-identifier">wait</span>
</pre>
<p>produces:</p>
<pre>Terminating: 27461
Child died
Terminating: 27460</pre>
<div class="method-source-code" id="trap-source">
<pre>static VALUE
sig_trap(int argc, VALUE *argv, VALUE _)
{
int sig;
sighandler_t func;
VALUE cmd;
rb_check_arity(argc, 1, 2);
sig = trap_signm(argv[0]);
if (reserved_signal_p(sig)) {
const char *name = signo2signm(sig);
if (name)
rb_raise(rb_eArgError, "can't trap reserved signal: SIG%s", name);
else
rb_raise(rb_eArgError, "can't trap reserved signal: %d", sig);
}
if (argc == 1) {
cmd = rb_block_proc();
func = sighandler;
}
else {
cmd = argv[1];
func = trap_handler(&cmd, sig);
}
return trap(sig, func, cmd);
}</pre>
</div>
</div>
</div>
<div id="method-i-untrace_var" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
untrace_var(symbol [, cmd] ) → array or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Removes tracing for the specified command on the given global variable and returns <code>nil</code>. If no command is specified, removes all tracing for that variable and returns an array containing the commands actually removed.</p>
<div class="method-source-code" id="untrace_var-source">
<pre>static VALUE
f_untrace_var(int c, const VALUE *a, VALUE _)
{
return rb_f_untrace_var(c, a);
}</pre>
</div>
</div>
</div>
<div id="method-i-warn" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
warn(*msgs, uplevel: nil) → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>If warnings have been disabled (for example with the <code>-W0</code> flag), does nothing. Otherwise, converts each of the messages to strings, appends a newline character to the string if the string does not end in a newline, and calls <a href="Warning.html#method-i-warn"><code>Warning.warn</code></a> with the string.</p>
<pre> warn("warning 1", "warning 2")
<em>produces:</em>
warning 1
warning 2</pre>
<p>If the <code>uplevel</code> keyword argument is given, the string will be prepended with information for the given caller frame in the same format used by the <code>rb_warn</code> C function.</p>
<pre> # In baz.rb
def foo
warn("invalid call to foo", uplevel: 1)
end
def bar
foo
end
bar
<em>produces:</em>
baz.rb:6: warning: invalid call to foo</pre>
<div class="method-source-code" id="warn-source">
<pre><span class="ruby-comment"># File warning.rb, line 42</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">warn</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">msgs</span>, <span class="ruby-value">uplevel:</span> <span class="ruby-keyword">nil</span>)
<span class="ruby-identifier">__builtin_rb_warn_m</span>(<span class="ruby-identifier">msgs</span>, <span class="ruby-identifier">uplevel</span>)
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
</section>
<section id="private-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Private Instance Methods</h3>
</header>
<div id="method-i-JSON" class="method-detail ">
<div class="method-heading">
<span class="method-name">JSON</span><span
class="method-args">(object, *args)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>If <em>object</em> is string-like, parse the string and return the parsed result as a Ruby data structure. Otherwise, generate a <a href="JSON.html"><code>JSON</code></a> text from the Ruby data structure object and return it.</p>
<p>The <em>opts</em> argument is passed through to generate/parse respectively. See generate and parse for their documentation.</p>
<div class="method-source-code" id="JSON-source">
<pre><span class="ruby-comment"># File ext/json/lib/json/common.rb, line 438</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">JSON</span>(<span class="ruby-identifier ruby-title">object</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
<span class="ruby-keyword">if</span> <span class="ruby-identifier">object</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_str</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">object</span>.<span class="ruby-identifier">to_str</span>, <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>)
<span class="ruby-keyword">else</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-URI" class="method-detail ">
<div class="method-heading">
<span class="method-name">URI</span><span
class="method-args">(uri)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>uri</code> converted to an <a href="URI.html"><code>URI</code></a> object.</p>
<div class="method-source-code" id="URI-source">
<pre><span class="ruby-comment"># File lib/uri/common.rb, line 733</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">URI</span>(<span class="ruby-identifier ruby-title">uri</span>)
<span class="ruby-keyword">if</span> <span class="ruby-identifier">uri</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">URI</span><span class="ruby-operator">::</span><span class="ruby-constant">Generic</span>)
<span class="ruby-identifier">uri</span>
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">uri</span> = <span class="ruby-constant">String</span>.<span class="ruby-identifier">try_convert</span>(<span class="ruby-identifier">uri</span>)
<span class="ruby-constant">URI</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">uri</span>)
<span class="ruby-keyword">else</span>
<span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>,
<span class="ruby-string">"bad argument (expected URI object or URI string)"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-gem" class="method-detail ">
<div class="method-heading">
<span class="method-name">gem</span><span
class="method-args">(gem_name, *requirements)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Use <a href="Kernel.html#method-i-gem"><code>Kernel#gem</code></a> to activate a specific version of <code>gem_name</code>.</p>
<p><code>requirements</code> is a list of version requirements that the specified gem must match, most commonly “= example.version.number”. See <a href="Gem/Requirement.html"><code>Gem::Requirement</code></a> for how to specify a version requirement.</p>
<p>If you will be activating the latest version of a gem, there is no need to call <a href="Kernel.html#method-i-gem"><code>Kernel#gem</code></a>, <a href="Kernel.html#method-i-require"><code>Kernel#require</code></a> will do the right thing for you.</p>
<p><a href="Kernel.html#method-i-gem"><code>Kernel#gem</code></a> 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.</p>
<p><a href="Kernel.html#method-i-gem"><code>Kernel#gem</code></a> should be called <strong>before</strong> any require statements (otherwise RubyGems may load a conflicting library version).</p>
<p><a href="Kernel.html#method-i-gem"><code>Kernel#gem</code></a> only loads prerelease versions when prerelease <code>requirements</code> are given:</p>
<pre class="ruby"><span class="ruby-identifier">gem</span> <span class="ruby-string">'rake'</span>, <span class="ruby-string">'>= 1.1.a'</span>, <span class="ruby-string">'< 2'</span>
</pre>
<p>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.</p>
<p>Example:</p>
<pre>GEM_SKIP=libA:libB ruby -I../libA -I../libB ./mycode.rb</pre>
<div class="method-source-code" id="gem-source">
<pre><span class="ruby-comment"># File lib/rubygems/core_ext/kernel_gem.rb, line 41</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">gem</span>(<span class="ruby-identifier">gem_name</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">requirements</span>) <span class="ruby-comment"># :doc:</span>
<span class="ruby-identifier">skip_list</span> = (<span class="ruby-constant">ENV</span>[<span class="ruby-string">'GEM_SKIP'</span>] <span class="ruby-operator">||</span> <span class="ruby-string">""</span>).<span class="ruby-identifier">split</span>(<span class="ruby-regexp">/:/</span>)
<span class="ruby-identifier">raise</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">LoadError</span>, <span class="ruby-node">"skipping #{gem_name}"</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">skip_list</span>.<span class="ruby-identifier">include?</span> <span class="ruby-identifier">gem_name</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">gem_name</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Dependency</span>
<span class="ruby-keyword">unless</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Deprecate</span>.<span class="ruby-identifier">skip</span>
<span class="ruby-identifier">warn</span> <span class="ruby-node">"#{Gem.location_of_caller.join ':'}:Warning: Kernel.gem no longer "</span>\
<span class="ruby-string">"accepts a Gem::Dependency object, please pass the name "</span>\
<span class="ruby-string">"and requirements directly"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">requirements</span> = <span class="ruby-identifier">gem_name</span>.<span class="ruby-identifier">requirement</span>
<span class="ruby-identifier">gem_name</span> = <span class="ruby-identifier">gem_name</span>.<span class="ruby-identifier">name</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">dep</span> = <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Dependency</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">gem_name</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">requirements</span>)
<span class="ruby-identifier">loaded</span> = <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">loaded_specs</span>[<span class="ruby-identifier">gem_name</span>]
<span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">loaded</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">dep</span>.<span class="ruby-identifier">matches_spec?</span>(<span class="ruby-identifier">loaded</span>)
<span class="ruby-identifier">spec</span> = <span class="ruby-identifier">dep</span>.<span class="ruby-identifier">to_spec</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">spec</span>
<span class="ruby-keyword">if</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">LOADED_SPECS_MUTEX</span>.<span class="ruby-identifier">owned?</span>
<span class="ruby-identifier">spec</span>.<span class="ruby-identifier">activate</span>
<span class="ruby-keyword">else</span>
<span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">LOADED_SPECS_MUTEX</span>.<span class="ruby-identifier">synchronize</span> { <span class="ruby-identifier">spec</span>.<span class="ruby-identifier">activate</span> }
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-j" class="method-detail ">
<div class="method-heading">
<span class="method-name">j</span><span
class="method-args">(*objs)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Outputs <em>objs</em> to STDOUT as <a href="JSON.html"><code>JSON</code></a> strings in the shortest form, that is in one line.</p>
<div class="method-source-code" id="j-source">
<pre><span class="ruby-comment"># File ext/json/lib/json/common.rb, line 416</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">j</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">objs</span>)
<span class="ruby-identifier">objs</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">obj</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">puts</span> <span class="ruby-constant">JSON</span><span class="ruby-operator">::</span><span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-value">:allow_nan</span> <span class="ruby-operator">=></span> <span class="ruby-keyword">true</span>, <span class="ruby-value">:max_nesting</span> <span class="ruby-operator">=></span> <span class="ruby-keyword">false</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">nil</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-jj" class="method-detail ">
<div class="method-heading">
<span class="method-name">jj</span><span
class="method-args">(*objs)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Outputs <em>objs</em> to STDOUT as <a href="JSON.html"><code>JSON</code></a> strings in a pretty format, with indentation and over many lines.</p>
<div class="method-source-code" id="jj-source">
<pre><span class="ruby-comment"># File ext/json/lib/json/common.rb, line 425</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">jj</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">objs</span>)
<span class="ruby-identifier">objs</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">obj</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">puts</span> <span class="ruby-constant">JSON</span><span class="ruby-operator">::</span><span class="ruby-identifier">pretty_generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-value">:allow_nan</span> <span class="ruby-operator">=></span> <span class="ruby-keyword">true</span>, <span class="ruby-value">:max_nesting</span> <span class="ruby-operator">=></span> <span class="ruby-keyword">false</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">nil</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-open" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
open(path [, mode [, perm]] [, opt]) → io or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
open(path [, mode [, perm]] [, opt]) {|io| block } → obj
</span>
</div>
<div class="method-description">
<p>Creates an <a href="IO.html"><code>IO</code></a> object connected to the given stream, file, or subprocess.</p>
<p>If <code>path</code> does not start with a pipe character (<code>|</code>), treat it as the name of a file to open using the specified mode (defaulting to “r”).</p>
<p>The <code>mode</code> is either a string or an integer. If it is an integer, it must be bitwise-or of open(2) flags, such as File::RDWR or File::EXCL. If it is a string, it is either “fmode”, “fmode:ext_enc”, or “fmode:ext_enc:int_enc”.</p>
<p>See the documentation of <a href="IO.html#method-c-new"><code>IO.new</code></a> for full documentation of the <code>mode</code> string directives.</p>
<p>If a file is being created, its initial permissions may be set using the <code>perm</code> parameter. See <a href="File.html#method-c-new"><code>File.new</code></a> and the open(2) and chmod(2) man pages for a description of permissions.</p>
<p>If a block is specified, it will be invoked with the <a href="IO.html"><code>IO</code></a> object as a parameter, and the <a href="IO.html"><code>IO</code></a> will be automatically closed when the block terminates. The call returns the value of the block.</p>
<p>If <code>path</code> starts with a pipe character (<code>"|"</code>), a subprocess is created, connected to the caller by a pair of pipes. The returned <a href="IO.html"><code>IO</code></a> object may be used to write to the standard input and read from the standard output of this subprocess.</p>
<p>If the command following the pipe is a single minus sign (<code>"|-"</code>), Ruby forks, and this subprocess is connected to the parent. If the command is not <code>"-"</code>, the subprocess runs the command.</p>
<p>When the subprocess is Ruby (opened via <code>"|-"</code>), the <code>open</code> call returns <code>nil</code>. If a block is associated with the open call, that block will run twice — once in the parent and once in the child.</p>
<p>The block parameter will be an <a href="IO.html"><code>IO</code></a> object in the parent and <code>nil</code> in the child. The parent's <code>IO</code> object will be connected to the child's $stdin and $stdout. The subprocess will be terminated at the end of the block.</p>
<h3 id="method-i-open-label-Examples">Examples<span><a href="#method-i-open-label-Examples">¶</a> <a href="#top">↑</a></span></h3>
<p>Reading from “testfile”:</p>
<pre class="ruby"><span class="ruby-identifier">open</span>(<span class="ruby-string">"testfile"</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">print</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">gets</span>
<span class="ruby-keyword">end</span>
</pre>
<p>Produces:</p>
<pre class="ruby"><span class="ruby-constant">This</span> <span class="ruby-identifier">is</span> <span class="ruby-identifier">line</span> <span class="ruby-identifier">one</span>
</pre>
<p>Open a subprocess and read its output:</p>
<pre class="ruby"><span class="ruby-identifier">cmd</span> = <span class="ruby-identifier">open</span>(<span class="ruby-string">"|date"</span>)
<span class="ruby-identifier">print</span> <span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">gets</span>
<span class="ruby-identifier">cmd</span>.<span class="ruby-identifier">close</span>
</pre>
<p>Produces:</p>
<pre>Wed Apr 9 08:56:31 CDT 2003</pre>
<p>Open a subprocess running the same Ruby program:</p>
<pre class="ruby"><span class="ruby-identifier">f</span> = <span class="ruby-identifier">open</span>(<span class="ruby-string">"|-"</span>, <span class="ruby-string">"w+"</span>)
<span class="ruby-keyword">if</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">nil?</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"in Child"</span>
<span class="ruby-identifier">exit</span>
<span class="ruby-keyword">else</span>
<span class="ruby-identifier">puts</span> <span class="ruby-node">"Got: #{f.gets}"</span>
<span class="ruby-keyword">end</span>
</pre>
<p>Produces:</p>
<pre>Got: in Child</pre>
<p>Open a subprocess using a block to receive the <a href="IO.html"><code>IO</code></a> object:</p>
<pre class="ruby"><span class="ruby-identifier">open</span> <span class="ruby-string">"|-"</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">f</span> <span class="ruby-keyword">then</span>
<span class="ruby-comment"># parent process</span>
<span class="ruby-identifier">puts</span> <span class="ruby-node">"Got: #{f.gets}"</span>
<span class="ruby-keyword">else</span>
<span class="ruby-comment"># child process</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"in Child"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>
<p>Produces:</p>
<pre>Got: in Child</pre>
<div class="method-source-code" id="open-source">
<pre>static VALUE
rb_f_open(int argc, VALUE *argv, VALUE _)
{
ID to_open = 0;
int redirect = FALSE;
if (argc >= 1) {
CONST_ID(to_open, "to_open");
if (rb_respond_to(argv[0], to_open)) {
redirect = TRUE;
}
else {
VALUE tmp = argv[0];
FilePathValue(tmp);
if (NIL_P(tmp)) {
redirect = TRUE;
}
else {
VALUE cmd = check_pipe_command(tmp);
if (!NIL_P(cmd)) {
argv[0] = cmd;
return rb_io_s_popen(argc, argv, rb_cIO);
}
}
}
}
if (redirect) {
VALUE io = rb_funcallv_kw(argv[0], to_open, argc-1, argv+1, RB_PASS_CALLED_KEYWORDS);
if (rb_block_given_p()) {
return rb_ensure(rb_yield, io, io_close, io);
}
return io;
}
return rb_io_s_open(argc, argv, rb_cFile);
}</pre>
</div>
</div>
<div class="aliases">
Also aliased as: <a href="Kernel.html#method-i-open_uri_original_open">open_uri_original_open</a>, <a href="Kernel.html#method-c-open_uri_original_open">open_uri_original_open</a>
</div>
</div>
<div id="method-i-open_uri_original_open" class="method-detail method-alias">
<div class="method-heading">
<span class="method-name">open_uri_original_open</span><span
class="method-args">(*args)</span>
</div>
<div class="method-description">
</div>
<div class="aliases">
Alias for: <a href="Kernel.html#method-i-open">open</a>
</div>
</div>
<div id="method-i-pp" class="method-detail ">
<div class="method-heading">
<span class="method-name">pp</span><span
class="method-args">(*objs)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>prints arguments in pretty form.</p>
<p>pp returns argument(s).</p>
<div class="method-source-code" id="pp-source">
<pre><span class="ruby-comment"># File lib/pp.rb, line 597</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pp</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">objs</span>)
<span class="ruby-identifier">objs</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">obj</span><span class="ruby-operator">|</span>
<span class="ruby-constant">PP</span>.<span class="ruby-identifier">pp</span>(<span class="ruby-identifier">obj</span>)
}
<span class="ruby-identifier">objs</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator"><=</span> <span class="ruby-value">1</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">objs</span>.<span class="ruby-identifier">first</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">objs</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
<div class="aliases">
Also aliased as: pp
</div>
</div>
<div id="method-i-require" class="method-detail ">
<div class="method-heading">
<span class="method-name">require</span><span
class="method-args">(path)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>When RubyGems is required, <a href="Kernel.html#method-i-require"><code>Kernel#require</code></a> is replaced with our own which is capable of loading gems on demand.</p>
<p>When you call <code>require 'x'</code>, this is what happens:</p>
<ul><li>
<p>If the file can be loaded from the existing Ruby loadpath, it is.</p>
</li><li>
<p>Otherwise, installed gems are searched for a file that matches. If it's found in gem 'y', that gem is activated (added to the loadpath).</p>
</li></ul>
<p>The normal <code>require</code> functionality of returning false if that file has already been loaded is preserved.</p>
<div class="method-source-code" id="require-source">
<pre><span class="ruby-comment"># File lib/rubygems/core_ext/kernel_require.rb, line 34</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">require</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-keyword">if</span> <span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:mon_owned?</span>)
<span class="ruby-identifier">monitor_owned</span> = <span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">mon_owned?</span>
<span class="ruby-keyword">end</span>
<span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">enter</span>
<span class="ruby-identifier">path</span> = <span class="ruby-identifier">path</span>.<span class="ruby-identifier">to_path</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_path</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">spec</span> = <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">find_unresolved_default_spec</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-comment"># Ensure -I beats a default gem</span>
<span class="ruby-identifier">resolved_path</span> = <span class="ruby-keyword">begin</span>
<span class="ruby-identifier">rp</span> = <span class="ruby-keyword">nil</span>
<span class="ruby-identifier">load_path_check_index</span> = <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">load_path_insert_index</span> <span class="ruby-operator">-</span> <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">activated_gem_paths</span>
<span class="ruby-constant">Gem</span>.<span class="ruby-identifier">suffixes</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">$LOAD_PATH</span>[<span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">load_path_check_index</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">lp</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">safe_lp</span> = <span class="ruby-identifier">lp</span>.<span class="ruby-identifier">dup</span>.<span class="ruby-identifier">tap</span>(<span class="ruby-operator">&</span><span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">UNTAINT</span>)
<span class="ruby-keyword">begin</span>
<span class="ruby-keyword">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">symlink?</span> <span class="ruby-identifier">safe_lp</span> <span class="ruby-comment"># for backward compatibility</span>
<span class="ruby-keyword">next</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">SecurityError</span>
<span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">exit</span>
<span class="ruby-identifier">raise</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">full_path</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">expand_path</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-identifier">safe_lp</span>, <span class="ruby-node">"#{path}#{s}"</span>))
<span class="ruby-keyword">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">file?</span>(<span class="ruby-identifier">full_path</span>)
<span class="ruby-identifier">rp</span> = <span class="ruby-identifier">full_path</span>
<span class="ruby-keyword">break</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">break</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">rp</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">rp</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">begin</span>
<span class="ruby-constant">Kernel</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value">:gem</span>, <span class="ruby-identifier">spec</span>.<span class="ruby-identifier">name</span>, <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Requirement</span>.<span class="ruby-identifier">default_prerelease</span>)
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">Exception</span>
<span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">exit</span>
<span class="ruby-identifier">raise</span>
<span class="ruby-keyword">end</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">resolved_path</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># If there are no unresolved deps, then we can use just try</span>
<span class="ruby-comment"># normal require handle loading a gem from the rescue below.</span>
<span class="ruby-keyword">if</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Specification</span>.<span class="ruby-identifier">unresolved_deps</span>.<span class="ruby-identifier">empty?</span>
<span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">exit</span>
<span class="ruby-keyword">return</span> <span class="ruby-identifier">gem_original_require</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># If +path+ is for a gem that has already been loaded, don't</span>
<span class="ruby-comment"># bother trying to find it in an unresolved gem, just go straight</span>
<span class="ruby-comment"># to normal require.</span>
<span class="ruby-comment">#--</span>
<span class="ruby-comment"># TODO request access to the C implementation of this to speed up RubyGems</span>
<span class="ruby-keyword">if</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Specification</span>.<span class="ruby-identifier">find_active_stub_by_path</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">exit</span>
<span class="ruby-keyword">return</span> <span class="ruby-identifier">gem_original_require</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># Attempt to find +path+ in any unresolved gems...</span>
<span class="ruby-identifier">found_specs</span> = <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Specification</span>.<span class="ruby-identifier">find_in_unresolved</span> <span class="ruby-identifier">path</span>
<span class="ruby-comment"># If there are no directly unresolved gems, then try and find +path+</span>
<span class="ruby-comment"># in any gems that are available via the currently unresolved gems.</span>
<span class="ruby-comment"># For example, given:</span>
<span class="ruby-comment">#</span>
<span class="ruby-comment"># a => b => c => d</span>
<span class="ruby-comment">#</span>
<span class="ruby-comment"># If a and b are currently active with c being unresolved and d.rb is</span>
<span class="ruby-comment"># requested, then find_in_unresolved_tree will find d.rb in d because</span>
<span class="ruby-comment"># it's a dependency of c.</span>
<span class="ruby-comment">#</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">found_specs</span>.<span class="ruby-identifier">empty?</span>
<span class="ruby-identifier">found_specs</span> = <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Specification</span>.<span class="ruby-identifier">find_in_unresolved_tree</span> <span class="ruby-identifier">path</span>
<span class="ruby-identifier">found_specs</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">found_spec</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">found_spec</span>.<span class="ruby-identifier">activate</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># We found +path+ directly in an unresolved gem. Now we figure out, of</span>
<span class="ruby-comment"># the possible found specs, which one we should activate.</span>
<span class="ruby-keyword">else</span>
<span class="ruby-comment"># Check that all the found specs are just different</span>
<span class="ruby-comment"># versions of the same gem</span>
<span class="ruby-identifier">names</span> = <span class="ruby-identifier">found_specs</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&</span><span class="ruby-value">:name</span>).<span class="ruby-identifier">uniq</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">names</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">></span> <span class="ruby-value">1</span>
<span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">exit</span>
<span class="ruby-identifier">raise</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">LoadError</span>, <span class="ruby-node">"#{path} found in multiple gems: #{names.join ', '}"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># Ok, now find a gem that has no conflicts, starting</span>
<span class="ruby-comment"># at the highest version.</span>
<span class="ruby-identifier">valid</span> = <span class="ruby-identifier">found_specs</span>.<span class="ruby-identifier">find</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span> <span class="ruby-operator">!</span><span class="ruby-identifier">s</span>.<span class="ruby-identifier">has_conflicts?</span> }
<span class="ruby-keyword">unless</span> <span class="ruby-identifier">valid</span>
<span class="ruby-identifier">le</span> = <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">LoadError</span>.<span class="ruby-identifier">new</span> <span class="ruby-node">"unable to find a version of '#{names.first}' to activate"</span>
<span class="ruby-identifier">le</span>.<span class="ruby-identifier">name</span> = <span class="ruby-identifier">names</span>.<span class="ruby-identifier">first</span>
<span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">exit</span>
<span class="ruby-identifier">raise</span> <span class="ruby-identifier">le</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">valid</span>.<span class="ruby-identifier">activate</span>
<span class="ruby-keyword">end</span>
<span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">exit</span>
<span class="ruby-keyword">return</span> <span class="ruby-identifier">gem_original_require</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">LoadError</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">load_error</span>
<span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">enter</span>
<span class="ruby-keyword">begin</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">load_error</span>.<span class="ruby-identifier">message</span>.<span class="ruby-identifier">end_with?</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">and</span> <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">try_activate</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-identifier">require_again</span> = <span class="ruby-keyword">true</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">ensure</span>
<span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">exit</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">return</span> <span class="ruby-identifier">gem_original_require</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">require_again</span>
<span class="ruby-identifier">raise</span> <span class="ruby-identifier">load_error</span>
<span class="ruby-keyword">ensure</span>
<span class="ruby-keyword">if</span> <span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:mon_owned?</span>)
<span class="ruby-keyword">if</span> <span class="ruby-identifier">monitor_owned</span> <span class="ruby-operator">!=</span> (<span class="ruby-identifier">ow</span> = <span class="ruby-constant">RUBYGEMS_ACTIVATION_MONITOR</span>.<span class="ruby-identifier">mon_owned?</span>)
<span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">puts</span> [<span class="ruby-identifier">$$</span>, <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>, <span class="ruby-identifier">$!</span>, <span class="ruby-identifier">$!</span>.<span class="ruby-identifier">backtrace</span>].<span class="ruby-identifier">inspect</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">$!</span>
<span class="ruby-identifier">raise</span> <span class="ruby-node">"CRITICAL: RUBYGEMS_ACTIVATION_MONITOR.owned?: before #{monitor_owned} -> after #{ow}"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
<div class="aliases">
Also aliased as: <a href="Kernel.html#method-i-gem_original_require">gem_original_require</a>
</div>
</div>
<div id="method-i-y" class="method-detail ">
<div class="method-heading">
<span class="method-name">y</span><span
class="method-args">(*objects)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>An alias for <a href="Psych.html#method-c-dump_stream"><code>Psych.dump_stream</code></a> meant to be used with IRB.</p>
<div class="method-source-code" id="y-source">
<pre><span class="ruby-comment"># File ext/psych/lib/psych/y.rb, line 5</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">y</span> <span class="ruby-operator">*</span><span class="ruby-identifier">objects</span>
<span class="ruby-identifier">puts</span> <span class="ruby-constant">Psych</span>.<span class="ruby-identifier">dump_stream</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">objects</span>)
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>