Java code to Ruby code?

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.

Kostas L. wrote:

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.

On Tue, Aug 4, 2009 at 4:11 AM, Brian C.[email protected]
wrote:

Kostas L. wrote:

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

Read this first:
How To Ask Questions The Smart Way

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.)