Java code to Ruby code

Hello guys,
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.

/*
class B {

public int foo() {
return B;
}
}
/*/

this doesn’t look right. What is the function foo supposed to return?

Other than that the code is not difficult to translate. Still need help?
:wink:

Andriy

IN : public int foo() {
return B;
}

What’ s B?? you mean B.class ?

Assuming it’s B.class , in Ruby :

class A
attr_reader :ref
def ml
puts ref.foo
end
end

class B
def foo
return self.class
end
end

A1 = A.new
B1 = B.new

A1.ref=B1
A1.ml

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs