Forum: Ruby including a namespace for just one class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
53a1f0aba6f0489d49f5e6fc3df323fa?d=identicon&s=25 Robert James (robertjames)
on 2006-12-25 18:21
(Received via mailing list)
I would like to reference constants and classes defined in module
People in the class Schedule.

class Login is not part of any module (other than the root).

doing this:
class Schedule
  include People
  def a_method
    # Worker.new won't work
    People::Worker.new # works
  end
end

doesn't work.

Is there a way to include the module People so that I don't need to
prefix People:: for only one particular class?

Also, why doesn't what I tried work?  Doesn't include copy in the
constants, and aren't classes just constants?
F3b02532d4cb4855881935c002389213?d=identicon&s=25 Morton Goldberg (Guest)
on 2006-12-25 20:46
(Received via mailing list)
On Dec 25, 2006, at 12:20 PM, S. Robert James wrote:

>     People::Worker.new # works
>   end
> end
>
> Is there a way to include the module People so that I don't need to
> prefix People:: for only one particular class?

I don't know why it doesn't work for you -- it works fine for me.

<code>
module People
    class Worker
    end
end

class Schedule
    include People
    def a_method
       p Worker.new
    end
end

Schedule.new.a_method
</code>

<results>
#<People::Worker:0x88494>
</results>

Regards, Morton
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2006-12-25 21:20
(Received via mailing list)
On Dec 25, 2006, at 12:20 PM, S. Robert James wrote:
> I would like to reference constants and classes defined in module
> People in the class Schedule.

Here's an example that works fine for me:
  module People
    POPULATION = 1234567
  end

  class Schedule
    include People
    def x
      p POPULATION
    end
  end

  s = Schedule.new
  s.x
  #=> 1234567

  p VERSION
  #=> "1.8.5"
53a1f0aba6f0489d49f5e6fc3df323fa?d=identicon&s=25 Robert James (robertjames)
on 2006-12-25 22:30
(Received via mailing list)
Is it possible that Rails changes things somehow?
I think I know - the autoloader can't find them then!
53a1f0aba6f0489d49f5e6fc3df323fa?d=identicon&s=25 Robert James (robertjames)
on 2006-12-25 23:16
(Received via mailing list)
S. Robert James wrote:
> Is it possible that Rails changes things somehow?
> I think I know - the autoloader can't find them then!

Does that make any sense?  The autoloader can find them if I include
People in the global namespace, but not if I include it in the class?!
If so, is there a workaround?
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-12-28 06:23
(Received via mailing list)
On Tue, Dec 26, 2006 at 07:15:04AM +0900, S. Robert James wrote:
>
> S. Robert James wrote:
> > Is it possible that Rails changes things somehow?
> > I think I know - the autoloader can't find them then!
>
> Does that make any sense?  The autoloader can find them if I include
> People in the global namespace, but not if I include it in the class?!
> If so, is there a workaround?
>
Yes it makes sense and you are probably right. The workaround is to
explicitly require the file

e.g:
require 'people' # Or whatever the file is
This topic is locked and can not be replied to.