11 Replies - 764 Views - Last Post: 29 July 2011 - 07:16 PM Rate Topic: -----

#1 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Question JRuby

Posted 28 July 2011 - 12:59 PM

Hello guys!

The problem am having is when i whant to add the script i made into my java code.. I have a variable inside my script which holds and number (Int variable) and i whant my java code to get this number which is inside the variable and put that number in a variable in the java code.

Is this posible?
Is This A Good Question/Topic? 0
  • +

Replies To: Question JRuby

#2 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Question JRuby

Posted 28 July 2011 - 01:33 PM

https://github.com/j.../wiki/RedBridge

http://java.sun.com/...pting/jruby/#ej

This post has been edited by MitkOK: 28 July 2011 - 01:34 PM

Was This Post Helpful? 1
  • +
  • -

#3 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Question JRuby

Posted 28 July 2011 - 01:38 PM

This doesn't help me too much.

EDIT: Its my first day i even installed and take a look on Ruby but i realy need to fix this.. Its important issue

This post has been edited by TheCompBoy: 28 July 2011 - 01:39 PM

Was This Post Helpful? -1
  • +
  • -

#4 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Question JRuby

Posted 28 July 2011 - 01:45 PM

Currect JRuby code:
# playerHealth.rb  
=begin  
 Ruby Numbers  
 Usual operators:  
 + addition  
 - subtraction  
 * multiplication  
 / division  
=end

require 'java'

puts "Set player health here"
playerHealth = 3 # Setting player health here!


If i now have a variable in my java code named "playerHealth" will it be given the number of 3?
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,179
  • Joined: 21-June 11

Re: Question JRuby

Posted 29 July 2011 - 08:54 AM

You can't access local variables defined in one file from another file. Not from ruby and not from java. You can also not change local variables defined in one of your java methods from outside that method.

So if you have a local variable playerHealth in one of your java methods, there is no way to change it from ruby (or from another java method). If you want to set the value of a local variable using your ruby code, you should put your ruby code in a method, which returns the health as its result, and then set the java local variable to the result of calling that method.

If you have an instance variable playerHealth on one of your java objects, you can pass that object into a ruby method, and that ruby method can then set the variable on the object.

Either way you should put your ruby code into a method, which you can then call from java.
Was This Post Helpful? 0
  • +
  • -

#6 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Question JRuby

Posted 29 July 2011 - 12:04 PM

View Postsepp2k, on 29 July 2011 - 03:54 PM, said:

You can't access local variables defined in one file from another file. Not from ruby and not from java. You can also not change local variables defined in one of your java methods from outside that method.

So if you have a local variable playerHealth in one of your java methods, there is no way to change it from ruby (or from another java method). If you want to set the value of a local variable using your ruby code, you should put your ruby code in a method, which returns the health as its result, and then set the java local variable to the result of calling that method.

If you have an instance variable playerHealth on one of your java objects, you can pass that object into a ruby method, and that ruby method can then set the variable on the object.

Either way you should put your ruby code into a method, which you can then call from java.


Okay first its realy great post thanks!
My variable is not in a method its in the class its defined with the class,
But i like the idea of creating a method in JRuby and call in in java.. But how would i do that? Could you maybe give me example in ruby code or give me link to where i can read about it?
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,179
  • Joined: 21-June 11

Re: Question JRuby

Posted 29 July 2011 - 06:22 PM

You create a method in jRuby the same way you create a method in regular ruby, i.e.:

def method_name(arguments)
  ...
end



So to create a method [il]player_health[//il], which returns 3, you'd write

def player_health
  3
end



And if you instead want to write a method which calls the setter method `setPlayerHealth` to set the instance variable `playerHealth` to 3 on some Java object, you'd write:

def set_player_health(object)
  object.setPlayerHealth(3)
end



Again this is the exact same code you'd write if you were trying to call a method named `setPlayerHealth` on a ruby object, so the use of jRuby is perfectly transparent here.
Was This Post Helpful? 0
  • +
  • -

#8 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Question JRuby

Posted 29 July 2011 - 06:25 PM

Alright i wrote my method in Ruby but now is problem how i whant to call that method from java... Any ideas?
Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,179
  • Joined: 21-June 11

Re: Question JRuby

Posted 29 July 2011 - 06:48 PM

View PostTheCompBoy, on 30 July 2011 - 03:25 AM, said:

Alright i wrote my method in Ruby but now is problem how i whant to call that method from java... Any ideas?


Both of MitkOK's links should adequately describe the process of calling ruby methods from Java using jRuby - and if they don't, at the very least they should lead to a more specific question than "any ideas?".

...

But now that I actually read the first link, it turns out I was actually wrong about there being no way to access local variables defined in a ruby file from Java (even though there is indeed no way to do so from regular ruby). That being said, putting the code into methods is still perfectly good advice, so you might as well stick with that.
Was This Post Helpful? 1
  • +
  • -

#10 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Question JRuby

Posted 29 July 2011 - 06:53 PM

Yea but i already looked through those two links but i didn't find anything about calling the methods.. I've checked through RedBridge maybe 3-4 times without finding it.. Please what chapter or part of RedBridge is about calling the Ruby methods?
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,179
  • Joined: 21-June 11

Re: Question JRuby

Posted 29 July 2011 - 07:12 PM

View PostTheCompBoy, on 30 July 2011 - 03:53 AM, said:

Yea but i already looked through those two links but i didn't find anything about calling the methods.. I've checked through RedBridge maybe 3-4 times without finding it.. Please what chapter or part of RedBridge is about calling the Ruby methods?


Ok, now that I try to point you to a specific section, it's really not that clear unless you read between the lines. Anyway, to call a ruby method from java, you first execute the file containing the method with engine.eval(script); (where script is the file containing your ruby code) and then engine.eval("my_method()") to call my_method (and get the result).

For a more elaborate example, you can see this wiki entriy.
Was This Post Helpful? 1
  • +
  • -

#12 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Question JRuby

Posted 29 July 2011 - 07:16 PM

Realy big thanks i would give you a plus reputation if i could!
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1