Using inheritance with multiple files in Ruby

Posted by Preethi Jain on Stack Overflow See other posts from Stack Overflow or by Preethi Jain
Published on 2012-09-09T08:32:05Z Indexed on 2012/09/09 9:38 UTC
Read the original article Hit count: 234

Filed under:

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

© Stack Overflow or respective owner

Related posts about ruby