title: ruby object model and class declaration
body: <p>i was trying some code about how classes are declared internally and what happens when a class is declared. I got that when a class is declared, an object of 'Class' is created and a constant with the name of the declared class is created. eg</p>
  
  <pre><code>MyClass = Class.new
  MyClass.class =&gt; Class
  MyClass.superclass =&gt; Object
  Class.constants.include? :MyClass =&gt; true
  </code></pre>
  
  <p>i also tried</p>
  
  <pre><code>x = Class.new =&gt; #&lt;Class:0xd886938&gt; 
  x.class =&gt; Class 
  x.superclass =&gt; Object 
  Class.constants.include? :x =&gt; false #why so???
  </code></pre>
  
  <p>it means that 'x' is a class. i m confused cause</p>
  
  <pre><code>class x
    def say_hi 
      'hi'
    end
  end
  </code></pre>
  
  <p>gives </p>
  
  <pre><code>SyntaxError: (irb):121: class/module name must be CONSTANT
  </code></pre>
  
  <p>why is this happening?</p>
  
  <p>---UPDATE---</p>
  
  <p>i tried</p>
  
  <pre><code>def x.x_method
    'x class method'
  end
  </code></pre>
  
  <p>and did</p>
  
  <pre><code>x.methods.include? :x_method =&gt; true
  </code></pre>
  
  <p>similarly</p>
  
  <pre><code>x.singleton_methods.include? :x_method =&gt; true
  </code></pre>
  
  <p>and mainly</p>
  
  <pre><code>x.methods - MyClass.methods =&gt; [:x_method]
  </code></pre>
  
  <p>The above line points that <code>x</code> is a class since it's an object of class <code>Class</code>. Since all user defined classes are objects of class <code>Class</code>, then their initial methods should also be the same unless some singleton method is declared for a specific class(<code>x</code> in the above case has a singleton method)</p>
  
  <pre><code>x =&gt; #&lt;Class:0xd886938&gt;
  obj_x = x.new  =&gt; #&lt;#&lt;Class:0xd886938&gt;:0xd30caac&gt; 
  obj_x.class =&gt; #&lt;Class:0xd886938&gt; 
  </code></pre>
  
  <p>The above code further strengthens the assumption that <code>x</code> is a class since it's allowing me to create new objects. if <code>x</code> would have been an instance then <code>new</code> would have failed with an error. </p>
  
  <p>My question is that, why is ruby allowing me to declare a class in a particular fashion? and if it isn't a class then why is it behaving like one?</p>