Re: [jruby-user] overloading Java methods via a module

Eric West <[email protected]>
Newsgroups gmane.comp.lang.jruby.user
Message-ID <CANVBD_mWNq=UYu19d5xQnjjoWc5b_dHSokWKmd4rVYqnQJvJpw@mail.gmail.com>
including a module won’t override the methods in the class it is included
into. For example, I’ll run some code on MRI to explain this:

$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]

module Cool
  def lame
    "Dude, that is so lame."
  endend
class Hipster
  include Cool

  def lame
    "You've probably never heard of it. Because you're lame."
  endend

hipster = Hipster.new
hipster.lame # => "You've probably never heard of it. Because you're lame."

As you can see, despite including the module, it doesn’t override the
method #lame. Now what I can do is call #super, watch:

module Cool
  def lame
    "Dude, that is so lame."
  endend
class Hipster
  include Cool

  def lame
    "You've probably never heard of it. Because you're lame."
    super
  endend

hipster = Hipster.new
hipster.lame # => "Dude, that is so lame."

Including a module places that module into the class’s object heirarchy. So
it’s method’s don’t override the class’s instance methods. Instead, they
become super to those methods. One way to achieve something like the result
you are after is to extend the instance of the class with the module. Like
so:

module Cool
  def lame
    "Dude, that is so lame."
  endend
class Hipster
  def lame
    "You've probably never heard of it. Because you're lame."
  endend

hipster = Hipster.new.extend(Cool)
hipster.lame # => Dude, that is so lame.

Hope that explains what you are running into. This actually has nothing to
do with jruby and java, it is just how ruby works.
​


On Thu, Jul 17, 2014 at 5:54 PM, Mike Luu <[email protected]> wrote:

> I can’t seem to overload a Java class instance method via a module.
>
> ```
>     require 'java'
>     module A
>       def getComment
>         'via module'
>       end
>     end
>
>     Java::JavaNet::HttpCookie.send(:include, A)
>     Java::JavaNet::HttpCookie.new('k', 'v').getComment
> ```
>
> the last line returns the original java definition when I want it to
> return my overloaded version
>
> What does work is opening up the class…
>
> ```
>     require 'java'
>     class Java::JavaNet::HttpCookie
>       def getComment
>         'via class opening'
>       end
>     end
>     java.net.HttpCookie.new('k', 'v').getComment
> ```
>
> note: I’m using HttpCookie as an example  :)
>
> Thanks,
> Mike
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.