Newbie: Make this code better?

Discussion in 'Ruby' started by Joshua Muheim, Mar 27, 2007.

  1. 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-forum.com/.
     
    Joshua Muheim, Mar 27, 2007
    #1
    1. Advertising

  2. Joshua Muheim

    SonOfLilit Guest

    hmmm, class or instance variable / cached function?


    Have you looked at http://rubymentor.rubyforge.org ?


    Aur

    On 3/27/07, 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
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    SonOfLilit, Mar 27, 2007
    #2
    1. Advertising

  3. 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
     
    Timothy Hunter, Mar 27, 2007
    #3
  4. Joshua Muheim

    eden li Guest

    What's wrong with using CountriesController#controller_name?

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

    On Mar 27, 7:26 am, 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
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.
     
    eden li, Mar 27, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Bencsik
    Replies:
    2
    Views:
    835
  2. Andrew Thompson
    Replies:
    8
    Views:
    153
    Premshree Pillai
    Jun 7, 2005
  3. Jay Bornhoft
    Replies:
    13
    Views:
    206
  4. eli m
    Replies:
    3
    Views:
    211
    Jorgen Grahn
    Mar 10, 2013
  5. Replies:
    2
    Views:
    56
    Mark H Harris
    May 13, 2014
Loading...

Share This Page