0

What would ::Schedule.new do in the following code? Isn't it missing something in front of the ::?

 module Schedulable
      attr_writer :schedule

      def schedule
        @schedule ||= ::Schedule.new
      end

      def schedulable?(start_date, end_date)
        !scheduled?(start_date - lead_days, end_date)
      end

    ..

    end
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
  • Hi, this post should answer your question: http://stackoverflow.com/questions/3009477/what-is-rubys-double-colon-all-about – Nikolai Manek Feb 10 '14 at 05:17
  • I've actually read that post, but I don't see how it answers my specific question here...Thanks – Hommer Smith Feb 10 '14 at 05:18
  • Calling your def schedule creates a new instance of a Schedule class but only if it's defined (of course) and stores it in (@)schedule: irb(main):013:0> schedule => # irb(main):014:0> @schedule => # – Nikolai Manek Feb 10 '14 at 05:28

2 Answers2

1

No.

If you put nothing in front of :: - it just says to the interpreter to use the global scope.

ie to look at the top-level of the hierarchy of objects, and look for a Schedule class there.

Taryn East
  • 27,486
  • 9
  • 86
  • 108
0

It creates a new instance of a Schedule class (which needs to be defined) but only if @schedule is not already defined. Therefore the "or equals" in front of it.

Here a simple test of it:

    irb(main):013:0> schedule
    => #<Schedule:0x3b9ccf62> // new Schedule instance
    irb(main):014:0> @schedule
    => #<Schedule:0x3b9ccf62> // same Schedule instance
    irb(main):015:0> @schedule = "foooo" // new value for @schedule
    => "foooo"
    irb(main):016:0> schedule // now method returns new value and no new instance of Schedule
    => "foooo"
Nikolai Manek
  • 980
  • 6
  • 16