views:

95

answers:

3

I asked a question a few days ago about representing public static final and private static final fields in equivalent Ruby code. It got me thinking about what other syntax elements in Java might not translate directly to Ruby. Generics and Annotations come to mind. Anything else that would not translate well if you tried to port some Java code to Ruby?

A: 

I'm not positive on this one but I think double brace initialization would not.

e.g.

HashSet<String> codes = new HashSet<String>() {{
    add("XZ13s");
    add("AB21/X");
    add("YYLEX");
    add("AR2D");
}};
Mimisbrunnr
This is not considered good practice in Java.
Helper Method
No one said, good java practice. Just things that wouldn't translate well. I mostly use this in test code.
Mimisbrunnr
you can do codes = ["XZ123s", "AB21/X", "YYLEX", "AR2D"] in ruby.
Alex Rockwell
@Alex: While that would have similar effect (although not completely, as you are constructing an `Array`, while Mimisbrunnr is suggesting a `Set`), I understand the point of double brace as a syntactic sugar for not having to type object on which you are making a method call (in this case `add`). I think closer translation to Ruby would be `HashSet.new.instance_eval{add('XZ13s');add('AB21/X')...}`
Mladen Jablanović
I've been writing Java for years and have never seen double brace initialization!
Matthew
@There actually is NO double-brace initialization... it's basically a hack...
Helper Method
@Mladen You're right, the set version would be something like: Set.new ["XZ123s", "AB21/X", ...etc]. I'm not sure I agree that you're translation is closer to the Java code. Ruby has no need to try to emulate the syntactic sugar of double brace initialization. However, I do think its interesting that Ruby is such a flexible language that you were able to create a construct to mimic Java's weird syntactic sugar for initializing a collection like that. Thanks for pointing that out, I wouldn't have thought of it.
Alex Rockwell
A: 

Programming itself in Ruby is very different from Java (functional programming comming to mind). So it is not only about learning the syntactic differences, it's mainly about learning a different way to code.

If you program in Ruby like you would in Java, it will (probably) result in very bad code (even if you're a professional Java programmer).

Helper Method
A: 

With all due respect Ruby should be treated as a language that is different from Java. One should not try to find a one-one mapping between Java and Ruby. I once worked for a big database company who had migrated a lot of plsql developers to write Java based apps in 2000s when Java was the "cool" language. Result is till date they maintain code where a class has field variables like m_person_id, parameters like p_person_id and local variables like l_person_id.

The problem was this "mapping", while the prefixes p_ (for parameters), l_ (for local variables) or m_(field variables) served well in plsql with little or no IDE support, they made no sense in Java with IDEs like Eclipse where they can highlight stuff in different colours.

The example may be bad but trying to move from Ruby from Java is okay but learning Ruby by comparing it's corresponding feature in Java is not :)

Calm Storm