Writing Ruby Libraries - hiding methods from outside the module

Posted by JP on Stack Overflow See other posts from Stack Overflow or by JP
Published on 2010-05-24T15:56:30Z Indexed on 2010/05/24 16:01 UTC
Read the original article Hit count: 199

Filed under:
|
|

Hi all, I'm writing a Ruby library which has a module with a bunch of classes inside it. Many of these classes need to be usable and modifiable by calling scripts, but I don't want (some of) the initializers to be visible/callable:

module MyLib
  class Control
    def initialize
      # They can use this
    end

    def do_stuff
      Helper.new('things')
    end
  end

  class Helper
    # Shouldn't be visible 
    def initialize(what)
      @what = what
    end

    def shout
      @what
    end
  end
end

c = MyLib::Control.new
h = c.do_stuff
p h.shout
# => "things"
# ^ All of this is desired

# v This is undesirable
p MyLib::Helper.new('!')
# => <MyLib::Helper @what='!'>

If it's a simple thing, then I'd also appreciate the generated RDoc not even include the .new method for the Helper class either. Any ideas?

Thanks for reading!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about class