Best way to create a helper method.

P

Patrick Li

What would you guys say is the best way of creating a helper method?

module MyModule
def myMethod
#bla bla
end
end

def helperMethod
#a helper method whose existence should not be known outside of this
file
end

helperMethod() shouldn't be visible from anywhere outside this file.

Thanks for helping me with this
-Patrick
 
P

Phlip

Patrick said:
What would you guys say is the best way of creating a helper method?

module MyModule
def myMethod
#bla bla
end
end

def helperMethod
#a helper method whose existence should not be known outside of this
file
end

helperMethod() shouldn't be visible from anywhere outside this file.

Can you put helperMethod inside MyModule, and make it private? I don't know if
the language supports that.

And you may want to consider unit tests as more important than static type
checking, to keep a program on track...
 
P

Patrick Li

I can:

module MyModule
def myMethod
#bla bla
end

private
def helperMethod
#a helper method whose existence should not be known outside of this
file
end
end

But I can still get access to helperMethod by mixing in the module.

eg.
class MyClass
include MyModule #I want myMethod() but not helperMethod()
end

Thanks for replying
-Patrick
 
P

Phlip

Patrick said:
But I can still get access to helperMethod by mixing in the module.

Private methods are not a form of code security. Even if you made this method
completely private, you can still get to it with .send:)helperMethod).

No language makes any private method absolutely private. You should think of
privacy as nothing more than a warning - "please reconsider calling this
method". Another common way to do that is move the method out of the default
namespace by calling it _helperMethod.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,658
Latest member
jaguar32

Latest Threads

Top