Using inheritance with multiple files in 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

Answers


Changes to be done in puppy.rb require the file in the following way. Assuming you have both files are in the same directory.

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

Need Your Help

Getting x,y in 3d coordinate system from 2d image pixel

c++ opencv camera-calibration pose-estimation

Using camera with fixed position I have to track an object in motion and to localize it in some pre chosen coordinate system. Idea is to put a specific color marker on the top of the object with kn...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.