From: E S Date: 2005-01-28T06:54:54+09:00 Subject: Re: Ten Things Every Java Programmer Should Know About Ruby > L�hett�j�: "Jim Weirich" > Aihe: Ten Things Every Java Programmer Should Know About Ruby > > We will be introducing Ruby to our XP Users group in Cincinnati next week. > I thought it would be fun to create a list of "Ten Things Every Java > Programmer Should Know About Ruby" to help the transition. I've got a > number of things in my head, but would love to hear ideas from the mailing > list. > > So go ahead and submit your ideas. What things should a Java programmer > be aware of when starting out in Ruby? Here's the list so far: ------------------------------------------------------------------- Ruby classes are Objects (therefore String.new, not new String()) Everything is an Object Compared to Java, XML is agile. Compared to Ruby, XML is Heavy. Ruby does not have type casting. Don't worry about interfaces, enjoy Duck Typing. No method overloading Enjoy closures and blocks Don't worry about early performance optimization Web-development is possible with other languages besides Java. Ruby has O/R mappers, so find your Ruby "hibernate", but drop any preconceptions. Ruby has MVC and OO programming and libraries, but drop any preconceptions. Ruby is a language to be used everywhere. You use it even in templates. No need for "Velocity/JSP." Ruby is not a Silver Bullet, unlike Java, right? :-) In Ruby data is strongly typed, but variables are *not* you can have variable number of parameters, and multiple return values Once you start coding Ruby, going back to Java is painful. "." (dot) is a method call operator. "::" (colon-colon) is a scope operator. Java static methods do not (quite) translate to Ruby class methods. CamelCase for class names, names_with_underscores for methods and variables. Everything is an expression. That you can write Ruby in Java (http://jruby.sourceforge.net) stop writing so much code @local_variable, $global_variable, Constants, (and @@class_variables) ri is your friend. irb is your other friend. Reflection in Ruby is much easier than in Java, and more deeply into the language than the java.lang.reflect tack-on. eval the builtin classes are much faster because they're written in C and not Ruby Boolean methods end in ?. Dangerous methods end in ! Avoid external utility classes Use class methods to define pseudo-compile directives You probably don't need Factories Enumerable is your friend No external configuration files method_missing Typing is the enemy C extensions/wrappers are *much* easier in Ruby than JNI interfaces in Java Ruby classes are always "open". Singleton methods no semi-colons, optional parenthesis Ruby packaging vs Java packaging you can use string interpolation, ex: "x: #{@myvar}" instead of having to say "x:" myvar' ruby has multiple inheritance through mixins (this is sooo nice to have) ruby has shortcuts for accessor methods which reduces alot of redundant coding in java writing code in ruby, can improve the code you write in java ----------------------------------------------------------------- If there's one thing to be said it's that these are clearly items from current users and I'm afraid they're maybe too specific. To summarize some of these points: * Ruby is pure OO. Everything is an object. 'Hello' is an object, 1 is an object, Class is an object and so on. Ruby's OO is structured in such a way that it assists you in writing good OO code and allows you to think about the problem in a more natural manner. Multiple inheritance is allowed through mixins. * Ruby is dynamic. You can add, remove and modify objects, classes and methods at runtime, and that also applies to built-in and standard classes. Ever wanted to add a method to class String? Now you can. Ruby has extensive reflection capabilities. One could go as far as to say that Java doesn't have reflection. * Ruby is agile, perfectly suited for XP. It's easy to prototype in but surprisingly even such fast code ends up being easy to build on. Coding time decreases dramatically and bugs are infrequent. * Ruby is strongly typed, not statically typed. Each variable has a strict type that it represents, but a type declaration is not needed in e.g. method definitions. The result of this is that you don't treat and classify objects based on what their type is, you do it based on what that particular object can do. This concept is called 'duck typing': "If it walks like a duck and it quacks like a duck, it's a duck". * Ruby offers many, many syntactic enhancements to make your job easier. You write very concise, clear and elegant code without sacrificing functionality. Indeed, you gain some. One of the most favourite features of Ruby programmers are code blocks, which are often used in place of iterators. * Ruby is plenty fast for everyday applications. If, after you've written a program, you profile it and find a certain bottleneck, you can easily drop down to C to rewrite that portion. This is usually not necessary. * Ruby can be extended with C very easily. The API is clean and easy compared to JNI, Python or Perl. Extensions are usually used to bridge existing libraries to Ruby. When's this meeting, anyway? :) > -- Jim Weirich jim@weirichhouse.org http://onestepback.org E