Ten Things Every Java Programmer Should Know About Ruby

Hi,

···

On Fri, 28 Jan 2005 06:12:32 +0900, Dick Davies <rasputnik@hellooperator.net> wrote:

* Zach Dennis <zdennis@mktec.com> [0137 20:37]:
> - 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.

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. :wink:

Cheers,
Joao

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

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 ?

···

On Fri, 28 Jan 2005, Aredridel wrote:

On Fri, 2005-01-28 at 06:34 +0900, Mohammad Khan wrote:
> Objection on # 2
> Everything is *not* object.
> By believing the quote 'in Ruby, Everything is object' already made my life hard. :stuck_out_tongue:

All but variables are...

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

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

>> Edgardo Hames said:
>>> I would suggest that once you come up with the top ten list, it
>>> would be really nice if you did post it to the mailing list. I'd
>>> like to forward it to some friends.
>> I'm going to collect responses in a public Ta-Da list at
>> Feedback on "Ten Things a Java Programmer Should Know About Ruby" (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.
> :stuck_out_tongue:

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

···

On Fri, 2005-01-28 at 12:31 +0900, Austin Ziegler wrote:

On Fri, 28 Jan 2005 06:34:22 +0900, Mohammad Khan > <mkhan@lextranet.com> wrote:
> On Thu, 2005-01-27 at 14:36, Jim Weirich wrote:

-austin

Hi,

> - 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.

It's funny how we can disagree over such "simple" syntax. :slight_smile: 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. :slight_smile:

> - 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?

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

···

On Fri, 28 Jan 2005 13:35:42 +0900, Navindra Umanee <navindra@cs.mcgill.ca> wrote:

Zach Dennis <zdennis@mktec.com> wrote:

Navindra Umanee wrote:

- 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.

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...

- 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?

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

···

Zach Dennis <zdennis@mktec.com> wrote:

> - 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.

@thing = "powdered toast"

=> "powdered toast"

"we're all out of #@thing!!!"

=> "we're all out of powdered toast!!!"

:slight_smile:

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

Regards,

Bill

···

From: "Navindra Umanee" <navindra@cs.mcgill.ca>

Zach Dennis <zdennis@mktec.com> wrote:

Navindra Umanee wrote:

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?

autoload?

Hi,

···

On Fri, 28 Jan 2005 20:40:18 +0900, PA <petite.abeille@gmail.com> wrote:

On Jan 28, 2005, at 12:28, Joao Pedrosa wrote:

>> "Managing complexity"
>> Most software projects fail to meet their goals.
>> -- The Economist, Nov 25th 2004
>> Managing complexity
>
> Yeah, complexity kills projects. :slight_smile:

The only thing I wanted to "subtly" hint at is that implementation
languages are the least important factor in the ultimate success and/or
failure of a project.

In other words, an implementation language of any sort is not a silver
bullet of any sort.

After all, blaming or praising a language for your own success or
failure would be equivalent to a writer blaming its pen for "writer's
block".

Maybe you are right. I just wish that Sun had supported Ruby instead
of Java. That's all. :slight_smile:

Regards,
Joao

PA wrote:

In other words, an implementation language of any sort is not a silver bullet of any sort.

After all, blaming or praising a language for your own success or failure would be equivalent to a writer blaming its pen for "writer's block".

But each can have significant influence. If you are trying to write something out in longhand , and your pen weighs 700 pounds, it will take a little longer to complete, and you will be a little less likely to explore possibilities that don't show immediate payoff. You may also be less likely to go revise earlier work, and may be more willing to settle for a less-than-ideal end result.

James

Hi,

The only thing I wanted to "subtly" hint at is that implementation
languages are the least important factor in the ultimate success and/or
failure of a project.

In other words, an implementation language of any sort is not a silver
bullet of any sort.

After all, blaming or praising a language for your own success or
failure would be equivalent to a writer blaming its pen for "writer's
block".

Ok. What about the Domain Specific Languages? If they are any
important in getting work done, then we may distinguish languages on
appropriateness. All the Ruby features make it a DSL for certain kinds
of programs and libraries. What's more, the Ruby features allow you to
add to the language, making it even more Specific for certain Domains.
Closures, for example, are considered very useful when addind new
"language" constructs. As is the set of features of Ruby, because the
whole is comprised of every single part, for the whole without a part,
would not be the whole.

Keep the fun in programming. Keep using Ruby. :slight_smile:

Cheers,
Joao

> - 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.

Ugly for that case. Gorgeous for the #{myvar.upcase} case, I think.

···

On Fri, 2005-01-28 at 13:35 +0900, Navindra Umanee wrote:

Zach Dennis <zdennis@mktec.com> wrote:

Navindra Umanee <navindra@cs.mcgill.ca> writes:

···

Zach Dennis <zdennis@mktec.com> wrote:

  - 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.

I've become very fond of using:

     "x: %s" % @myvar

....or even

      "x: %s, y: %d" % [@myvar,
                        @myothervar]

--
Stig Sandbeck Mathisen <ssm@fnord.no> -+- http://fnord.no
  Trust the Computer, the Computer is your Friend

Hi,

···

On Fri, 28 Jan 2005 02:09:21 -0300, Joao Pedrosa <joaopedrosa@gmail.com> wrote:

Hi,

On Fri, 28 Jan 2005 13:35:42 +0900, Navindra Umanee > <navindra@cs.mcgill.ca> wrote:
> Zach Dennis <zdennis@mktec.com> wrote:
> > - 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.

It's funny how we can disagree over such "simple" syntax. :slight_smile: 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. :slight_smile:

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

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

···

On Fri, 28 Jan 2005 14:19:14 +0900, Zach Dennis <zdennis@mktec.com> wrote:

Navindra Umanee wrote:
> Zach Dennis <zdennis@mktec.com> wrote:
>
>> - 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.

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...

>> - 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?

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

--
Nicholas Van Weerdenburg

I have to say you've quite convinced me. Now that you mention it, I'm
myself quite sick of those long text string issues you bring up with
Java. It's nice that Ruby does interpolation similar to the way we're
used to with shell scripts.

And besides, I've found you can somewhat simulate the Java behaviour
anyway:

class String
   def -(arg)
      to_s + arg.to_s
   end
end

p "one: " - 1 - " two: " - 2

Thanks for the tips and insightful commentary to you and Joao. I'd be
interested in seeing the Rails code for recursive require.

Cheers,
Navin.

···

Zach Dennis <zdennis@mktec.com> wrote:

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...

Mathieu Bouchard wrote:

···

On Fri, 28 Jan 2005, Aredridel wrote:

On Fri, 2005-01-28 at 06:34 +0900, Mohammad Khan wrote:

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

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 ?

The ruby interpreter itself. (Maybe someday ruby will have multiple interpreters.)

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

All but variables are...

All but variables,

Variables are placeholders for objects. Though there may be a good point
here (since variables can refer to more than one object).

blocks (which are not Procs),

Blocks are objects. Even anonymous blocks, thought they cannot be
manipulated as such (they are anonymous, right?).

methods (which are not
Methods and not even UnboundMethods, which are both mere wrappers) and,

Methods are part of objects. So they do belong to the object-system.

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.

That's implementation specific, but the Ruby language doesn't specify
that, so it isn't strictly part of the language. For example a Ruby
program could be converted to a fpga, or will run in the future on some
weird hardware that we cannot imagine now.

Kristof

···

On Fri, 28 Jan 2005 07:54:42 +0900, Mathieu Bouchard wrote:

On Fri, 28 Jan 2005, Aredridel wrote:

On Fri, 2005-01-28 at 06:34 +0900, Mohammad Khan wrote:

James Britt wrote:

PA wrote:
>
> In other words, an implementation language of any sort is not a silver
> bullet of any sort.
>
> After all, blaming or praising a language for your own success or
> failure would be equivalent to a writer blaming its pen for "writer's
> block".

But each can have significant influence. If you are trying to write
something out in longhand , and your pen weighs 700 pounds, it will take
a little longer to complete, and you will be a little less likely to
explore possibilities that don't show immediate payoff. You may also be
less likely to go revise earlier work, and may be more willing to settle
for a less-than-ideal end result.

Excellent point and analogy -- I'm going to save this!

Curt