Ruby On Rails Classroom image

Pooja  Negi / Student / Web Technology

To post your Question Join Classroom
Lesson Topics's No:-  ||
Lessons:-Class Inheritance

Class Inheritance

When learning about classes, it’s useful to find out the class hierarchy using the superclass method:



A diagram of this inheritance hierarchy appears in Figure 4.1. We see here that the superclass of String is Object and the superclass of Object is BasicObject, but BasicObject has no superclass. This pattern is true of every Ruby object: Trace back the class hierarchy far enough and every class in Ruby ultimately inherits from BasicObject, which has no superclass itself. This is the technical meaning of ‘‘everything in Ruby is an object.’’

          To understand classes a little more deeply, there’s no substitute for making one of our own. Let’s make a Word class with a palindrome? method that returns true if the word is the same spelled forward and backward:


>>   class   Word
>>  def   palindrome?  (string)
>>    string == string. reverse
>>  end
>>  end
=>  nil


We can use it as follows:

>>  w =  Word. new                          # Make a new Word object.
=>  #<Word:0x22d0b20>
>>  w.palindrome?  ("foobar")
=>  false
>>  w.palindrome?  ("level")
=>  true 


If this example strikes you as a bit contrived, good; this is by design. It’s odd to create a new class just to create a method that takes a string as an argument. Since a word is a string, it’s more natural to have our Word class inherit from String, as seen in Listing 4.8. (You should exit the console and re-enter it to clear out the old definition of Word.)  


Listing 4.8 Defining a Word class in the console



Here Word < String is the Ruby syntax for inheritance (discussed briefly in Section 3.1.2), which ensures that, in addition to the new palindrome? method, words also have all the same methods as strings:

>>  s = Word .new  ("level")                   # Make a new Word, initialized with "level".
=>  "level"
>> s.palindrome?                              # Words have the palindrome? method.
=>  true
>>  s.length                                     # Words also inherit all the normal string methods.
=>  5



Since the Word class inherits from String, we can use the console to see the class hierarchy explicitly:  

>>  s.class
=>  Word
>>  s.class.superclass
=>  String
>>  s.class.superclass.superclass
=>  Object


This hierarchy is illustrated in Figure 4.2.
                 In Listing 4.8, note that checking that the word is its own reverse involves accessing the word inside the Word class. Ruby allows us to do this using the self keyword: Inside the Word class, self is the object itself, which means we can use

self  ==  self .reverse

     to check if the word is a palindrome.11 

Pooja   Negi

Skills    Ruby On Rails

Qualifications :- High School - SSN high school, College/University - HNBGU, College/University - SRHU,
Location :-Ranipokhari,Rishikesh,Uttarakhand,India
Description:- Student

  Students (0)