Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

"include" does not behave the same in rubinius as in MRI or JRuby #2829

Open
rossman2 opened this Issue · 2 comments

3 participants

@rossman2

If you include a module then attempt to modify a class from the included module, you must specify the modules namespace or a new class is created instead of modifying the modules class. For example, if the following code was in a file named "mymod.rb":


module TestMod
   class SupportClass
      def print_modclass_method
         puts "This is from the module's class method"
      end
   end
end

Then "require" the file and include the "TestMod" namespace as follows:


require './mymod.rb'
include TestMod

class TestMod::SupportClass  # This line works in rubinius
#class SupportClass  # This line does NOT work in rubinius, but does in jruby and mri

   def print_should_add
      puts "This is from the dynamically added method"
   end
end

SupportClass.new.print_should_add
SupportClass.new.print_modclass_method

As noted, the commented out line does not work in rubinius, but it will work properly in JRuby and MRI. What it appears to do is create a new class which shadows the TestMod::SupportClass. Therefore the call to "print_modclass_method" fails because it does not exist in "SupportClass".

@jemc
Collaborator

I can confirm and reproduce this:
MRI 2.1.1p76:

ruby -e "module A; class B; end; end; include A; class B; p self==A::B; end"
true

rbx-2.2.6:

ruby -e "module A; class B; end; end; include A; class B; p self==A::B; end"
false
@jemc
Collaborator

If it helps, this only applies when it is included at the top level, not when in a module as below:
MRI 2.1.1p76:

ruby -e "module A; class B; end; end; module C; include A; class B; p self==A::B; end; end"
false

rbx-2.2.6:

ruby -e "module A; class B; end; end; module C; include A; class B; p self==A::B; end; end"
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.