[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Best way to create a helper method.

Patrick Li

8/15/2008 1:49:00 AM

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
--
Posted via http://www.ruby-....

4 Answers

Phlip

8/15/2008 1:52:00 PM

0

Patrick Li wrote:

> 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...

--
Phlip

Patrick Li

8/15/2008 2:10:00 PM

0

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
--
Posted via http://www.ruby-....

Phlip

8/15/2008 2:25:00 PM

0

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

protected ?

Phlip

8/15/2008 3:04:00 PM

0

Patrick Li wrote:

> 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.

--
Phlip