[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbie: Make this code better?

Joshua Muheim

3/26/2007 11:26:00 PM

Hi all

Another newbie question. I have the following code:

private
def model_obj_name
CountriesController.controller_class_name.underscore.sub(/_controller$/,
'').singularize
end

I use it quite a lot, and it's always the same, so I'd like to be
executed only once and then stored somewhere and every following time it
just returns this value.

I thought about putting this stuff into the initialize body and creating
an attr_accessor, but then it would be public, right? But I need it
private...

Any cool solution? Thanks :-)
Josh

--
Posted via http://www.ruby-....

3 Answers

SonOfLilit

3/26/2007 11:32:00 PM

0

hmmm, class or instance variable / cached function?


Have you looked at http://rubymentor.rub... ?


Aur

On 3/27/07, Joshua Muheim <forum@josh.ch> wrote:
> Hi all
>
> Another newbie question. I have the following code:
>
> private
> def model_obj_name
> CountriesController.controller_class_name.underscore.sub(/_controller$/,
> '').singularize
> end
>
> I use it quite a lot, and it's always the same, so I'd like to be
> executed only once and then stored somewhere and every following time it
> just returns this value.
>
> I thought about putting this stuff into the initialize body and creating
> an attr_accessor, but then it would be public, right? But I need it
> private...
>
> Any cool solution? Thanks :-)
> Josh
>
> --
> Posted via http://www.ruby-....
>
>

Tim Hunter

3/26/2007 11:34:00 PM

0

Joshua Muheim wrote:
> Hi all
>
> Another newbie question. I have the following code:
>
> private
> def model_obj_name
> CountriesController.controller_class_name.underscore.sub(/_controller$/,
> '').singularize
> end
>
> I use it quite a lot, and it's always the same, so I'd like to be
> executed only once and then stored somewhere and every following time it
> just returns this value.
>
> I thought about putting this stuff into the initialize body and creating
> an attr_accessor, but then it would be public, right? But I need it
> private...
>
> Any cool solution? Thanks :-)
> Josh
>
>
What about this?

private
def model_obj_name
@name ||= CountriesController.controller_class_name.underscore.sub(/_controller$/,
'').singularize
end




eden li

3/27/2007 2:28:00 AM

0

What's wrong with using CountriesController#controller_name?

http://api.rubyonrails.org/classes/ActionController/Base.ht...
Converts the class name from something like
"OneModule::TwoModule::NeatController" to "neat".

On Mar 27, 7:26 am, Joshua Muheim <f...@josh.ch> wrote:
> Hi all
>
> Another newbie question. I have the following code:
>
> private
> def model_obj_name
> CountriesController.controller_class_name.underscore.sub(/_controller$/,
> '').singularize
> end
>
> I use it quite a lot, and it's always the same, so I'd like to be
> executed only once and then stored somewhere and every following time it
> just returns this value.
>
> I thought about putting this stuff into the initialize body and creating
> an attr_accessor, but then it would be public, right? But I need it
> private...
>
> Any cool solution? Thanks :-)
> Josh
>
> --
> Posted viahttp://www.ruby-....