views:

124

answers:

2

I have a jruby rails app with a ruby module in lib that is name-spacing my java objects so I don't have conflicts.

I'm wondering what the difference is between including the specific classes in that module and including the package. I've included the sample code below.

In the console, for example 1 when I say MyMod:: and hit tab, it has (for example) 101 methods and class options with MyMod::MyClass being one of them.

For example 2, when I hit MyMod:: and tab, it only has 100 method/class options and it doesn't contain MyClass. If I then go and reference MyMod::MyClass, then run that MyMod:: tab again, I now have 101 options and MyClass is listed.

Here's my question. What's the difference between referencing these classes immediately in my module à la example 1 vs having them load on demand like example 2. If I have a package with about 20 classes that I use, is it preferred to have them loaded on demand or up front, and is there any overhead to loading this on demand, à la example 2

Sample Code:

example 1

module MyMod
  MyClass = Java::my.package.MyClass
  ....
end

vs example 2

module MyMod
  include_package "my.package"
end
A: 

I got slightly different results. Perhaps you're using a different auto-completer?

irb(main):001:0> module MyMod;MyClass = Java::JavaUtil::Date;end
=> Java::JavaUtil::Date
irb(main):002:0> module OtherMod;include_package "Java.Util";end
=> nil
irb(main):003:0> MyMod.methods.size
=> 109
irb(main):004:0> OtherMod.methods.size
=> 109
irb(main):005:0> require 'irb/completion'
=> true
irb(main):006:0> MyMod::
Display all 110 possibilities? (y or n)
irb(main):006:0> OtherMod::
Display all 109 possibilities? (y or n)
irb(main):006:0> OtherMod::
irb(main):007:0*

As to your question, I don't know for sure. But if I had to guess, d'd say that as ruby is dynamic, neither approach loads anything up-front.

Rob
I'm using rails console. I just tried your example with two rb files created to your spec with the same results, however methods doesn't seem to list the available classes to that module. Here's some output from my console >> MyMod.methods.size => 161 >> OtherMod.methods.size => 161 >> MyMod::<tab> Display all 162 possibilities? (y or n) >> OtherMod::<tab> Display all 161 possibilities? (y or n)
brad
oops... that doesn't format too well, i think you get it though >> is obviously the console prompt
brad
+1  A: 

If you really are going to use a class, it is going to cost you something at some point. It doesn't matter whether loading it specifically or on-demand. I think for your 2 examples, if you do use MyClass, then there's no difference. On the other hand, if MyClass is never used, then example 1 is clearly wasting something.

Also, include_package does not really pull the whole package in, but kind of like establishing a search scope when needing a class. Generally it is not recommended to use include_package. See JRUBY-2376 for problems it has.

bryantsai
Great thanks. I'm not really extending or overriding any java code in my ruby, which is what that bug seems to pertain to, I'm literally just including the packages so I can reference them, good to know though.
brad