Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reference ruby class without module name

Tags:

ruby

Is there a way in ruby to load a module containing many classes and be able to access these classes without prefixing them with the module name? Consider foo.rb and bar.rb:

foo.rb:

require 'bar'
bar = BarModule::Bar.new()

bar.rb

module BarModule
  class Bar
  end
end

Basically I'd like the ability, from foo.rb, to refer to the class "Bar" without specifying its module every time I reference it. In java terms, I'm looking for something akin to:

import BarModule.*;

Anything like that exist?

like image 442
jph Avatar asked Apr 19 '12 19:04

jph


1 Answers

Modules can be mixed in to one another. To use BarModule as a mixin, you want to include BarModule.

like image 125
Chuck Avatar answered Oct 01 '22 19:10

Chuck