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?
Question JRuby
Page 1 of 111 Replies - 573 Views - Last Post: 29 July 2011 - 07:16 PM
Replies To: Question JRuby
#2
Re: Question JRuby
Posted 28 July 2011 - 01:33 PM
https://github.com/j.../wiki/RedBridge
http://java.sun.com/...pting/jruby/#ej
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
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
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
Currect JRuby code:
If i now have a variable in my java code named "playerHealth" will it be given the number of 3?
# 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
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.
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:
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.
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
You create a method in jRuby the same way you create a method in regular ruby, i.e.:
So to create a method [il]player_health[//il], which returns 3, you'd write
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:
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.
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
Alright i wrote my method in Ruby but now is problem how i whant to call that method from java... Any ideas?
#9
Re: Question JRuby
Posted 29 July 2011 - 06:48 PM
TheCompBoy, 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.
#10
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?
#11
Re: Question JRuby
Posted 29 July 2011 - 07:12 PM
TheCompBoy, 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.
#12
Re: Question JRuby
Posted 29 July 2011 - 07:16 PM
Realy big thanks i would give you a plus reputation if i could!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|