# Ruby vs Java (this,self/main)

**URL:** <https://www.ruby-forum.com/t/ruby-vs-java-this-self-main/108191>\
**Category:** Ruby\
**Created:** [June 29, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/ruby-vs-java-this-self-main/108191 "2007-06-29T21:39:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [June 29, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/ruby-vs-java-this-self-main/108191/1 "2007-06-29T21:39:15Z")

</div>

First, thank you for any help you can provide.

The issue:

The jruby version of this java app will not output callbacks in the  
running  
thread…or to the running command line process. The java version works,  
but  
the ruby version goes straight to the command line and waits for more  
user  
input instead of waiting and outputting server responses. The code will  
probably explain it better:

###### Java version

public class FeedSubscriberPublisher implements PFSubscriber {  
DFConn \_dfConn; // Connection to the establishment server  
(authentication etc)  
PFConn \_pfConn; // Connection to the feed event server  
public FeedSubscriberPublisher(DFConn df) {  
\_dfConn = df;  
\_pfConn = FSStarter.startConn(\_dfConn, this);  
\_pfConn.start();  
System.out.println(“Subscribing to event PFEventFeedUpdate…”);  
\_psConn.subscribe(“PFEventFeedUpdate”);  
}

```
public void newEvent(PFEvent event) {
        System.out.println("Feed Details: " + event);
}

public static void main(String args[]) {
    DFConn df = null;
    df = Establisher.connect(args, "FeedSubscriberPublisher");
    FeedSubscriberPublisher sub = new FeedSubscriberPublisher(df);
}

```

}

###### Ruby Version:

df = Java::Establisher.connect(user\_pass\_etc, “FeedSubscriberPublisher”)  
pfconn = Java::FSStarter.startConn(df, self)  
pfconn.start  
puts “Subscribing to Feed events”  
pfconn.subscribe(“PFEventFeedUpdate”)  
def newEvent(event)  
puts event  
end

The Ruby version breaks when providing ‘self’ as the ‘this’ equivalent  
when  
connecting(line two in the ruby script, here it is in IRB):

irb(main):007:0\> pfconn = Java:: FSStarter.startConn(df, self)  
NameError: no method ‘startConn’ with arguments matching [  
com.feed.service.DFConn, org.jruby.RubyObject]  
from  
/Users/enebo/jruby/releases/jruby-0\_9\_8/src/builtin/javasupport.rb:579:in  
`matching_method' from /Users/enebo/jruby/releases/jruby-0_9_8/src/builtin/javasupport.rb:126:in`startConn’  
from  
/Users/enebo/jruby/releases/jruby-0\_9\_8/src/builtin/javasupport.rb:85:in  
`startConn' from (irb):1:in`binding’

And when I don’t provide ‘self’ at all, it throws no errors however it  
never  
seems to wait for client input. I.e:

irb(main):009:0\> df = Java::Establisher.connect(user\_pass\_etc,  
“FeedSubscriberPublisher”)  
irb(main):010:0\> pfconn = Java::FSStarter.startConn(df)  
=\> #\<#Class:01x1f38c53:0x2cd1fc6 @java\_object=  
com.feed.service.DFConn@73cb02\>  
irb(main):011:0\> pfconn.start  
=\> nil  
irb(main):012:0\> pfconn.subscribe(“PFEventFeedUpdate”)  
=\> nil  
irb(main):013:0\> def newEvent(event)  
irb(main):014:1\> puts event.toString  
irb(main):015:1\> end  
=\> nil  
irb(main):016:0\> puts “Why can I type here? I want this thing to stop  
accepting input and start ouputting callbacks from the server!”  
Why can I type here? I want this thing to stop accepting input and start  
ouputting callbacks from the server!  
=\> nil  
irb(main):017:0\>

I’m not very familiar with java as you can probably tell. What gives?  
Surely  
it’s a simple fix I’m not grasping.

Thanks for any help or insight you could provide.

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [June 30, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/ruby-vs-java-this-self-main/108191/2 "2007-06-30T21:04:56Z")

</div>

list. rb wrote:

> The Ruby version breaks when providing ‘self’ as the ‘this’ equivalent when  
> connecting(line two in the ruby script, here it is in IRB):

I think the problem here is that the “self” you’re passing doesn’t  
implement whatever interfaces you need on the Java side.

Because we have to match the static types Java’s looking for, simply  
implementing the method isn’t enough; you need to give JRuby a chance to  
actually create a real Java type:

class MySubscriber  
include Java::PFSubscriber

def newEvent(event)  
puts event  
end  
end  
df = Java::Establisher.connect(user\_pass\_etc, “FeedSubscriberPublisher”)  
pfconn = Java::FSStarter.startConn(df, MySubscriber.new)  
pfconn.start  
puts “Subscribing to Feed events”  
pfconn.subscribe(“PFEventFeedUpdate”)

There’s also some shortcut syntaxes that are somewhat experimental, but  
make this process a little easier:

pfconn = Java::FSStarter.startConn(df, PFSubscriber.impl {|method,  
\*args| puts args[0]})

- Charlie

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [July 1, 2007, 6:10am UTC](https://www.ruby-forum.com/t/ruby-vs-java-this-self-main/108191/3 "2007-07-01T06:10:44Z")

</div>

Awesome Charles.

I had to make one change to your suggestion:  
class MySubscriber \< com.feed.service.PFSubscriber  
def newEvent(event)  
puts event  
end  
end

For some reason, the other syntax was throwing an error.

Thanks a million for the help

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [July 1, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/ruby-vs-java-this-self-main/108191/4 "2007-07-01T13:04:11Z")

</div>

list. rb wrote:

> Awesome Charles.
> 
> I had to make one change to your suggestion:  
> class MySubscriber \< com.feed.service.PFSubscriber  
> def newEvent(event)  
> puts event  
> end  
> end
> 
> For some reason, the other syntax was throwing an error.

If PFSubscriber is an interface, the above syntax doesn’t work in 1.0.  
What version of JRuby are you using?

If it’s not an interface, nevermind 🙂

- Charlie

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [July 1, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/ruby-vs-java-this-self-main/108191/5 "2007-07-01T15:31:25Z")

</div>

java-jruby0.9.8

The error I get is in javasupport:551 `method_missing`: undefined method  
`java_class` for NilClass (NoMethodError)

in StartConnection…

It sounds like I should upgrade then?

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [July 1, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/ruby-vs-java-this-self-main/108191/6 "2007-07-01T20:29:01Z")

</div>

list. rb wrote:

> java-jruby0.9.8
> 
> The error I get is in javasupport:551 `method_missing`: undefined method  
> `java_class` for NilClass (NoMethodError)
> 
> in StartConnection…
> 
> It sounds like I should upgrade then?

Yes, I’d recommend upgrading. You don’t want to be building apps on  
pre-1.0 due to the syntax changes that happened. They’re not major, but  
it would be unfortunate to have to go back and modify things.

- Charlie

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [July 1, 2007, 3:34pm UTC](https://www.ruby-forum.com/t/ruby-vs-java-this-self-main/108191/7 "2007-07-01T15:34:26Z")

</div>

And yes, it’s a public interface 🙂
