Question
Ruby class names begin with an an uppercase letter, the same as Constants. We create a class using the class keyword and create a new
Ruby class names begin with an an uppercase letter, the same as Constants. We create a class using the class keyword and create a new instance of the class with the new method. Although our Pet class doesnt have any methods of its own, it will inherit all of the methods of the parent Objectclass.
class Pet end buster = Pet.new puts buster.class puts buster.class.superclass print(buster.methods, ' ')
Object Pet Object [:nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
We can can add methods to the Pet class and the objects will inherit them. And, we can reopen any existing class in the midst of our program and add more methods or alter existing methods. In the example below the class Pet is created with a single method and a second method is added while the program is running.
class Pet def speak "Hello, I am a #{self.class}" end end buster = Pet.new puts buster.speak class Pet def speak_backwards "Hello, I am a #{self.class}".reverse end end puts buster.speak_backwards
Hello, I am a Pet teP a ma I ,olleH
Did you notice that the buster Pet object was able to use the method that was defined after buster was created. When a class is modified, all existing and new instances have access to the new methods.
4 Initializing an objects attributes
Ruby provides an initializing method named initialize that allows us to pass data parameters into the class when we create a new object. The initialize method is always called when a new object is created.
The example below show the initialize method that accepts one parameter (name) and passes that value into the body of the initialize method as a local variable. You can use that local variable to set a Pet objects name attribute.
class Pet def initialize(name) # Notice the instance variable here. @name = name end def speak puts "Hi! I am a #{self.class}. My name is #{@name}!" end end buster = Pet.new('Buster') puts buster.speak
Hello, I am a Pet. My name is Buster
5 Inheritance
We can create child classes that inherit the methods and variables of their super classes. For example, we can create a Dog class that inherits the properties of the Pet class.
#+BEGIN_SRC ruby :exports output :results output class Pet def initialize(name) @name = name end def speak puts "Hi! I am a #{self.class}. My name is #{@name}!" end end class Dog < Pet end buster = Dog.new('Buster') puts buster.speak
Hi! I am a Dog. My name is Buster!
We can enhance our Pet class by adding features that would apply to most pets, such as color, height, and weight. Heres an example of adding a color, sound, and address properties to the Pet class. You can see that the Dog class inherits the changes to the parent Pet class.
class Pet def initialize(name, color, sound, address) # Notice the instance variable here. @name = name @color = color @sound = 'Woof! Woof!' @address = address end def speak puts "Hi! I am a #{self.class}. My name is #{@name}! I am a beautiful shade of #{@color}! I live at #{@address}." end end class Dog < Pet end buster = Dog.new('Buster', 'golden brown', 'Woof! Woof!', 'the house where my food dish is') puts buster.speak
Hi! I am a Dog. My name is Buster! I am a beautiful shade of golden brown! I live at the house where my food dish is.
6 Overwriting a method
When we create a subclass we sometimes want to modify the methods inherited from the superclass and still use all of the functionality of the superclass. Heres how we can accomplish this with the super command.
class Employee def initialize(name, address) @name = name @address = address end def speak puts "Name: #{@name} Address: #{@address}." end end class Accountant < Employee def initialize(name, address, email) # We have to call the superclass initialize method. super(name, address) @email = email end def speak super # calls the superclass speak method puts "Email: #{@email}" end end accountant1 = Accountant.new('John Smith', '1 Market St, San Francisco, CA', 'jsmith@sanfrancisco.com') accountant1.speak
Name: John Smith Address: 1 Market St, San Francisco, CA. Email: jsmith@sanfrancisco.com
7 TODO Create a Cat and Horse class
For this exercise,
Create additional Cat and Horse classes that are sub-classes of Pet.
Using super, modify the speak method for the Cat and Horse classes to include all of the output for the Pet#speak method and add the @sound and @address information to the output.
Create several instances of each class and have each pet speak.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started