File: C:/Ruby27-x64/share/doc/ruby/html/Class.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class Class - 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="class">
<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">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="Module.html">Module</a>
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-new">::new</a>
<li ><a href="#method-i-allocate">#allocate</a>
<li ><a href="#method-i-inherited">#inherited</a>
<li ><a href="#method-i-json_creatable-3F">#json_creatable?</a>
<li ><a href="#method-i-new">#new</a>
<li ><a href="#method-i-superclass">#superclass</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-Class">
<h1 id="class-Class" class="class">
class Class
</h1>
<section class="description">
<p>Extends any <a href="Class.html"><code>Class</code></a> to include <em>json_creatable?</em> method.</p>
<p>Classes in Ruby are first-class objects—each is an instance of class <a href="Class.html"><code>Class</code></a>.</p>
<p>Typically, you create a new class by using:</p>
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Name</span>
<span class="ruby-comment"># some code describing the class behavior</span>
<span class="ruby-keyword">end</span>
</pre>
<p>When a new class is created, an object of type <a href="Class.html"><code>Class</code></a> is initialized and assigned to a global constant (Name in this case).</p>
<p>When <code>Name.new</code> is called to create a new object, the <a href="Class.html#method-i-new"><code>new</code></a> method in <a href="Class.html"><code>Class</code></a> is run by default. This can be demonstrated by overriding <a href="Class.html#method-i-new"><code>new</code></a> in Class:</p>
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Class</span>
<span class="ruby-keyword">alias</span> <span class="ruby-identifier">old_new</span> <span class="ruby-identifier">new</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">new</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
<span class="ruby-identifier">print</span> <span class="ruby-string">"Creating a new "</span>, <span class="ruby-keyword">self</span>.<span class="ruby-identifier">name</span>, <span class="ruby-string">"\n"</span>
<span class="ruby-identifier">old_new</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Name</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">n</span> = <span class="ruby-constant">Name</span>.<span class="ruby-identifier">new</span>
</pre>
<p><em>produces:</em></p>
<pre class="ruby"><span class="ruby-constant">Creating</span> <span class="ruby-identifier">a</span> <span class="ruby-identifier">new</span> <span class="ruby-constant">Name</span>
</pre>
<p>Classes, modules, and objects are interrelated. In the diagram that follows, the vertical arrows represent inheritance, and the parentheses metaclasses. All metaclasses are instances of the class `Class'.</p>
<pre> +---------+ +-...
| | |
BasicObject-----|-->(BasicObject)-------|-...
^ | ^ |
| | | |
Object---------|----->(Object)---------|-...
^ | ^ |
| | | |
+-------+ | +--------+ |
| | | | | |
| Module-|---------|--->(Module)-|-...
| ^ | | ^ |
| | | | | |
| Class-|---------|---->(Class)-|-...
| ^ | | ^ |
| +---+ | +----+
| |
obj--->OtherClass---------->(OtherClass)-----------...</pre>
</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-new" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
new(super_class=Object) → a_class
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
new(super_class=Object) { |mod| ... } → a_class
</span>
</div>
<div class="method-description">
<p>Creates a new anonymous (unnamed) class with the given superclass (or <a href="Object.html"><code>Object</code></a> if no parameter is given). You can give a class a name by assigning the class object to a constant.</p>
<p>If a block is given, it is passed the class object, and the block is evaluated in the context of this class like <a href="Module.html#method-i-class_eval"><code>class_eval</code></a>.</p>
<pre class="ruby"><span class="ruby-identifier">fred</span> = <span class="ruby-constant">Class</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">meth1</span>
<span class="ruby-string">"hello"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">meth2</span>
<span class="ruby-string">"bye"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">a</span> = <span class="ruby-identifier">fred</span>.<span class="ruby-identifier">new</span> <span class="ruby-comment">#=> #<#<Class:0x100381890>:0x100376b98></span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">meth1</span> <span class="ruby-comment">#=> "hello"</span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">meth2</span> <span class="ruby-comment">#=> "bye"</span>
</pre>
<p>Assign the class to a constant (name starting uppercase) if you want to treat it like a regular class.</p>
<div class="method-source-code" id="new-source">
<pre>static VALUE
rb_class_initialize(int argc, VALUE *argv, VALUE klass)
{
VALUE super;
if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
rb_raise(rb_eTypeError, "already initialized class");
}
if (rb_check_arity(argc, 0, 1) == 0) {
super = rb_cObject;
}
else {
super = argv[0];
rb_check_inheritable(super);
if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
rb_raise(rb_eTypeError, "can't inherit uninitialized class");
}
}
RCLASS_SET_SUPER(klass, super);
rb_make_metaclass(klass, RBASIC(super)->klass);
rb_class_inherited(super, klass);
rb_mod_initialize(klass);
return klass;
}</pre>
</div>
</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-allocate" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
allocate() → obj
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Allocates space for a new object of <em>class</em>'s class and does not call initialize on the new instance. The returned object must be an instance of <em>class</em>.</p>
<pre class="ruby"><span class="ruby-identifier">klass</span> = <span class="ruby-constant">Class</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
<span class="ruby-ivar">@initialized</span> = <span class="ruby-keyword">true</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialized?</span>
<span class="ruby-ivar">@initialized</span> <span class="ruby-operator">||</span> <span class="ruby-keyword">false</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">klass</span>.<span class="ruby-identifier">allocate</span>.<span class="ruby-identifier">initialized?</span> <span class="ruby-comment">#=> false</span>
</pre>
<div class="method-source-code" id="allocate-source">
<pre>static VALUE
rb_class_alloc_m(VALUE klass)
{
rb_alloc_func_t allocator = class_get_alloc_func(klass);
if (!rb_obj_respond_to(klass, rb_intern("allocate"), 1)) {
rb_raise(rb_eTypeError, "calling %"PRIsVALUE".allocate is prohibited",
klass);
}
return class_call_alloc_func(allocator, klass);
}</pre>
</div>
</div>
</div>
<div id="method-i-json_creatable-3F" class="method-detail ">
<div class="method-heading">
<span class="method-name">json_creatable?</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns true if this class can be used to create an instance from a serialised <a href="JSON.html"><code>JSON</code></a> string. The class has to implement a class method <em>json_create</em> that expects a hash as first parameter. The hash should include the required data.</p>
<div class="method-source-code" id="json_creatable-3F-source">
<pre><span class="ruby-comment"># File ext/json/lib/json/common.rb, line 453</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">json_creatable?</span>
<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:json_create</span>)
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-new" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
new(args, ...) → obj
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Calls <a href="Class.html#method-i-allocate"><code>allocate</code></a> to create a new object of <em>class</em>'s class, then invokes that object's initialize method, passing it <em>args</em>. This is the method that ends up getting called whenever an object is constructed using <code>.new</code>.</p>
<div class="method-source-code" id="new-source">
<pre>static VALUE
rb_class_s_new(int argc, const VALUE *argv, VALUE klass)
{
VALUE obj;
obj = rb_class_alloc(klass);
rb_obj_call_init_kw(obj, argc, argv, RB_PASS_CALLED_KEYWORDS);
return obj;
}</pre>
</div>
</div>
</div>
<div id="method-i-superclass" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
superclass → a_super_class or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the superclass of <em>class</em>, or <code>nil</code>.</p>
<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">superclass</span> <span class="ruby-comment">#=> IO</span>
<span class="ruby-constant">IO</span>.<span class="ruby-identifier">superclass</span> <span class="ruby-comment">#=> Object</span>
<span class="ruby-constant">Object</span>.<span class="ruby-identifier">superclass</span> <span class="ruby-comment">#=> BasicObject</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Foo</span>; <span class="ruby-keyword">end</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Bar</span> <span class="ruby-operator"><</span> <span class="ruby-constant">Foo</span>; <span class="ruby-keyword">end</span>
<span class="ruby-constant">Bar</span>.<span class="ruby-identifier">superclass</span> <span class="ruby-comment">#=> Foo</span>
</pre>
<p>Returns nil when the given class does not have a parent class:</p>
<pre class="ruby"><span class="ruby-constant">BasicObject</span>.<span class="ruby-identifier">superclass</span> <span class="ruby-comment">#=> nil</span>
</pre>
<div class="method-source-code" id="superclass-source">
<pre>VALUE
rb_class_superclass(VALUE klass)
{
VALUE super = RCLASS_SUPER(klass);
if (!super) {
if (klass == rb_cBasicObject) return Qnil;
rb_raise(rb_eTypeError, "uninitialized class");
}
while (RB_TYPE_P(super, T_ICLASS)) {
super = RCLASS_SUPER(super);
}
if (!super) {
return Qnil;
}
return super;
}</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-inherited" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
inherited(subclass)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Callback invoked whenever a subclass of the current class is created.</p>
<p>Example:</p>
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Foo</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">inherited</span>(<span class="ruby-identifier">subclass</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-node">"New subclass: #{subclass}"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Bar</span> <span class="ruby-operator"><</span> <span class="ruby-constant">Foo</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Baz</span> <span class="ruby-operator"><</span> <span class="ruby-constant">Bar</span>
<span class="ruby-keyword">end</span>
</pre>
<p><em>produces:</em></p>
<pre class="ruby"><span class="ruby-constant">New</span> <span class="ruby-value">subclass:</span> <span class="ruby-constant">Bar</span>
<span class="ruby-constant">New</span> <span class="ruby-value">subclass:</span> <span class="ruby-constant">Baz</span>
</pre>
<div class="method-source-code" id="inherited-source">
<pre>static VALUE
rb_obj_dummy1(VALUE _x, VALUE _y)
{
return rb_obj_dummy();
}</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>