Ten Things Every Java Programmer Should Know About Ruby

D

Dick Davies

* Zach Dennis said:
- ruby has a lower percentage of dr. diagnoses carpal tunnel sydrome
cases then java (ok, this is just an opinion...=)

Definitely in my case. I switced over a year into RSI and it makes a huge
difference.

I know a lot of Java coders who say 'use an IDE to generate code to save typing'
but i like to see what code is being written on my behalf. A tool to churn out
code smells like bandaid to a brokenly verbose language to me...

I prefer to use my keypresses wisely in solving my problem rather than nursing a
compiler.

REXML vs. JAXP. I rest my case.
 
J

Joao Pedrosa

Hi,

Definitely in my case. I switced over a year into RSI and it makes a huge
difference.

I know a lot of Java coders who say 'use an IDE to generate code to save typing'
but i like to see what code is being written on my behalf. A tool to churn out
code smells like bandaid to a brokenly verbose language to me...

I prefer to use my keypresses wisely in solving my problem rather than nursing a
compiler.

REXML vs. JAXP. I rest my case.

I too like to take it easy when using the keyboard and mouse. Ruby
does not push me to my limits. Brain + Ruby == less coding. Or not so
when we don't have our framworks ready. ;-)

Cheers,
Joao
 
M

Mohammad Khan

Edgardo Hames said:

I'm going to collect responses in a public Ta-Da list at
http://jimweirich.tadalist.com/lists/public/14055 (which is already way
over 10 items!). Later (possibly this weekend) I'll sort through the
collected suggestions, edit them down to my top ten and publish the
result.

Objection on # 2
Everything is *not* object.
By believing the quote 'in Ruby, Everything is object' already made my life hard. :p


Mohammad
 
Z

Zach Dennis

The list looks pretty good, but on the 4th up from the bottom there is a
typo:

you can use string interpolation, ex: "x: #{@myvar}" instead of having
to say "x:" myvar'

should be:

you can use string interpolation, ex: "x: #{@myvar}" instead of having
to say "x:" + myvar'

If you decide to use that one in your final list...

Zach
 
M

Mathieu Bouchard

All but variables are...

All but variables, blocks (which are not Procs), methods (which are not
Methods and not even UnboundMethods, which are both mere wrappers) and,
for all practical purposes, every single bit of code, because even though
Ruby stores all the method code as trees of objects ("AST"), those are not
exposed to the user, so they don't count.

Any other exceptions to the rule I might be missing ?

_____________________________________________________________________
Mathieu Bouchard -=- Montréal QC Canada -=- http://artengine.ca/matju
 
C

Csaba Henk

- ruby has multiple inheritance through mixins (this is sooo nice to have)

Maybe it should be explained better. By multiple inheritance people
usually mean something like

class C < A, B
....

But this is not valid ruby, and that may disappoint many, though
shouldn't.

Csaba
 
J

James Britt

PA said:
(5) Discipline. Because of its inherent flexibility, Ruby require more
self-discipline.

Wow. I have about zero self-discipline, yet Ruby makes my life easier.

Maybe there's just less arbitrary stuff to remember when using Ruby
rather than Java.


James
 
G

Glenn Vanderburg

In addition to the many great suggestions already made, here are two
more:

* Many things that you're used to thinking of as syntax are now just
code.

* For good (but subtle) reasons, you have to leave the '++' and '--'
operators behind. You can still use '+=' and '-=', though.

Although I understand the reasons for that last one, years of C, C++,
Perl, and Java still lead me to try to use them in Ruby every now and
then.

---Glenn
 
A

Austin Ziegler

Objection on # 2 Everything is *not* object. By believing the
quote 'in Ruby, Everything is object' already made my life hard.
:p

Everything *is* an object in Ruby. Variables are labels for objects;
they aren't anything that a programmer can access in any way --
which makes them irrelevant to the consideration.

-austin
 
S

Shashank Date

I found out quite by accident, that if you "include" the following Module:

module M; def a; puts "weird"; 1; end; end

then you can create some pretty weird expressions:

p :: M .==:): M::a )? :a: :: M


or

p ::M.==:):M::a)?:a: ::M::a.+(a)


Go ahead ... see for yourself what they evaluate too.

Just for fun !

-- shanko
 
M

Mohammad Khan

Everything *is* an object in Ruby. Variables are labels for objects;
they aren't anything that a programmer can access in any way --
which makes them irrelevant to the consideration.

same way, I can say...
Everything is *LABEL* in Ruby !!

Mohammad
 
N

Navindra Umanee

Zach Dennis said:
- you can use string interpolation, ex: "x: #{@myvar}" instead of
having to say "x:" + myvar'

Ruby seems to lose here. The syntax #{@myvar} is a rare case of
ugliness.
- ruby doesn't force you to have 1 file per public class, you can have
all the public classes you want in a file (not that you have to do this,
but it's a nice option to have)

On the other hand, if you *want* to put each class in a file, you end
up having to painfully "require" each file every time you want to
access a class. Java can automatically find classes in the current
package and load them. Or is just me? Is there a Ruby Way?

Cheers,
Navin.
 
N

Nicholas Van Weerdenburg

These are the concepts that got me interested, by leveraging my
Java/C++ heritage, my mild Perl experience, my Python experiment, and
the Smalltalk mystique that drifts back and forth across the computing
landscape...
1. you can fit in your mind and write code without looking at the docs
every six minutes.
2. less syntax and less typing
3. Fixed what's wrong with Perl
4. Fixes what's wrong with Python
5. It's super productive (like Perl, Python and Smalltalk)- maybe 5-10x Java.

This excited me, because I always wanted to learn Smalltalk...
= 6. Is a lot like Smalltalk, but doesn't look as funny

JavaScript gets some unfair knocks, so maybe best avoided...
+ 7. Is a lot like JavaScript, but more OO and more for ful app development

The following are cool when you know them, and in many ways the heart
of Ruby, but were meaningless to me until I banged my head against
them for a while. I think a lot of what is exciting about Ruby is
better learned then taught, much like Zen meditation. Just think what
marketing these concepts did for Smalltalk and Lisp- Java and C#.
* 8. Blocks and Closures
* 9. Open Classes
* 10. Duck Typing

Regards,
Nick
 
J

Joao Pedrosa

Hi,

Ruby seems to lose here. The syntax #{@myvar} is a rare case of
ugliness.

It's funny how we can disagree over such "simple" syntax. :) Fact is,
@is_not_a_method, and it has the escope explicit (always better then
implicit, right?), besides being only one character. Perfect, Matz,
thanks. :)
On the other hand, if you *want* to put each class in a file, you end
up having to painfully "require" each file every time you want to
access a class. Java can automatically find classes in the current
package and load them. Or is just me? Is there a Ruby Way?

Just create 1 method that loads all the files that you want in the
current directory or in the directory that you want (or in the
subdirectories, for that matter.) Rails uses such techniques to cut
the clutter, for example.
Cheers,
Navin.

Cheers,
Joao
 
Z

Zach Dennis

Navindra said:
Ruby seems to lose here. The syntax #{@myvar} is a rare case of
ugliness.

I don't know what the official ruling is, but when using variables you
can drop the {}.

@a = "here i go again"
puts "#@a"

@@a = ["here", " i", " go", " again" ]
puts "#@@a"

Also you have to way in heredoc support, which java dont got:

a = "interpolation"
b = "easier"
c = "You know what I'm saying"
str =<<END
i love string #{a}.
it makes my life #{b}.
#{c}
END
puts str

The larger area of text you have to write the nicer it gets in ruby and
the more it sucks in java to have to write " + ". Especially when you
have really long strings and are dealing with newlines, spaces and tabs.
I like to neatly format my source and have it be a max of 72 to 80
characters per line. I hate writing long text in java because I end up
having ugly source on 10-15 lines with a gazillion " + " and \n's that
is hard to visually parse out or I have 50 lines of + " ..." on it's
own line.

No matter what you say, I prefer doing interpolation over " + " anyday.
I've writtent 15 times the amount of java code in thepast few months for
work-related reasons then i have in ruby, you would think I'd prefer the
" + " way...

On the other hand, if you *want* to put each class in a file, you end
up having to painfully "require" each file every time you want to
access a class. Java can automatically find classes in the current
package and load them. Or is just me? Is there a Ruby Way?

This totally depends on how you use your code. Most likely you won't
need to access every file from it's src file. And if you do it is best
to design a loader class:

In main.rb you have:
require "gui/gui"
require "data/data"

- gui/gui.rb which loads all required files for module GUI
- data/data.rb which loads all required files for module DATA

in gui.rb you have:
require "layout"
require "fonts"
require "2dapi"
require "3dapi"

and similar in data/data.rb.

You don't have to require each individual file you just have to design
your program. I am not saying what you say wouldn't be nice, and someone
may already have a modification to *require* for this to work, but you
are not hindered right now in doing this, it just requires more
discipline on the design and access of your code.

And back to a java principle...alot java code are for one or two lines
of code that use ((MyClass)getGenericObject()).setThis( true );

In Ruby you don't have to say "require myclass" just to do that. You
just say:

get_obj.this = true

Zach
 
B

Bill Kelly

From: "Navindra Umanee said:
Ruby seems to lose here. The syntax #{@myvar} is a rare case of
ugliness.
=> "we're all out of powdered toast!!!"

:)

Also comes in " #$thing " flavor. :)


Regards,

Bill
 
J

Joao Pedrosa

Hi,

Hi,



It's funny how we can disagree over such "simple" syntax. :) Fact is,
@is_not_a_method, and it has the escope explicit (always better then
implicit, right?), besides being only one character. Perfect, Matz,
thanks. :)

Sorry to reply to myself, but I didn't think that you were complaining
of the interpolation, which is plenty useful when passing code around
to be evaluated (eval).

Regards,
Joao
 
N

Nicholas Van Weerdenburg

Java 1.5 adds printf functionality, saving some of the + absurdity.


Navindra said:
Ruby seems to lose here. The syntax #{@myvar} is a rare case of
ugliness.

I don't know what the official ruling is, but when using variables you
can drop the {}.

@a = "here i go again"
puts "#@a"

@@a = ["here", " i", " go", " again" ]
puts "#@@a"

Also you have to way in heredoc support, which java dont got:

a = "interpolation"
b = "easier"
c = "You know what I'm saying"
str =<<END
i love string #{a}.
it makes my life #{b}.
#{c}
END
puts str

The larger area of text you have to write the nicer it gets in ruby and
the more it sucks in java to have to write " + ". Especially when you
have really long strings and are dealing with newlines, spaces and tabs.
I like to neatly format my source and have it be a max of 72 to 80
characters per line. I hate writing long text in java because I end up
having ugly source on 10-15 lines with a gazillion " + " and \n's that
is hard to visually parse out or I have 50 lines of + " ..." on it's
own line.

No matter what you say, I prefer doing interpolation over " + " anyday.
I've writtent 15 times the amount of java code in thepast few months for
work-related reasons then i have in ruby, you would think I'd prefer the
" + " way...

On the other hand, if you *want* to put each class in a file, you end
up having to painfully "require" each file every time you want to
access a class. Java can automatically find classes in the current
package and load them. Or is just me? Is there a Ruby Way?

This totally depends on how you use your code. Most likely you won't
need to access every file from it's src file. And if you do it is best
to design a loader class:

In main.rb you have:
require "gui/gui"
require "data/data"

- gui/gui.rb which loads all required files for module GUI
- data/data.rb which loads all required files for module DATA

in gui.rb you have:
require "layout"
require "fonts"
require "2dapi"
require "3dapi"

and similar in data/data.rb.

You don't have to require each individual file you just have to design
your program. I am not saying what you say wouldn't be nice, and someone
may already have a modification to *require* for this to work, but you
are not hindered right now in doing this, it just requires more
discipline on the design and access of your code.

And back to a java principle...alot java code are for one or two lines
of code that use ((MyClass)getGenericObject()).setThis( true );

In Ruby you don't have to say "require myclass" just to do that. You
just say:

get_obj.this = true

Zach
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top