IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Ruby threw me a curve
I am using Ruby on Rails for a small project of mine, and I came across a little piece of code there:

>>>>>>>>>>>>
alias_method :initialize_without_callbacks, :initialize
alias_method :initialize, :initialize_with_callbacks
<<<<<<<<<<<<

So we save the original method, and redefine it as something else.

Only one problem. This is executed twice.

I actually don't know why it's executed twice, but the second time around the original method gets replaced by the the overwritten one, and when we try to call initialize() we get an infinite recursion.

Bleah

The solution I came up with is to check for existance of :initialize and non-existance of initialize_without_callbacks, and then do the aliasing. That is complicated by the fact that initialize() is private, so I can't use method_defined?().

Will see if good folks on rails list will suggest something better...

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

New Are you sure that is executed twice?
It looks like you're overwriting initialize and then you're trying to save initialize elsewhere. Reverse those two lines and see if it works better.

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New I am quite sure
And it's not me who is doing this, either - it's deep in the bowels of Ruby on Rails

alias_method uses first arguent as new name, second as old name, so they do it in the right order, imho

And, I printed a stack trace from the moment when we pass the code - see it here:

[link|http://thread.gmane.org/gmane.comp.lang.ruby.rails/28621|http://thread.gmane.....ruby.rails/28621]

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

New Ah, you're right, I looked closer at the method definition.
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New OK, now I see it
Dunno if I can get the official Rails people to act...

they use a "defined? ClassName" operator, which, unfortunately, triggers loading of the class. This explains the first call stack. As Ruby Gem is not expecting this to happen (the gem that triggers the loading doe not have the gem thatcontains ClassName as a dependency), it loads the ClassName once again at the end of activate() call.

SO the solution I'd suggest is either declare teh dependency, or get rid of the code that triggers loading and replace it with a test that does not load class it's supposed to check the existance of.

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

New Let me get this straight ...
During the loading of a RubyGem library, a "defined? ClassName" call in that library triggers the loading of a file (Rails uses the missing_const hook to automatically load classes), and then the RubyGems activate command completes by loading the file again.

Do I understand the scenario correctly?

Sounds like this might be a problem with the way RubyGems handles the loading. Is there something that RubyGems can do to address this? If there is, I'd like to fix it.

Sorry for the late response ... I don't read this forum too regularly (it needs an RSS feed).
--
-- Jim Weirich jim@weirichhouse.org [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New The scenario is correct
Why it happens, I have no idea.

Worse, right-thinking folks at Rails mailing list can't reproduce the behavior. I, on the other hand, can't get rid of it. The only solution for me is to require_gem ActiveRecord before requiring rails.

I noticed that RubyGems does loading in sorta two stages - first it adds the directory to load path, and then eventually it does the actual "require". I am not sure I understand how it works and why it's done that way. If I did, I'd be better equiped to judge whether RubyGems is doing something wrong...

In general, wouldn't it be fair to say that if you refer to gem, you should specify a dependency on the gem? I think RubyGems is blameles in this.

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

New Re: The scenario is correct
In general, wouldn't it be fair to say that if you refer to gem, you should specify a dependency on the gem? I think RubyGems is blameles in this.

Depends on what kind of dependency you are refering to. The dependencies in the gem specifications are only used to determine what is needed at download time -- they have no effect on the system runtime. The 'require_gem' dependencies are only needed if you are using a particular version of a gem, otherwise you can leave them off entirely and RubyGems will pick out the latest version you have installed.

Let's see if we can nail this problem. What version of RubyGems are you using (the latest released version is 0.8.11)? Also what versions of ActiveRecord do you have installed? When you do a require_gem, are you getting the latest version or are you specifying a particular version?

If you can reproduce the problem in a reasonable small set of code, I would be willing to take a look at it.
--
-- Jim Weirich jim@weirichhouse.org [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New My environment
I seem to load 0.8.1, and then 0.8.11 upgrade

I simply used gems to grab rails and some extras, and here is what I have now:

>>>>>>>>>>>>>>>
actionmailer (1.1.3)
Service layer for easy email delivery and testing.

actionpack (1.11.0)
Web-flow and rendering framework putting the VC in MVC.

actionwebservice (0.9.3)
Web service support for Action Pack.

activerecord (1.13.0)
Implements the ActiveRecord pattern for ORM.

activesupport (1.2.3)
Support and utility classes used by the Rails framework.

rails (0.14.3)
Web-application framework with template engine, control-flow layer,
and ORM.

rake (0.6.2)
Ruby based make-like utility.

rubygems-update (0.8.11)
RubyGems Update GEM

sources (0.0.1)
This package provides download sources for remote gem installation
<<<<<<<<<<<<<<


As to the role of dependencies in loading process, don't you try to activate dependencies before activating the main gem?


I will try to get a small example, but it would require me to manufacture gems, something I really have no experience with. I'll try it during the week.

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

New Re: My environment
and here is what I have now:

[... list of gem packages elided ...]

Good, it looks like you only have one version of each package installed. (Gems should handle multiple versions, but since you are having problems it is good to eliminate variables). I assume that none of these are installed directly (as non-gem packages), correct?

As to the role of dependencies in loading process, don't you try to activate dependencies before activating the main gem?

Yes, but gems should be handling that. As soon as you require a file that is not found in the search path, RubyGems will find a gem that can satisfy that required file and activate it automatically. There is no need for the user to do anything special unless (a) they desire a particular version of a package and wish to manually activate it, or (b) there are some conflicts where RubyGems activates the wrong gem because of a name clash.

I will try to get a small example, but it would require me to manufacture gems, something I really have no experience with. I'll try it during the week.

If you get another together, I would love to see it.

I just had a thought. You might want to try out the latest beta version of RubyGems to see if that addresses the issue. There are some subtle autorequire bugs that were stamped out in the beta version and perhaps there is one that is affecting you.

You can get the latest beta version of RubyGems with:
  gem update --system --source [link|http://onestepback.org/betagems|http://onestepback.org/betagems]

--
-- Jim Weirich jim@weirichhouse.org [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New OK, I see more clearly now
I have sample code that repropduces the problem.

The code is essentially a top-level gem that depends on gems 1, 2 and 3.

2 and 3 depend on 1

3 uses "load("second.rb")" to load 2 in the middle of its .rb file (that's a huge simplification, but the complex code in rails boils down to "load". They do it to be able to refresh code after it gets changed during development, I think).

Now, if I have a test file that contains

require 'rubygems'
require 'top'
require 'third'
require 'second'


I get "second" loaded twice.

If you need my source, let me know where to post a tarball.



------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

New The rain has gone
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
Expand Edited by jake123 Nov. 19, 2005, 02:31:10 AM EST
New I can see all obstacles in my way
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New Gone are the dark clouds that had me blind
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New It's gonna be a bright (bright) bright (bright) sunshiny day
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New A source of annoying catchphrases now.
New Sorry, saw Arkadiy's subject line and it just popped into my
head, fully formed. After that it was just inevitable that I was going to do that... gotta do something about that I guess.
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New (Twas just the LRPDism. :-)
New Except the song has no "ok" or "more"
Of course, I thought of them as well.
New Ditto. It's got staying power.
New But not in that way, pervs.
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New Grrrrrrr.

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

New Re: OK, I see more clearly now
3 uses "load("second.rb")" to load 2 in the middle of its .rb file (that's a huge simplification, but the complex code in rails boils down to "load". They do it to be able to refresh code after it gets changed during development, I think).

Ahhh ... require won't load the same file twice, but load doesn't know anything about require's list of file names. So files loaded by 'load' are not reported to 'require' ... or something like that.

If Rails is working hard to load the file itself, is it possible for you to omit your require statement? There are a lot of declarations in rails that just handle the file loading automatically for you ... is there something you can take advantage of?
--
-- Jim Weirich jim@weirichhouse.org [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New It should be possible
But in the real code the require statementr is not there, either. The only thing that's "require"d is the top-level module.

I need to improve my model to figure out how that happens. Will try to do it later.

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

New Re: Ruby threw me a curve
Arkadiy,

Have you made any more progress here?

If you did manage to make modifications that enabled you to write a functioning rails app, I would appreciate it very much if you could post those modifications here. Even posting your files (which I could then diff against the non-modified rails files) would be a great help, if listing your changes would be too time consuming.

I can't understand how some people (you and me, for example) get this error that prevents even the most trivially simple rails app from functioning, yet the vast majority of people get no error. I really don't understand it at all.

Thanks a lot for all your work.
Mike
New Hello, fellow sufferer.
I did manage to get my app working by loading ActiveRecord explicitly in boot.rb, like this:

require 'rubygems'
require_gem 'activerecord' # this is my new line
require 'initializer'


I have no idea why most people don't have this problem...

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

     Ruby threw me a curve - (Arkadiy) - (25)
         Are you sure that is executed twice? - (ben_tilly) - (22)
             I am quite sure - (Arkadiy) - (21)
                 Ah, you're right, I looked closer at the method definition. -NT - (ben_tilly)
                 OK, now I see it - (Arkadiy) - (19)
                     Let me get this straight ... - (JimWeirich) - (18)
                         The scenario is correct - (Arkadiy) - (17)
                             Re: The scenario is correct - (JimWeirich) - (16)
                                 My environment - (Arkadiy) - (15)
                                     Re: My environment - (JimWeirich) - (14)
                                         OK, I see more clearly now - (Arkadiy) - (13)
                                             The rain has gone -NT - (jake123) - (10)
                                                 I can see all obstacles in my way -NT - (jake123) - (8)
                                                     Gone are the dark clouds that had me blind -NT - (jake123) - (7)
                                                         It's gonna be a bright (bright) bright (bright) sunshiny day -NT - (jake123) - (6)
                                                             A source of annoying catchphrases now. -NT - (Another Scott) - (5)
                                                                 Sorry, saw Arkadiy's subject line and it just popped into my - (jake123) - (4)
                                                                     (Twas just the LRPDism. :-) -NT - (Another Scott)
                                                                     Except the song has no "ok" or "more" - (broomberg) - (2)
                                                                         Ditto. It's got staying power. -NT - (Another Scott) - (1)
                                                                             But not in that way, pervs. -NT - (jake123)
                                                 Grrrrrrr. -NT - (Arkadiy)
                                             Re: OK, I see more clearly now - (JimWeirich) - (1)
                                                 It should be possible - (Arkadiy)
         Re: Ruby threw me a curve - (MikeH727) - (1)
             Hello, fellow sufferer. - (Arkadiy)

1500: The Goal, the Mission, the Odyssey.
263 ms