Uninitialized persistent error in Ruby class

I have two classes in RubyMine:

book.rb

 class Book
   def initialize(name,author)
   end
 end

test.rb

require 'book'
class teste
   harry_potter = Book.new("Harry Potter", "JK")
end

When I run test.rb, I get this error: C: /Users/DESKTOP/RubymineProjects/learning/test.rb: 3: in <class:Test>': uninitialized constant Test::Book (NameError) from C:/Users/DESKTOP/RubymineProjects/learning/test.rb:1:in'from -e: 1: in load' from -e:1:in'

+4
source share
3 answers

You defined an initialization method, but forgot to assign values ​​to instance variables, and a typo in the code caused an error, fixed it as:

book.rb

class Book
  def initialize(name,author)
    @name = name
    @author = author
  end
end

test.rb

require './book'
class Test
  harry_potter = Book.new("Harry Potter", "JK")
end

, ? , , Ruby - . "The Book of Ruby" .

+4

, require 'book' book.rb , Book.

Ruby , require, ./, , , ..

require './book'
+12

Rails , , :

book.rb

class Book
  def initialize(name, author)
  end
end

book_test.rb

class BookTest
  harry_potter = Book.new("Harry Potter", "JK")
end
0

All Articles