Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using inheritance with multiple files in Ruby

Tags:

ruby

I am new to Ruby . I have a question with respect to using Inheritence in Ruby .

I have a class called as Doggy inside a file named Doggy.rb

class Doggy
  def bark
    puts "Vicky is barking"
  end
end

I have written another class named Puppy in another file named puppy.rb

class Puppy < Doggy
end

puts Doggy.new.bark

I am getting this Error:

Puppy.rb:1:in `<main>': uninitialized constant Doggy (NameError)

Is it mandatory to have these classes (Doggy and Puppy ) inside a single file only?

Edited

As per the suggestions , i have tried using require and require_relative as shown , but still i am getting below Error

Puppy.rb:1:in `<main>': uninitialized constant Doggy (NameError)

    class Puppy < Doggy
    end
    require_relative 'Doggy.rb'
    puts Doggy.new.bark
like image 247
Pawan Avatar asked Sep 09 '12 08:09

Pawan


People also ask

How do you use multiple inheritance in Ruby?

Ruby does not support multiple inheritance. It only supports single-inheritance (i.e. class can have only one parent), but you can use composition to build more complex classes using Modules.

How does inheritance work in Ruby?

In Ruby, single class inheritance is supported, which means that one class can inherit from the other class, but it can't inherit from two super classes. In order to achieve multiple inheritance, Ruby provides something called mixins that one can make use of.

What is super class in Ruby?

Ruby uses the super keyword to call the superclass implementation of the current method. Within the body of a method, calls to super acts just like a call to that original method. The search for a method body starts in the superclass of the object that was found to contain the original method.

Can modules inherit Ruby?

The Ruby class Class inherits from Module and adds things like instantiation, properties, etc – all things you would normally think a class would have. Because Module is literally an ancestor of Class , this means Modules can be treated like classes in some ways.


4 Answers

Changes to be done in puppy.rb file
Assuming both files are in the same directory, you're expected to require the file in the following way:

doggy.rb

class Doggy
  def bark
    puts "Vicky is barking"
  end
end

puppy.rb

require File.expand_path('../doggy.rb', __FILE__)
class Puppy < Doggy
end

puts Doggy.new.bark
like image 176
AnkitG Avatar answered Sep 23 '22 15:09

AnkitG


You should require file with Doggy class in it from file where Puppy is. Put

require './doggy'

or, if you are on ruby-1.9:

require_relative 'doggy'

in puppy.rb (assuming file names are doggy.rb and puppy.rb).

like image 40
Victor Deryagin Avatar answered Sep 19 '22 15:09

Victor Deryagin


Also, in addition to what everyone else has said, puts Dog.new.bark will always fail, because your class is not called Dog, it's Doggy. Beware.

like image 40
stephenmurdoch Avatar answered Sep 23 '22 15:09

stephenmurdoch


Not necessary, you have to require the file where Doggy is declared. You can use require or require_relative.

Then, anyway make sure you use the name you declared: Doggy and not Dog.

like image 31
Waiting for Dev... Avatar answered Sep 20 '22 15:09

Waiting for Dev...