Confusing code behavior in Rails

Discussion in 'Ruby' started by szymon.rozga, Jun 21, 2005.

  1. szymon.rozga

    szymon.rozga Guest

    I have a class in /project_dir/lib called Failure. Failure has one
    instance variable, t. But, if you call f.<anything> ( f being of type
    Failure ), I override method_missing and create a method called
    <anything> which returns 10 (it's a simplification of a much bigger
    problem).

    It works flawlessly, but only once. Under the trend action of my
    controller, I instantiate a new instance of Failure. The first time I
    load the action and try to display some variable on the view,
    everything works. However, if I reload the page, all the instance
    variables created on the fly return Nil. And they don't function
    properly until I restart Webrick. And then it does the only once thing
    again.

    Any thoughts ? Or feel free to ask for more details.
    szymon.rozga, Jun 21, 2005
    #1
    1. Advertising

  2. szymon.rozga wrote:
    > I have a class in /project_dir/lib called Failure. Failure has one
    > instance variable, t. But, if you call f.<anything> ( f being of type
    > Failure ), I override method_missing and create a method called
    > <anything> which returns 10 (it's a simplification of a much bigger
    > problem).
    >
    > It works flawlessly, but only once. Under the trend action of my
    > controller, I instantiate a new instance of Failure. The first time I
    > load the action and try to display some variable on the view,
    > everything works. However, if I reload the page, all the instance
    > variables created on the fly return Nil. And they don't function
    > properly until I restart Webrick. And then it does the only once thing
    > again.


    It sounds like this is what's happening: You're creating these instance
    variables and accessor methods in method_missing the first time
    through. Then, after the page is done loading, Rails destroys its
    world. When you load the page the second time, it re-creates
    everything, but the accessor methods are already defined, so it never
    falls through to method_missing and you're just accessing empty
    instance variables.
    Charles Steinman, Jun 21, 2005
    #2
    1. Advertising

  3. szymon.rozga

    szymon.rozga Guest

    Thanks for the quick response. I know the user is going to want to
    access an array of methods whose name will be determines from the user
    input. If I simply create these methods during the initialization of
    the object, I should be ok? Or is there a better solution I'm not
    considering?
    szymon.rozga, Jun 21, 2005
    #3
  4. szymon.rozga

    Eric Hodel Guest

    On 21 Jun 2005, at 00:00, szymon.rozga wrote:

    > Thanks for the quick response. I know the user is going to want to
    > access an array of methods whose name will be determines from the user
    > input. If I simply create these methods during the initialization of
    > the object, I should be ok? Or is there a better solution I'm not
    > considering?


    You could put this information in the database and wrap it up with a
    Model.

    --
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
    Eric Hodel, Jun 21, 2005
    #4
  5. Make sure you're using require_dependency rather than require, so
    rails knows it needs to reload the file when in development mode.

    Cheers,

    Chris

    On 6/21/05, Charles Steinman <> wrote:
    >=20
    > szymon.rozga wrote:
    > > I have a class in /project_dir/lib called Failure. Failure has one
    > > instance variable, t. But, if you call f.<anything> ( f being of type
    > > Failure ), I override method_missing and create a method called
    > > <anything> which returns 10 (it's a simplification of a much bigger
    > > problem).
    > >
    > > It works flawlessly, but only once. Under the trend action of my
    > > controller, I instantiate a new instance of Failure. The first time I
    > > load the action and try to display some variable on the view,
    > > everything works. However, if I reload the page, all the instance
    > > variables created on the fly return Nil. And they don't function
    > > properly until I restart Webrick. And then it does the only once thing
    > > again.

    >=20
    > It sounds like this is what's happening: You're creating these instance
    > variables and accessor methods in method_missing the first time
    > through. Then, after the page is done loading, Rails destroys its
    > world. When you load the page the second time, it re-creates
    > everything, but the accessor methods are already defined, so it never
    > falls through to method_missing and you're just accessing empty
    > instance variables.
    >=20
    >=20
    >
    Chris McGrath, Jun 21, 2005
    #5
    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. clintonG
    Replies:
    4
    Views:
    592
    clintonG
    Oct 17, 2005
  2. shapper

    GridView ... confusing behavior

    shapper, Mar 9, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    269
    Greg9Strat
    Mar 9, 2007
  3. scigeek
    Replies:
    7
    Views:
    346
    scigeek
    Sep 8, 2009
  4. paul

    confusing thread behavior

    paul, Dec 3, 2009, in forum: Python
    Replies:
    2
    Views:
    428
  5. Rudi Cilibrasi
    Replies:
    6
    Views:
    840
    Rudi Cilibrasi
    Nov 6, 2011
Loading...

Share This Page