# A inherent class, how to write?

**URL:** <https://www.ruby-forum.com/t/a-inherent-class-how-to-write/171846>\
**Category:** Ruby\
**Created:** [July 10, 2009, 4:01am UTC](https://www.ruby-forum.com/t/a-inherent-class-how-to-write/171846 "2009-07-10T04:01:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [July 10, 2009, 4:01am UTC](https://www.ruby-forum.com/t/a-inherent-class-how-to-write/171846/1 "2009-07-10T04:01:33Z")

</div>

I have a file like this;

class File  
class \<\< self  
def diff  
#some stuff--------------  
end  
end  
end

def method\_1  
#-----------  
end

def method\_2  
#------------  
end

## method\_1 method\_2

now ,I need to rewrite this file.

but I don’t how to deal with the class File…

one.  
+++++++++++++++++++  
class File  
class \<\< self  
def diff  
#some stuff--------------  
end  
end  
end

class Myclass  
def method\_1  
end

def method\_2  
end  
end

++++++++++++++++++++

two  
++++++++++++++++++++  
class Myclass  
class File  
class \<\< self  
def diff  
#some stuff--------------  
end  
end  
end

def method\_1  
end

def method\_2  
end  
end  
+++++++++++++++++++++

so, which way should I use?  
any thought will be appreciated.

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [July 10, 2009, 7:40am UTC](https://www.ruby-forum.com/t/a-inherent-class-how-to-write/171846/2 "2009-07-10T07:40:51Z")

</div>

On 10.07.2009 04:01, Zhenning G. wrote:

> ## def method\_1
> 
> ```
> end
> 
> ```
> 
> end  
> end
> 
> def method\_1  
> end
> 
> def method\_2  
> end  
> end  
> +++++++++++++++++++++

That’s difficult to answer since we cannot see how your method\_1 and  
method\_2 use class File.

In the general case inheritance is done like this:

class A  
end

class B \< A  
end

B is now subclass of A.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [July 10, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/a-inherent-class-how-to-write/171846/3 "2009-07-10T13:41:21Z")

</div>

At 2009-07-09 10:01PM, “Zhenning G.” wrote:

> I have a file like this;
> 
> class File  
> class \<\< self  
> def diff  
> #some stuff--------------  
> end  
> end  
> end

How is that different from  
class File  
def diff  
#some stuff--------------  
end  
end  
?

Still learning…

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [July 10, 2009, 2:25pm UTC](https://www.ruby-forum.com/t/a-inherent-class-how-to-write/171846/4 "2009-07-10T14:25:47Z")

</div>

Hi –

On Fri, 10 Jul 2009, Glenn J. wrote:

> How is that different from  
> class File  
> def diff  
> #some stuff--------------  
> end  
> end  
> ?
> 
> Still learning…

The construct:

class \<\< object  
# …  
end

puts you into a class definition block for the singleton class of  
object. The singleton class is where the methods are defined that are  
specific to that exact object (as opposed to those that the object  
gets from its class).

In this case:

class File  
class \<\< self

“self” is the class object File (because when you’re in a class  
definition block, “self” is set to the class object). The methods  
defined in the inner definition block will be callable only directly  
on File itself (and its subclasses; classes get to share their  
singleton methods with their subclasses)[1]. So you’ll be able to do:

File.diff

but not

f = File.new(…)  
f.diff # wrong; it’s not an instance method

If you do

class File  
def diff

then you’re defining an instance method.

For more info, see [http://www.wobblini.net/singletons.html](http://www.wobblini.net/singletons.html). (And for a  
lot more info, see [http://www.manning.com/black2](http://www.manning.com/black2) 🙂

David

[1] And singleton methods of class objects are more commonly referred  
to as “class methods”.
