major differences and similarity in java and ruby?

Ruby over Java?


Ruby is a scripting language and it is interpreted at the run time by the Ruby interpreter , The Ruby code is interpreted and converted to machine level language i.e Assembly code . Talking about the platform Independence you can run ruby code in any of the the platform like Linux ,Windows or Mac if you have platform dependent Ruby Interpreter installed.Ruby is slower  than Java to run but faster to write, or read whereas Java takes too long to develop
Where as in Java , it is Compiled and converted to an intermediate byte class and this byte class is interpreted by platform dependent JVM (Java Virtual Machine ) .
In that way you can think you Ruby source file as byte class which can be run on any platform ,with one difference byte class is already compiled but ruby source file will be compiled at the Run time .
The Object Oriented feature in Ruby is actually very different compared to Java. In Ruby, everything is an object, including a primitive type (in Java) like integer. In Ruby, new is like a property instead of a keyword. So to instantiate an object you would do this in Ruby:
animal = Animal.new
Ruby is strong typing but also dynamic.Ruby enables you to do duck typing.Ruby’s answer to multiple inheritance is mixin (which is a language feature), where in Java you would implement many interfaces.Ruby has got block, where you would use anonymous class to achieve the same thing in Java. But IMHO Ruby block is more powerful.
Similarities
As with Java, in Ruby,…
Memory is managed for you via a garbage collector.
Objects are strongly typed.
There are public, private, and protected methods.
There are embedded doc tools (Ruby’s is called RDoc). The docs generated by rdoc look very similar to those generated by javadoc.
Differences
Unlike Java, in Ruby,…
You don’t need to compile your code. You just run it directly.
There are several different popular third-party GUI toolkits. Ruby users can try WxRuby, FXRuby, Ruby-GNOME2, or the bundled-in Ruby Tk for example.
You use the end keyword after defining things like classes, instead of having to put braces around blocks of code.
You have require instead of import.
All member variables are private. From the outside, you access everything via methods.
Parentheses in method calls are usually optional and often omitted.
Everything is an object, including numbers like 2 and 3.14159.
There’s no static type checking.
Variable names are just labels. They don’t have a type associated with them.
There are no type declarations. You just assign to new variable names as-needed and they just “spring up” (i.e. a = [1,2,3] rather than int[] a = {1,2,3};).
There’s no casting. Just call the methods. Your unit tests should tell you before you even run the code if you’re going to see an exception.
It’s foo = Foo.new( “hi”) instead of Foo foo = new Foo( “hi” ).
The constructor is always named “initialize” instead of the name of the class.
You have “mixin’s” instead of interfaces.
YAML tends to be favored over XML.
It’s nil instead of null.
== and equals() are handled differently in Ruby. Use == when you want to test equivalence in Ruby (equals() is Java). Use equal?() when you want to know if two objects are the same (== in Java).
simple data structure in Java
class MySimpleStructure{
int data1;
int data2;
MyOtherDataStructure m1;
}

MySimpleStructure s1 = new MySimpleStructure();
s1.data1 = 19;
s1.m1 = new MyOtherDataStructure();
Equivalent implementation in Ruby.
class MySimpleStructure
attr_accessor :data1, :data2, m1
end

s1 = MySimpleStructure.new
s1.data1 = 19
s1.m1 = MyOtherDataStructure.new

0 comments:

Post a Comment