Java code to Ruby code??

K

Kostas L.

Hello guys,
I dont know if it is the right place to ask about java but i have a
problem.
I am trying to "translate" some java code to ruby code for a project
in my univercity. My professor gave me this java code an i want to
give him the same sunctionality in ruby.

Here is the code:

//Classes A and B
class A {
private B ref;

public void m1(){
System.out.println(ref.foo());
}

public void setRef(B aB){
ref = aB;
}
}

class B {

public int foo() {
return B;
}
}

//Creating the objects
A A1 = new A();
B B1 = new B();
A1.setRef(B1);
A1.m1();

This code is creating a connection between the two classes A and B.

I am a little confused about how to write this to ruby.
Any help would be useful!

Thanx in advance
Kostas L.
 
B

Brian Candler

Kostas said:
I am a little confused about how to write this to ruby.
Any help would be useful!

Read this first:
http://www.catb.org/~esr/faqs/smart-questions.html#intro

Basically what you need to do is:

1. Start writing some Ruby code
2. Keep going until you get stuck - e.g. you don't know how to do step
X, or your program crashes and you don't understand why
3. Use google to solve your problem
4. If you cannot find the solution using google, post the relevant parts
of your code so far, preferably in the form of a short standalone
program which is runnable by someone else which demonstrates the problem
5. Ask a specific question about that code

Regards,

Brian.
 
B

brabuhr


You might also try writing a test case to help you figure out what the
expected behaviour should be and work towards it:
cat KostasL.rb
require 'test/unit'

###/ Code Below \###

class A; end

class B; end

###\ Code Above /###

class KostasLTest < Test::Unit::TestCase
def test_001
a = A.new
b = B.new

a.set_reference(b)
result = a.method1
expected = "???"

assert_equal(expected, result)
end
end
ruby KostasL.rb
Loaded suite KostasL
Started
E
Finished in 0.001412 seconds.

1) Error:
test_001(KostasLTest):
NoMethodError: undefined method `set_reference' for #<A:0xb7c37550>
KostasL.rb:16:in `test_001'

1 tests, 0 assertions, 0 failures, 1 errors

(So, an obvious next step would be to define a set_reference method in
class A; etc.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top