|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
include_package strangenessI'm extending Java classes in Ruby, but there's one odd catch. I use include_package to pull the whole Java package into a Ruby module. The problem is that the classes are defined lazily, so if my extension is defined before the Java entity is referenced, rather than "extending" the java class, I hide it. Eg:
module Util include_package 'java.util' class ArrayList def baz size end end end l = Util::ArrayList.new l.add 5 # <- This fails with "undefined method `add' for #<Util::ArrayList:0x16b61c3>" Tim --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: include_package strangenessI'm not sure if that's what you want to do, but I think you have to declare the class to extend with the fully qualified name. This works for me, from jirb:
irb(main):022:0> module Util
irb(main):023:1> include_package 'java.util' irb(main):024:1> irb(main):025:1* class Java::JavaUtil::ArrayList irb(main):026:2> def baz irb(main):027:3> size
irb(main):028:3> end irb(main):029:2> end irb(main):030:1> end irb(main):033:0> l = java.util.ArrayList.new => #<Java::JavaUtil::ArrayList:0xe11a87 @java_object=#<Java::JavaObject:0x94cbe2>>
irb(main):034:0> l.add 5 => true irb(main):035:0> l.baz => 1 On Fri, Jul 17, 2009 at 10:05 PM, Timothy Hanson <thanson@...> wrote: I'm extending Java classes in Ruby, but there's one odd catch. I use include_package to pull the whole Java package into a Ruby module. The problem is that the classes are defined lazily, so if my extension is defined before the Java entity is referenced, rather than "extending" the java class, I hide it. Eg: -- David Calavera http://www.thinkincode.net |
|
|
Re: include_package strangenessSo, if you want to use your namespace you have to extend the class outside the namespace definition, or at least it's how I do it, this is an example:
http://pastie.org/549864
On Fri, Jul 17, 2009 at 10:24 PM, David Calavera <david.calavera@...> wrote: I'm not sure if that's what you want to do, but I think you have to declare the class to extend with the fully qualified name. This works for me, from jirb: -- David Calavera http://www.thinkincode.net |
|
|
RE: include_package strangenessWhat worked for me was to just reference the symbol in the
module before extending it. Tim From: David Calavera
[mailto:david.calavera@...] So, if you want to use your namespace you have to extend the
class outside the namespace definition, or at least it's how I do it, this is
an example: On Fri, Jul 17, 2009 at 10:24 PM, David Calavera <david.calavera@...> wrote: I'm not sure if that's what you want to do, but I think you
have to declare the class to extend with the fully qualified name. This works
for me, from jirb: irb(main):022:0> module Util irb(main):023:1> include_package 'java.util' irb(main):024:1> irb(main):025:1* class Java::JavaUtil::ArrayList irb(main):026:2> def baz irb(main):027:3> size irb(main):028:3> end irb(main):029:2> end irb(main):030:1> end irb(main):033:0> l = java.util.ArrayList.new
=> #<Java::JavaUtil::ArrayList:0xe11a87
@java_object=#<Java::JavaObject:0x94cbe2>> irb(main):034:0> l.add 5
=> true irb(main):035:0> l.baz
=> 1 On Fri, Jul 17, 2009 at 10:05 PM, Timothy Hanson <thanson@...>
wrote: I'm extending Java classes in
Ruby, but there's one odd catch. I use include_package to pull the whole Java
package into a Ruby module. The problem is that the classes are defined lazily,
so if my extension is defined before the Java entity is referenced, rather than
"extending" the java class, I hide it. Eg:
--
|
|
|
Re: include_package strangenessOn Fri, Jul 17, 2009 at 3:05 PM, Timothy
Hanson<thanson@...> wrote: > I'm extending Java classes in Ruby, but there's one odd catch. I use include_package to pull the whole Java package into a Ruby module. The problem is that the classes are defined lazily, so if my extension is defined before the Java entity is referenced, rather than "extending" the java class, I hide it. Eg: Yeah, this is a known limitation of include_package. Since it just installs a const_missing hook, it's subject to constant lookup behaviors. In this case, it does not fire the hook when you just open a class, because you could honestly just be defining a new class. Your workaround of referencing the classes first is a pretty good solution. We've also hoped that some day we could include a package indexing tool that eagerly defines the constants, but that has never happened. If someone wanted to try to integrate Jython's package indexer, the code is available here: http://code.google.com/p/jvm-language-runtime/source/browse/#svn/trunk/packagecache - Charlie --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: include_package strangenessSorry for joining the ancient thread now - just noticed it.
> referencing the classes first is a pretty good solution I think the best way to deal with this is the same as for extending classes that we know exist already: use class_eval. It can hit 2 birds with 1 stone: 1) should work safely with include_package, 2) in general, it will complain if you were "extending" the wrong class by mistake (which IMHO is a common annoyance). So: MyJavaClass.class_eval do def my_extension end end rather than: class MyJavaClass def my_extension end end Gergo 2009/7/21 Charles Oliver Nutter <headius@...>: > On Fri, Jul 17, 2009 at 3:05 PM, Timothy > Hanson<thanson@...> wrote: >> I'm extending Java classes in Ruby, but there's one odd catch. I use include_package to pull the whole Java package into a Ruby module. The problem is that the classes are defined lazily, so if my extension is defined before the Java entity is referenced, rather than "extending" the java class, I hide it. Eg: > > Yeah, this is a known limitation of include_package. Since it just > installs a const_missing hook, it's subject to constant lookup > behaviors. In this case, it does not fire the hook when you just open > a class, because you could honestly just be defining a new class. > > Your workaround of referencing the classes first is a pretty good > solution. We've also hoped that some day we could include a package > indexing tool that eagerly defines the constants, but that has never > happened. If someone wanted to try to integrate Jython's package > indexer, the code is available here: > > http://code.google.com/p/jvm-language-runtime/source/browse/#svn/trunk/packagecache > > - Charlie > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Free embeddable forum powered by Nabble | Forum Help |