This is first time I've experiencing Object-orient scripting language. Enjoying inheritance parent-child relationships in the programming.
It can be defined as sharing methods between classes. it avoids copynjing and pasting of same kind of methods and variables.
Inheritance has two concepts which are Super class and subclass.
Let me share my Example:
class Vechile
def accelrate
puts "floor it"
end
def horn
puts "blow blow"
end
end
class Car < Vechile
end
vechile=Car.new
vechile.accelrate
vechile.horn
I've issue with my Linux box so I've installed on Windows.
C:\Users\JITTA>ruby -version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-mingw32]
-e:1:in `': undefined local variable or method `rsion' for main:Object (NameError)
Let's start irb interactive mode:
C:\Users\JITTA>irb -I
irb(main):001:0> class Vechile
irb(main):002:1> def accelrate
irb(main):003:2> puts "floor it"
irb(main):004:2> end
irb(main):005:1>
irb(main):006:1* def horn
irb(main):007:2> puts "blow blow"
irb(main):008:2> end
irb(main):009:1> end
=> :horn
irb(main):010:0>
irb(main):011:0* class Car < Vechile
irb(main):012:1> end
=> nil
irb(main):013:0>
irb(main):014:0* vechile=Car.new
=> #
irb(main):015:0> vechile.accelrate
floor it
=> nil
irb(main):016:0> vechile.horn
blow blow
=> nil
irb(main):017:0>
The "super" keyword is used to get the parent attributes into derived class.
irb(main):001:0> class Person
irb(main):002:1> def greeting
irb(main):003:2> puts "hello"
irb(main):004:2> end
irb(main):005:1> end
=> :greeting
irb(main):006:0> class Friend < Person
irb(main):007:1> def greeting
irb(main):008:2> super
irb(main):009:2> puts "glad to see you!"
irb(main):010:2> end
irb(main):011:1> end
=> :greeting
irb(main):012:0> Friend.new.greeting
hello
glad to see you!
Without Super keyword
irb(main):013:0> class Person
irb(main):014:1> def greeting
irb(main):015:2> puts "hello"
irb(main):016:2> end
irb(main):017:1> end
=> :greeting
irb(main):018:0> class Friend < Person
irb(main):019:1> def greeting
irb(main):020:2> puts "glad to see you"
irb(main):021:2> end
irb(main):022:1> end
=> :greeting
irb(main):023:0> Friend.new.greeting
glad to see you
=> nil
irb(main):024:0>
The Object class
The uncreated super class of a class untill it is created as subclass is said to be Object class
Example:
class Unix /*Object class*/
def kernal
puts "The unix kernal is different to linux"
end
def vendor
puts "Unix has diffrent vendors like ibm,hp and oracle"
end
end
class Solaris < Unix /*Super class*/
end
os=Solaris.new
os.kernal
os.vendor
One more experiment with the inheritance
irb(main):001:0>
irb(main):002:0* class Unix
irb(main):003:1> def kernal
irb(main):004:2> puts "The unix kernal is different to linux"
irb(main):005:2> end
irb(main):006:1> def vendor
irb(main):007:2>
irb(main):008:2* puts "Unix has diffrent vendors like ibm,hp and oracle"
irb(main):009:2> end
irb(main):010:1> end
=> :vendor
irb(main):011:0> class Solaris < Unix
irb(main):012:1> end
=> nil
irb(main):013:0> os=Solaris.new
=> #
irb(main):014:0> os.kernal
The unix kernal is different to linux
=> nil
irb(main):015:0> os.vendor
Unix has diffrent vendors like ibm,hp and oracle
=> nil
In Ruby script why everything inherit from the Object class?
Because the Object class provides useful methods which needed by every Ruby object like
1 to_s /*converts an object to a string for printing /
2 inspect
3 class
4 methods
5 instance_variables