From PHP to Ruby on Rails by Bernard Pineda
Author:Bernard Pineda
Language: eng
Format: epub
Publisher: Packt Publishing Pvt Ltd
Published: 2023-11-16T00:00:00+00:00
Inheritance in Ruby
So far, weâve looked at a few features that come with Rubyâs implementation of the OOP paradigm, but we have neglected to look at one of the core features that help us recycle code. Inheritance can be simplified as the practice of passing the features of a class to create a brand-new child class. With this new class, we can use any of the features from the parent class, create new features, or customize the features that come from the parent class. The syntax for inheritance can be quite different than in PHP, but the behavior is quite similar. With that in mind, letâs take a look at a few use cases and see it in action.
Letâs say we wanted a class that would let us connect to a database. Instead of having to write all the functionality to connect to a database, we could get an already created database class, create a new one that inherited all the database functionality, and then focus on creating just the features that we need. This is one way to reuse code with inheritance, but letâs use a simpler example so that we can see inheritance in practice. Letâs say we wanted to make an abstraction of a user. The user must have first name, last name, age, and email details. We can take the Person class, defined in the previous section, inherit the features in our new User class, and just focus on the missing pieces.
So, letâs take our Person class and create a file called inheritance_example.rb with the following content:
# inheritance_example.rb class Person attr_accessor :first_name, :last_name def initialize( first_name, last_name ) @first_name = first_name @last_name = last_name end def full_name puts "#{first_name} #{last_name}" end end
Now, letâs create a new class below our original class called User and inherit from the Person class. Weâll do this with the < operator. Letâs add this to the end of our Person class:
# inheritance_example.rb ⦠class User < Person end
With just two lines of code, weâve made a brand-new class that behaves (for now) in the exact same way as the Person class. Letâs confirm that by creating a new User object. Letâs now add the following to the end of our file:
# inheritance_example.rb ⦠user = User.new
Now, letâs try to run this script from our shell:
ruby inheritance_example.rb
This should output the following:
inheritance_example.rb:4:in `initialize': wrong number of arguments (given 0, expected 2) (ArgumentError) from inheritance_example.rb:18:in `new' from inheritance_example.rb:18:in `<main>'
Our script failed, but why? As we read through the message, it states that the constructor expected two parameters, but none were given. From our previous execution example, we can infer that we have to give our constructor the parameters for the first name and last name. So, letâs add those parameters, and letâs also call the full_name() method. Our code will now look like this:
class Person attr_accessor :first_name, :last_name def initialize( first_name, last_name ) @first_name = first_name @last_name = last_name end def full_name puts "#{first_name} #{last_name}" end end class User < Person end user = User.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9865)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9755)
The Mikado Method by Ola Ellnestam Daniel Brolund(9745)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8256)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7743)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7739)
Grails in Action by Glen Smith Peter Ledbrook(7665)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7514)
Windows APT Warfare by Sheng-Hao Ma(6497)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6376)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6243)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6112)
Kotlin in Action by Dmitry Jemerov(5017)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4295)
Functional Programming in JavaScript by Mantyla Dan(4016)
Solidity Programming Essentials by Ritesh Modi(3836)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3611)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3562)
The Ultimate iOS Interview Playbook by Avi Tsadok(3529)
