Newbie: Make this code better?

J

Joshua Muheim

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
 
T

Timothy Hunter

Joshua said:
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
 

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
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top