Re: Ruby vs Java (this,self/main)



list. rb wrote:
###### 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):

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

.



Relevant Pages

  • Ruby script to Module/Class refactor
    ... I have written my first live ruby script that actually performs useful ... # and append .csv extension to each. ... puts FileTest.readable?? ...
    (comp.lang.ruby)
  • Re: lowering rubys reach
    ... Don't get me wrong it should make it a lot easier to write extensions for ruby - especially those that are merely for speed gains. ... What I was think of was the introduction of language profiles which would trigger certain language features. ... would complain about variable a not being declared and would need to be change to include a forward declaration, ...
    (comp.lang.ruby)
  • Re: Suggestion: swap name of "puts" and "print" and rename "puts" to "put_s"
    ... character and "puts" doesn't. ... Because that's how it is in the C language: ... Either way, just make it consistent. ... One of the benefits I have found from using Ruby is that it actively ...
    (comp.lang.ruby)
  • Re: Mind giving a little help?
    ... but Ben showed you two techniques which will work. ... puts "Is this URL OK? ... going to be running this program via command promt, I'm not using Ruby ... People are much more likely to answer a short concise question like 'how do I read a file?' ...
    (comp.lang.ruby)
  • Idea: Ruby Tutorial For Newbies
    ... I am learning to program with ruby but I'm struggling to ... Many of the other books cover ruby 1.6. ... tutorials I've found either don't go far enough or spend too long ... It looks to me like the word puts is a command. ...
    (comp.lang.ruby)