8

I’m using Rails 4.2.4. How do I define a helper (private) method within a module? I have this module

module WebpageHelper

  def get_url(url)
    content = get_content(url)
    ..
  end

  def get_content(url)
    …
  end

  module_function :get_url

end

I don’t want the method “get_content” to be publicly accessible, but with the above code I get the error

Error during processing: undefined method `get_content' for WebpageHelper:Module

How do i properly define a private helper method in my module?

1 Answers1

0

I think the best way (and mostly how existing libs are written) do this by making a class within the module that deals with all the logic, and the module just provides a convenient method.

Docs here;

Private method

Or see Here for some nice examples e.g. class << self option.

Mazaz
  • 108
  • 3
  • I need a code example here, because if I'm reading you right, you're saying create a class with the helper method, which would make this method publicly accessible, which is exactly what I'm tryihng to avoid. This method will only be used exclusively in this module. –  Jul 22 '16 at 14:51
  • So the method of this inner class can only be accessed within the module itself? –  Sep 12 '16 at 17:06