[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Ten Things Every Java Programmer Should Know About Ruby

e

1/27/2005 9:55:00 PM

> Lähettäjä: "Jim Weirich" <jim@weirichhouse.org>
> 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.sourc...)
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://onest...

E



51 Answers

Ben Giddings

1/27/2005 10:38:00 PM

0

E S wrote:
> Here's the list so far:
> -------------------------------------------------------------------
> Ruby classes are Objects (therefore String.new, not new String())
> Everything is an Object

I'd emphasize this for Java ppl. Many of them think that Java is fully
object oriented, but they then have to deal with the problems if "int"
vs. Integer.

I'd make this fully clear to them by using one of the really cool things
like:

5.times { puts "Everything is an object!" }

> Everything is an expression.

A great example of this is:
language = case(name)
when "John", "Jane", "Mike"
"English"
when "Yukihiro", "Maiko"
"Japanese"
else
"Unknown"
end

> ri is your friend. irb is your other friend.

I'd say irb is your best friend. ri is cool and all, but nothing beats
being able to fire up irb and see for yourself what happens.

> method_missing

Show off the roman numerals example from the pickaxe for this one,
that's always impressive.

> ruby has shortcuts for accessor methods which reduces alot of redundant coding in java

And, ruby has no public members, so all access *must* be through
accessors. That's a major deal for Java ppl, who are used to having to
have a "foo" member variable, and then a getFoo() and setFoo() method.

Start with the default accessor, then if/when needed, add bounds
checking and whatever else you'd like.

Ben


Yohanes Santoso

1/28/2005 3:05:00 AM

0

E S <eero.saynatkari@kolumbus.fi> writes:

> Everything is an Object

I have never liked this over-generalised slogan. Without qualifying
it, then the sentence 'expression is an Object' would be true, yet
that is not true.

It should be qualified as: every object is an Object. The word
'object' there has two senses. The first and obvious one is instances
of Object or its descendants. The second meaning is data entity.

> * Ruby is pure OO.

I also have never liked this proclamation as everyone has their own
definitions of what OO is. This proclamation is worth not much more
than proclaiming the assembly language is 'pure OO'.


YS.





James Britt

1/28/2005 3:30:00 AM

0

Yohanes Santoso wrote:
> E S <eero.saynatkari@kolumbus.fi> writes:
>
>
>> Everything is an Object
>
>
> I have never liked this over-generalised slogan. Without qualifying
> it, then the sentence 'expression is an Object' would be true, yet
> that is not true.
>
> It should be qualified as: every object is an Object. The word
> 'object' there has two senses. The first and obvious one is instances
> of Object or its descendants. The second meaning is data entity.
>
>
>>* Ruby is pure OO.
>
>
> I also have never liked this proclamation as everyone has their own
> definitions of what OO is. This proclamation is worth not much more
> than proclaiming the assembly language is 'pure OO'.

Certain descriptions are more useful for catchy buzz phrases than
technical discourse. If you are addressing a group of developers,
Show, Don't Tell might be a good approach.


James


Bill Kelly

1/28/2005 4:20:00 AM

0

From: "Yohanes Santoso" <ysantoso-rubytalk@dessyku.is-a-geek.org>
> E S <eero.saynatkari@kolumbus.fi> writes:
>
> > Everything is an Object
>
> I have never liked this over-generalised slogan. Without qualifying
> it, then the sentence 'expression is an Object' would be true, yet
> that is not true.
>
> It should be qualified as: every object is an Object. The word
> 'object' there has two senses. The first and obvious one is instances
> of Object or its descendants. The second meaning is data entity.

While, "Everything is an Object" may be an exxageration,
I think there's enough truth to it for it not to be a
meaningless statement. In how many languages that
claim to be OO are all data types, even integers, floats,
and strings, Objects? And in how many are Classes themselves
first-class Objects? In my admittedly limited experience,
(Perl, Python, C++, Objective C, Java, Smalltalk), just one
language. Oh, and ruby. Two languages. :)

> > * Ruby is pure OO.
>
> I also have never liked this proclamation as everyone has their own
> definitions of what OO is. This proclamation is worth not much more
> than proclaiming the assembly language is 'pure OO'.

Clearly[1], a language is OO if Alan Kay says it is. :)
Since ruby borrows significantly from Smalltalk's object
model, there's a good chance ruby is OO, too. <grin>


[1] http://c2.com/cgi/wiki?HeInven...



Happy Rubying,

Regards,

Bill




Navindra Umanee

1/29/2005 5:14:00 PM

0

E S <eero.saynatkari@kolumbus.fi> wrote:
> * 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".

What is the point of strong typing then?

Thanks,
Navin.


Florian Gross

1/29/2005 6:00:00 PM

0

Navindra Umanee wrote:

>>* 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".
>
> What is the point of strong typing then?

Not having your Objects changing types all the time without you
noticing. If you forget to initialize a field in PHP it will be
converted to zero or an empty String if necessary thus shadowing your
bug. Strong typing usually is a requirement for languages where OOP
works well.

Petite Abeille

1/29/2005 6:07:00 PM

0


On Jan 29, 2005, at 18:13, Navindra Umanee wrote:

> What is the point of strong typing then?

Pig aPig = Pig.new()
Bird aBird = Bird.new()

aFlyingPig = (Bird) aPig

Doesn't make it so.

Cheers

--
PA, Onnay Equitursay
http://alt.text...



Navindra Umanee

1/29/2005 6:22:00 PM

0

Florian Gross <flgr@ccan.de> wrote:
> Not having your Objects changing types all the time without you
> noticing. If you forget to initialize a field in PHP it will be
> converted to zero or an empty String if necessary thus shadowing your
> bug. Strong typing usually is a requirement for languages where OOP
> works well.

In Ruby, objects pass for different types all the time. In my own
recent Ruby code I've mistakenly passed in the wrong types but didn't
notice because the types responded to the same methods.

Even though the type didn't change, effectively the problem and bug
shadowing was identical to the case you describe.

Cheers,
Navin.


Navindra Umanee

1/29/2005 6:28:00 PM

0

PA <petite.abeille@gmail.com> wrote:
> On Jan 29, 2005, at 18:13, Navindra Umanee wrote:
>
> > What is the point of strong typing then?
>
> Pig aPig = Pig.new()
> Bird aBird = Bird.new()
>
> aFlyingPig = (Bird) aPig
>
> Doesn't make it so.

This looks more like Java than Ruby... I don't see your point.
In Ruby you would get:

class Pig def what_am_i p "pig" end end
class Bird def what_am_i p "bird" end end

pig = Pig.new
bird = Bird.new

method_expecting_pig_but_given_bird(bird)

Makes it so.

Cheers,
Navin.


Petite Abeille

1/29/2005 6:41:00 PM

0


On Jan 29, 2005, at 19:27, Navindra Umanee wrote:

> This looks more like Java than Ruby... I don't see your point.

Sorry. Forgot to reboot in Ruby mode:

aMethodExceptingToFlyABirdButGivenAPigInstead( aPig )

And what would happen to this mythical flying pig now? aPig.fly()?
EXC_BAD_ACCESS (aka method_missing).

Cheers

--
PA, Onnay Equitursay
http://alt.text...