Gem::Platform::Java Uninitialized Constant

All -

I’m creating a gem whose dependencies differ by platform – if MRI Ruby,
it
needs to require rjb, and if JRuby, it very much needs not to require
rjb.

In my gemspec file, I have something like this:

if /java/ === RUBY_PLATFORM
spec.platform = Gem::Platform::JAVA
end

However, when I gem build this gemspec in JRuby, I get this error:

Invalid gemspec in [mygem.gemspec]: uninitialized constant
Gem::Platform::JAVA

I also get that error for MSWIN. In pry, I can see that the constants
do
exist:

[8] pry(main)> cd Gem::Platform
[9] pry(Gem::Platform):1> ls
constants: CURRENT JAVA MINGW MSWIN RUBY
Gem::Platform.methods: local match new
Gem::Platform#methods:
== === =~ _deprecated_empty? cpu cpu= empty? eql? hash
inspect
os os= to_a to_s version version=
instance variables: @local
locals: _ __ dir ex file in out pry

Anyone know what’s going on here? My ultimate goal is to have a gem
that’s
dependent on this one install the one appropriate to the platform, and
itself have an MRI and JRuby version.

Thanks for any help,
Keith

On Wed, Jul 17, 2013 at 4:05 PM, Keith B.
[email protected]wrote:

Gem::Platform::JAVA

quick fix would be to use the string “java”

  • christian

BTW: my ruby2 does not have Gem::Platform::JAVA either - which does not
mean much since today is the first time I use ruby2

Hi Keith,

I don’t see a Gem::Platform::JAVA when running under plain JRuby 1.7.4:

jruby-1.7.4 :001 > RUBY_PLATFORM
=> “java”
jruby-1.7.4 :003 > Gem::VERSION
=> “1.8.24”
jruby-1.7.4 :004 > Gem::Platform::JAVA
NameError: uninitialized constant Gem::Platform::JAVA
from org/jruby/RubyModule.java:2631:in const_missing' from (irb):5:in evaluate’
from org/jruby/RubyKernel.java:1093:in eval' from org/jruby/RubyKernel.java:1489:in loop’
from org/jruby/RubyKernel.java:1254:in catch' from org/jruby/RubyKernel.java:1254:in catch’
from /Users/rsutphin/.rvm/rubies/jruby-1.7.4/bin/irb:13:in `(root)’

I also don’t see it when grepping the source for rubygems.

Perhaps Gem::Platform::JAVA is something introduced by something that’s
loaded in the pry environment but not from a general gem install
environment? Or perhaps there are different versions of rubygems
involved.

HTH,
Rhett

From: Keith B. [email protected]
Reply-To: [email protected]
Date: Wednesday, July 17, 2013 10:05 AM
To: [email protected]
Subject: [jruby-user] Gem::Platform::Java Uninitialized Constant

That’s the irony. It does have it – it just can’t be specified (see
pry
output below, running MRI 2.0).

I know I can specify “java”, I was doing that before, but I’m skeptical
that something normally requiring a composite object can be assigned
only
one of those elements, and still work properly. I’m having problems
getting platform detection and installation of the proper platform’s gem
working properly, and I thought cleaning up this issue might help.

Thanks,
Keith

[1] pry(main)> cd Gem::Platform::JAVA
[2] pry(#Gem::Platform):1> ls
Gem::Platform#methods: == === =~ cpu cpu= eql? hash inspect os
os=
to_a to_s version version=
self.methods: pry
instance variables: @cpu @os @version
locals: _ __ dir ex file in out pry
[3] pry(#Gem::Platform):1> to_s
=> “java”
[4] pry(#Gem::Platform):1> @cpu
=> nil
[5] pry(#Gem::Platform):1> @os
=> “java”
[6] pry(#Gem::Platform):1> @version
=> nil
[7] pry(#Gem::Platform):1> RUBY_VERSION
=> “2.0.0”

Rhett -

You’re right! It didn’t occur to me that the platforms might differ by
platform. :wink:

This would be a JRuby bug, right?:

JRuby Gem::Platform Constants:

constants: CURRENT_SPECIFICATION_VERSION MARSHAL_FIELDS
NONEXISTENT_SPECIFICATION_VERSION TODAY

MRI Ruby 1.9.3 Gem::Platform Constants:

constants: CURRENT JAVA MINGW MSWIN RUBY

  • Keith

Hi Keith,

Gem::Platform::JAVA doesn’t exist in my install of 1.9.3-p448, either:

1.9.3p448 :001 > RUBY_PLATFORM
=> “x86_64-darwin12.4.1”
1.9.3p448 :002 > Gem::VERSION
=> “1.8.25”
1.9.3p448 :003 > Gem::Platform::JAVA
NameError: uninitialized constant Gem::Platform::JAVA
from (irb):4
from /Users/rsutphin/.rvm/rubies/ruby-1.9.3-p448/bin/irb:16:in
`’

I think there’s something getting pulled into the environment where you
are running pry for 1.9.3 that’s adding the JAVA constant. It looks like
bundler is one possible culprit:

1.9.3p448 :004 > require ‘bundler’
=> true
1.9.3p448 :005 > Gem::Platform::JAVA
=> #<Gem::Platform:0x3fdfa2cf5a3c @cpu=nil, @os=“java”, @version=nil>

So you shouldn’t rely on that constant in your gemspec. The only
constant
that rubygems itself defines is Gem::Platform::RUBY, and that’s just the
string “ruby”. I think that using the string “java” is what’s expected
I
can back up kristian in saying that “java” definitely works. If you look
at the source for Gem::Platform, you can see where it maps from the
string
to the composite object.

Rhett

From: Keith B. [email protected]
Reply-To: [email protected]
Date: Wednesday, July 17, 2013 10:39 AM
To: [email protected]
Subject: Re: [jruby-user] Gem::Platform::Java Uninitialized Constant

Ok, guys, you have definitively answered my question now. I’ll use
“java”.

Thanks,
Keith

It seems like something on your system is introducing those constants.
Do
you have any pry plugins or a .pryrc file or anything?

$ rbenv version
2.0.0-rc2

$ pry
[1] pry(main)> cd Gem::Platform
[2] pry(Gem::Platform):1> ls
constants: CURRENT RUBY
Gem::Platform.methods: local match new
Gem::Platform#methods:
== =~ cpu= hash os to_a version
=== cpu eql? inspect os= to_s version=
instance variables: @local
locals: _ __ dir ex file in out pry
[3] pry(Gem::Platform):1>
[4] pry(main)>

$ rbenv version
jruby-1.7.4

$ pry
[1] pry(main)> cd Gem::Platform
[2] pry(Gem::Platform):1> ls
constants: CURRENT RUBY
Gem::Platform.methods: local match new
Gem::Platform#methods:
== =~ cpu empty? hash os to_a version
=== _deprecated_empty? cpu= eql? inspect os= to_s version=
instance variables: @local
locals: _ __ dir ex file in out pry
[3] pry(Gem::Platform):1>

$ rbenv version
1.9.3-p327

$ pry
[1] pry(main)> RUBY_VERSION
=> “1.9.3”
[2] pry(main)> cd Gem::Platform
[3] pry(Gem::Platform):1> ls
constants: CURRENT RUBY
Gem::Platform.methods: local match new
Gem::Platform#methods:
== =~ cpu empty? hash os to_a version
=== _deprecated_empty? cpu= eql? inspect os= to_s version=
instance variables: @local
locals: _ __ dir ex file in out pry
[4] pry(Gem::Platform):1>

There are techniques for doing what you want though. Check out How do I
make a gem that targets both MRI and
JRuby?http://stackoverflow.com/a/13540730/1200100for instance.