# Java code to Ruby code?

**URL:** <https://www.ruby-forum.com/t/java-code-to-ruby-code/173168>\
**Category:** Ruby\
**Created:** [August 4, 2009, 9:55am UTC](https://www.ruby-forum.com/t/java-code-to-ruby-code/173168 "2009-08-04T09:55:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 4, 2009, 9:55am UTC](https://www.ruby-forum.com/t/java-code-to-ruby-code/173168/1 "2009-08-04T09:55:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 4, 2009, 10:11am UTC](https://www.ruby-forum.com/t/java-code-to-ruby-code/173168/2 "2009-08-04T10:11:55Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 4, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/java-code-to-ruby-code/173168/3 "2009-08-04T17:22:55Z")

</div>

On Tue, Aug 4, 2009 at 4:11 AM, Brian C.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://www.catb.org/~esr/faqs/smart-questions.html#intro)

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