JRuby

`Include Java` causes #defined? to act unexpectedly

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Not A Bug
  • Affects Version/s: JRuby 1.4
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    0

Description

Encountered this bug while using Cucumber. RSpec does something similar to the below. When I `include Java` at the top level of a project I'm testing I get an error.

# commenting out the next line fixes the bug
include Java
 
module Foo
  unless defined?(Default)
    class Default
    end
  end
end
 
Foo::Default.new

Credit to Aslak Hellesoy for helping track down this bug.

Activity

Hide
Vladimir Sizikov added a comment -

Usually, include Java is not recommended. It is much better and cleaner to use require 'java'. That way, no problem with the supplied example.

Show
Vladimir Sizikov added a comment - Usually, include Java is not recommended. It is much better and cleaner to use require 'java'. That way, no problem with the supplied example.
Hide
Vladimir Sizikov added a comment -

So, here's what's going on. There is a special Default module for java classes with no packages, so 'include Java' brings Default module into life, and defined?(Default) returns true.

One more reason not to do include Java, since it pollutes the name space.

Show
Vladimir Sizikov added a comment - So, here's what's going on. There is a special Default module for java classes with no packages, so 'include Java' brings Default module into life, and defined?(Default) returns true. One more reason not to do include Java, since it pollutes the name space.
Hide
Hiro Asari added a comment -

As Vladimir mentioned, this is intended.

In general, "include M" adds names M::* in the current scope.

$ irb
irb(main):001:0> module M; def what?; p "foo"; end; end
=> nil
irb(main):002:0> what?
NoMethodError: undefined method `what?' for main:Object
	from (irb):2
	from :0
irb(main):003:0> include M
=> Object
irb(main):004:0> what?
"foo"
=> nil
Show
Hiro Asari added a comment - As Vladimir mentioned, this is intended. In general, "include M" adds names M::* in the current scope.
$ irb
irb(main):001:0> module M; def what?; p "foo"; end; end
=> nil
irb(main):002:0> what?
NoMethodError: undefined method `what?' for main:Object
	from (irb):2
	from :0
irb(main):003:0> include M
=> Object
irb(main):004:0> what?
"foo"
=> nil

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: