SuperCheezGi
SuperCheezGi

Reputation: 879

My Ruby require statement is throwing errors

I brain farted and didn't look in the related question section.
My answer has since been found.

(Yes, I'm new to Ruby.)
This is what my console outputs:

C:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- Week (LoadError)
    from C:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
    from main.rb:14:in `<main>'

This is my main.rb file:

require 'week.rb'

class Decade
include Week

    no_of_yrs = 10

    def no_of_months
         puts Week::FIRST_DAY
         number = 10 * 12
        puts number
    end
end

d1 = Decade.new
puts Week::FIRST_DAY

Week.weeks_in_month
Week.weeks_in_year

d1.no_of_months

This is my week.rb:

module Week
    FIRST_DAY = "Sunday"

    def Week.weeks_in_month
        puts "You have four weeks in a month"
    end

    def Week.weeks_in_year
        puts "You have 52 weeks in a year"
    end
end

The problem could be just some stupid naming error, but I haven't found it if it is.

Upvotes: 1

Views: 159

Answers (2)

Eli Rose
Eli Rose

Reputation: 6988

require_relative 'week.rb'

should also work.

Upvotes: 1

lurker
lurker

Reputation: 58224

Ruby looks for your require modules in directories found in the $LOAD_PATH environment variable. If it's not there, it squawks. You can do a couple of different things to fix it:

  • Include the full path or relative path in your require: e.g., require './weeks.rb'
  • Include the path of the file in LOAD_PATH
  • Move weeks.rb into a path that is already in LOAD_PATH

Upvotes: 2

Related Questions