https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112013-05-12T11:28:54ZRuby Issue Tracking SystemRuby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=392662013-05-12T11:28:54Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>This isn't a bug. If you load animal.rb then dog.rb, at the point Bark::Dog is defined, Bark::Animal does not exist, so it finds Animal at the top level (standard ruby constant lookup). If you want to force the behavior you desire, you should probably be specific about the superclass (class Dog < Bark::Animal) and require bark.rb at the top of dog.rb (to make sure Bark::Animal is defined before Bark::Dog).</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=392672013-05-12T11:34:37Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>It's a feature by the design.<br>
I can't get what you expect from your code.<br>
You will need more concrete proposal.</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=392682013-05-12T17:41:33ZeLobato (Daniel Lobato Garcia)elobatocs@gmail.com
<ul></ul><p>jeremyevans0: That's what I did when I found a problem caused by this on production code. Nonetheless you have to take into account that sometimes the order of the requires is not something you control.</p>
<p>nobu: In order to get these results, create the files, open IRB and run:</p>
<blockquote>
<p>require 'animal.rb'<br>
require 'dog.rb'<br>
require 'bark.rb'</p>
</blockquote>
<blockquote>
<p>Bark::Dog.new.bark<br>
fuck. <--- This should be woof because Bark::Dog should inherit from Bark::Animal.</p>
</blockquote>
<p>Let me know if I am missing anything.</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=392692013-05-12T18:07:11Zinjekt (Lee Jarvis)ljjarvis@gmail.com
<ul></ul><p>Nobu you don't need the requires to reproduce. This is simply the same thing:</p>
<p>#!/usr/bin/env ruby</p>
<p>class Animal<br>
def bark<br>
puts 'fuck.'<br>
end<br>
end</p>
<p>module Bark<br>
class Dog < Animal<br>
end<br>
end</p>
<p>module Bark<br>
class Animal<br>
def bark<br>
puts 'woof'<br>
end<br>
end<br>
end</p>
<p>Bark::Dog.new.bark</p>
<p>I don't think this is unexpected at all. I think Jeremy pretty much nailed the reasons why.</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=392882013-05-13T09:51:49Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>I know the reason of course.</p>
<p>The point is how others reading your code can guess your intention from the code.<br>
Can you explain why the result is "unexpected"?</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=393222013-05-14T11:00:10Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>One idea I'm thinking now is warning when a constant already introduced from the top-level is assigned.</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=393552013-05-16T00:16:05ZeLobato (Daniel Lobato Garcia)elobatocs@gmail.com
<ul></ul><p>This error showed up in a Rails app, on my code I had two different files (ProxyAPI::Resource and ProxyAPI::BMC < Resource), and somehow there was a separated Resource class defined by a loaded gem. Then ProxyAPI::BMC was inheriting from that one instead of ProxyAPI::Resource, which caused problems.</p>
<p>The warning could happen at initialization, but if a constant inside the module is added, IMO it should be inherited. Nonetheless I don't know too much about constant lookup at the moment, could you point me to the relevant parts of the code base or something like that?</p>
<p>My only concern with the warning showing up just when the class is defined, is that maybe it will be hard to notice, but it's surely better than no warning at all.</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=394252013-05-19T00:40:30Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><blockquote>
<p>The warning could happen at initialization, but if a constant inside the module is added, IMO it should be inherited.</p>
</blockquote>
<p>You can't inherit a constant which is not defined at that time yet.<br>
Even if same name constant is defined later, it isn't concerned.</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=394892013-05-23T03:14:33ZAnonymous
<ul></ul><p>OMG, why puts 'fuck'? ;_;</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=394902013-05-23T03:31:26Zjonforums (Jon Forums)
<ul></ul><p>boris_stitnicky (Boris Stitnicky) wrote:</p>
<blockquote>
<p>OMG, why puts 'fuck'? ;_;</p>
</blockquote>
<p>It's bait to see who gets distracted by irrelevant minutiae.</p>
<p>People curse. Some even scamper promisingly close to wit. Who cares. Move on. ;)</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=395062013-05-23T14:05:00Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><blockquote>
<p>OMG, why puts 'fuck'? ;_;</p>
</blockquote>
<p>Ruby doesn't prevent you from fucking yourself.</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=395072013-05-23T17:40:19Zrkh (Konstantin Haase)me@rkh.im
<ul></ul><p>eLobato (Daniel Lobato Garcia) wrote:</p>
<blockquote>
<p>This error showed up in a Rails app, on my code I had two different files (ProxyAPI::Resource and ProxyAPI::BMC < Resource), and somehow there was a separated Resource class defined by a loaded gem. Then ProxyAPI::BMC was inheriting from that one instead of ProxyAPI::Resource, which caused problems.</p>
<p>The warning could happen at initialization, but if a constant inside the module is added, IMO it should be inherited. Nonetheless I don't know too much about constant lookup at the moment, could you point me to the relevant parts of the code base or something like that?</p>
<p>My only concern with the warning showing up just when the class is defined, is that maybe it will be hard to notice, but it's surely better than no warning at all.</p>
</blockquote>
<p>Warnings would need to be really really smart or a lot of libraries will start printing warnings (for instance, rack defines Rack::File). I have to say, the whole issue would not have occurred if you had either required bark from dog (which is what require is for) or, if you want to use Rails' autoloading magic, used the qualified constant name (ie inherit from Bark::Animal).</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=395302013-05-26T03:02:18ZeLobato (Daniel Lobato Garcia)elobatocs@gmail.com
<ul></ul><p>Fair point rkh, feel free to close this unless there is something to avoid printing lots of warnings.</p> Ruby master - Feature #8393: A class who's parent class is in a module can go wrong if files are required in the wrong orderhttps://redmine.ruby-lang.org/issues/8393?journal_id=395362013-05-27T15:14:38Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Rejected</i></li></ul>