Question JRuby
Page 1 of 111 Replies - 587 Views - Last Post: 29 July 2011 - 07:16 PM
#1
Question JRuby
Posted 28 July 2011 - 12:59 PM
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?
Replies To: Question JRuby
#2
Re: Question JRuby
Posted 28 July 2011 - 01:33 PM
http://java.sun.com/...pting/jruby/#ej
This post has been edited by MitkOK: 28 July 2011 - 01:34 PM
#3
Re: Question JRuby
Posted 28 July 2011 - 01:38 PM
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
#4
Re: Question JRuby
Posted 28 July 2011 - 01:45 PM
# 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?
#5
Re: Question JRuby
Posted 29 July 2011 - 08:54 AM
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.
#6
Re: Question JRuby
Posted 29 July 2011 - 12:04 PM
sepp2k, on 29 July 2011 - 03:54 PM, said:
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?
#7
Re: Question JRuby
Posted 29 July 2011 - 06:22 PM
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.
#8
Re: Question JRuby
Posted 29 July 2011 - 06:25 PM
#9
Re: Question JRuby
Posted 29 July 2011 - 06:48 PM
TheCompBoy, on 30 July 2011 - 03:25 AM, said:
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.
#10
Re: Question JRuby
Posted 29 July 2011 - 06:53 PM
#11
Re: Question JRuby
Posted 29 July 2011 - 07:12 PM
TheCompBoy, on 30 July 2011 - 03:53 AM, said:
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.
#12
Re: Question JRuby
Posted 29 July 2011 - 07:16 PM
|
|

New Topic/Question
Reply



MultiQuote




|