What attracts me to Ruby

As a newbie moving over from Python, the recent posts
regarding Python vs Ruby has set me thinking. On the
surface Ruby and Python seem so similar, so why are
they fundamentally so different?

Then I remember the guiding principle of Python: There
is only one way to do something.

http://t2.technion.ac.il/~shlomif/humour/ways_to_do_it.html

This philosophy appealled to me in the beginning. I
started using Python because I felt so frustrated with
Perl. While I found Perl, with its philosophy of
’There is more than one way to do somehting, so
frustrating. Even though I had become quite skillful
at writing my own scripts for munging data, I still
couldn’t understand anybody elses. After a couple of
years casual useage I’m still no closer to
understanding anything from CSPAN.

Python seemed better for larger scripts, but I found
that I was soon having to use a pile of clumsy idioms,
such as deliberately using exceptions to exit loops.
The restrictions of Python have the same problem as
Java and VB (though not as bad). Things like having
to use while 1 … break annoyed me. Would it really
hurt to add ‘until’ to the syntax?

The remarkable thing about Ruby is that it takes
Perl’s
philosophy of having many ways to do something. I can
use a code block, I can use a for loop, I can use
small talk style iterators. The context variables
like $_ are available, just like Perl.

All of this should result in a hodge podge of a
language. Mixing all of these methods should result
in illegible code. Having to remember all of these
approaches should be a nightmare for the newcomer.

Somehow, it all just works. Everything works nicely.
After a few weeks and reading the book, I find my
scripts running as expected first time. I even find
myself mixing {}s with do…ends with yields without
really thinking about it. I had expected that I would
choose the method I liked best and use mainly that
method. To my surprice, it all comes naturally (is
this violating the principle of least suprise? :slight_smile:

Probably the most amazing thing was that with just two
weeks experience I was able to delve into the Standard
Library and understand the code. The stuff I didn’t
understand was down to OO plumbing, and not obscure
syntax. On top of this, my questions were patiantly
answered on this list within seconds.

The only other language I can understand the Standard
Libaray’s source for is Java. The problem there is
that while I can understand exactly what the code is
doing, I find it hard to grasp why. Getting an
overview from Java source is a nightmare.

With ruby, not only could I understand what the code
was doing, I could also see how the objects were all
interacting. I’ve never been able to do this before
in any language.

It seems to me that Ruby has a big, big future ahead
if my experiences are in any way typical.

···

Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://uk.messenger.yahoo.com/

“Ged Byrne” gedb01@yahoo.co.uk wrote in message
news:20030820084950.29906.qmail@web10807.mail.yahoo.com

Then I remember the guiding principle of Python: There
is only one way to do something.

The remarkable thing about Ruby is that it takes
Perl’s
philosophy of having many ways to do something. I can
use a code block, I can use a for loop, I can use
small talk style iterators. The context variables
like $_ are available, just like Perl.

Somehow, it all just works. Everything works nicely.

In Ruby there are many ways to do things because there are many problems.
But there is “The Ruby Way” which is not enforce by the language, but rather
encouraged by the language.

The Ruby Way means there is a natural way to do things given a specific
problem and as long as you try to express your code as natural as possible,
there really isn’t a mess of different idioms where you have to be a
specialist to understand it (well, you dive into some very complex aspects
of Ruby but that is mostly for fun or for writing library code to add new
advanced functionality).

As always in programming there are many ways to do things at the larger
design level. Ruby makes it easy to move source code through incarnations
starting with a with lines of code growing into a few functions growing into
a class then multiple classes and finally using observer patterns and what
not.

For small programs it is more natural just to write the code directly
without any functions. For larger programs you need more structure and
abstraction.

The ability to easily grow programs and the friendly syntax I think is what
appeals the most in Ruby.

The $ syntax is widely recognized to be a mistake - it helps Perlers
intially but it doesn’t bring anything to the table you couldn’t express
easily in a cleaner syntax. I think it is fair to say that it is considered
bad form to use $ in Ruby code, but then I guess many have just gotten used
to it.

Mikkel

Ged Byrne wrote:

[snip]

After a couple of years casual useage I’m still no closer to
understanding anything from CSPAN.

I often feel the same way when I watch British Parliament on BBC America. :slight_smile:

I suspect that how well people take to a language depends on what language
they have come from. If you were using C++/Java and then shown Python you
would probably become enraptured by it.

However as a Perl programmer Python didn’t seem such a great deal, it was just
too close to Perl for me. Ruby however had a large enough gap to make it very
interesting (much cleaner OO than Perl).

If you were into Python then again I think that Ruby is not all that much of a
leap unless the indentation really got on your tits :slight_smile:

Perhaps converts to Ruby will be easier to find in the C++/Java camp than in
the Perl/Python camp.

I’ve often wondered if there was a good alternative to using $1, $2, $3,
etc. to pull stuff out of a string that has been matched with a regular
expression. Ideas?

– Matt

···

On Wed, 20 Aug 2003, MikkelFJ wrote:

The $ syntax is widely recognized to be a mistake - it helps Perlers
intially but it doesn’t bring anything to the table you couldn’t express
easily in a cleaner syntax. I think it is fair to say that it is considered
bad form to use $ in Ruby code, but then I guess many have just gotten used
to it.


From my heart and from my hand, why don’t people understand my intention?

Interesting view. I am comfortable with Python, which I came to from
C++/Pascal (not Java) but am struggling with Ruby, and I /thought/ it
was because Ruby was too close to Perl (which I also struggle with).
Looks as if I’ll have to find another explanation!

···

On Wed, 20 Aug 2003 23:41:16 +0900, Peter Hickman peter@semantico.com wrote:

However as a Perl programmer Python didn’t seem such a great deal, it was just
too close to Perl for me. Ruby however had a large enough gap to make it very
interesting (much cleaner OO than Perl).

I’ve had the same question. It just seems really convenient to have
those there, but it doesn’t seem very ruby-like at the same time.

-Kurt

···

On Thu, Aug 21, 2003 at 03:02:22AM +0900, Matt Lawrence wrote:

On Wed, 20 Aug 2003, MikkelFJ wrote:

The $ syntax is widely recognized to be a mistake - it helps Perlers
intially but it doesn’t bring anything to the table you couldn’t express
easily in a cleaner syntax. I think it is fair to say that it is considered
bad form to use $ in Ruby code, but then I guess many have just gotten used
to it.

I’ve often wondered if there was a good alternative to using $1, $2, $3,
etc. to pull stuff out of a string that has been matched with a regular
expression. Ideas?

– Matt

From my heart and from my hand, why don’t people understand my intention?

======= End of Original Message =======<

···

On Wed, 20 Aug 2003, MikkelFJ wrote:

The $ syntax is widely recognized to be a mistake - it helps Perlers
intially but it doesn’t bring anything to the table you couldn’t express
easily in a cleaner syntax. I think it is fair to say that it is considered
bad form to use $ in Ruby code, but then I guess many have just gotten used
to it.

I’ve often wondered if there was a good alternative to using $1, $2, $3,
etc. to pull stuff out of a string that has been matched with a regular
expression. Ideas?

See bottom of

http://www.rubycentral.com/book/tut_stdtypes.html

(the ‘Object-Oriented Regular Expressions’ section)

Good day for overcoming obstacles. Try a steeplechase.
Rasputin :: Jack of All Trades - Master of Nuns

yes. the match method returns a MatchData object which stores this
information, as well as the text before, and the text after the match.
An example from the pickaxe book:

m = /(.)(.)(\d+)(\d)/.match(“THX1138.”)
m[0] » “HX1138”
m[1, 2] » [“H”, “X”]
m[1…3] » [“H”, “X”, “113”]
m[-3, 2] » [“X”, “113”]

other methods of the MatchData object are post_match, pre_match, length,
etc … you could see the book itself for details :slight_smile:
(http://www.rubycentral.com/book/ref_c_matchdata.html) …

but how do you get around the other dollar-variables? like $<, , $_ and
$! … i really hate global variables … i think they are unelegant. is
there an object oriented way to access them?

bye
RvB (Rene van Bevern) rvb@rvb.dyndns.org

···

On Thu, Aug 21, 2003 at 03:02:22AM +0900, Matt Lawrence wrote:

I’ve often wondered if there was a good alternative to using $1, $2, $3,
etc. to pull stuff out of a string that has been matched with a regular
expression. Ideas?

For example:

re = /(.)(.)((.).)/
match = re.match “abcd”

if match
p match[0]
p match[1]
p match[2]
p match[3]
p match[4]
end

Matt Lawrence wrote:

···

I’ve often wondered if there was a good alternative to using $1, $2, $3,
etc. to pull stuff out of a string that has been matched with a regular
expression. Ideas?

– Matt

As of 1.8, MatchData has the captures method as well:

% ruby -e 'p "abcd".match(/(.)(.)(.)(.)/).captures' 
["a", "b", "c", "d"]
% ruby -e 'a, b, c, d =  "abcd".match(/(.)(.)(.)(.)/).captures; puts a, b, c, d'
a
b
c
d
···

On Thu, 21 Aug 2003 03:37:20 +0900 Dan Doel djd15@po.cwru.edu wrote:

For example:

re = /(.)(.)((.).)/
match = re.match “abcd”

if match
p match[0]
p match[1]
p match[2]
p match[3]
p match[4]
end


Jim Hranicky, Senior SysAdmin UF/CISE Department |
E314D CSE Building Phone (352) 392-1499 |
jfh@cise.ufl.edu http://www.cise.ufl.edu/~jfh |


                      About politics:
                 Don't worry about results
               It's the thought that counts

I’ll try to add something worthwhile here, because it’s a thought that
has struck me often.

Many Pyhonistas (and perhaps others?) say Ruby is too Perlish for
their taste. What they generally mean, I think, is that Ruby has $x
variables, and some syntax similarities (if, while, … modifiers).
The judgement is true but:

  • superficial, because Ruby is deeply elegant, which shouldn’t be
    ignored among surface comparisons
  • irrelevant, because the “too-Perlish” fears (unreadable code,
    different ways of doing things) don’t materialise in practice; Ruby
    code tends to be elegant, readable, etc. because coders don’t abuse
    the language

On the contrary, I have always considered Python to be too Perlish for
me at a deeper level, because although it tries hard to do OO better
than Perl, it’s still wasn’t part of the original language design.
Its OO capabilities are a “feature” in the language, rather than the
wellspring of the language.

Python is known to have attracted mostly fristrated Perl programmers.
Why it didn’t grab me is that although I was a frustrated Perl
programmer, that’s only 10% of what I was programming-wise, and I
didn’t see enough difference to make it worth it to replace that 10%.

What attracted me to Ruby, then, was its elegance, which made me
believe I could use it for more than 10% of my programming, which
makes it more worth learning.

Cheers,
Gavin

···

On Thursday, August 21, 2003, 2:49:35 AM, Tim wrote:

On Wed, 20 Aug 2003 23:41:16 +0900, Peter Hickman > peter@semantico.com wrote:

However as a Perl programmer Python didn’t seem such a great deal, it was just
too close to Perl for me. Ruby however had a large enough gap to make it very
interesting (much cleaner OO than Perl).

Interesting view. I am comfortable with Python, which I came to from
C++/Pascal (not Java) but am struggling with Ruby, and I /thought/ it
was because Ruby was too close to Perl (which I also struggle with).
Looks as if I’ll have to find another explanation!

I try to minimize the usage of the Perl special variables in Ruby, too.
But sometimes they can’t be avoided. An idiom I use fairly often is
this:

case string
when /(\d+)/ then puts "Numeric: " + $1
when /([A-Za-z]+)/ then puts "Alphabetic: " + $~[1]
end

It doesn’t seem to be possible to get the return value of the === method
in a general way either. A cleaner solution without special variables
would require a syntax change for the case expression similar to that:

case string as match
when /(\d+)/ then puts "Numeric: " + match[1]
when /([A-Za-z]+)/ then puts "Alphabetic: " + match[1]
end

match would be assigned to === string if the keyword
“as” is used to allow postprocessing in the when clauses.

···

On 2003-08-21 03:37:20 +0900, Dan Doel wrote:

For example:

re = /(.)(.)((.).)/
match = re.match “abcd”

if match
p match[0]
p match[1]
p match[2]
p match[3]
p match[4]
end


Were it left to me to decide whether we should have government without
newspapers or newspapers without government, I should not hesitate for a
moment to prefer the latter. But I should mean that every man should
receive those papers and be capable of reading them.
– Thomas Jefferson

Many Pyhonistas (and perhaps others?) say Ruby is too Perlish for
their taste. What they generally mean, I think, is that Ruby has $x
variables, and some syntax similarities (if, while, … modifiers).
The judgement is true but:

  • superficial, because Ruby is deeply elegant, which shouldn’t be
    ignored among surface comparisons

Maybe, but when one is struggling to get to grips with a language it’s
the syntax that’s most evident, and until one gets past that one can’t
see the more profound issues.

  • irrelevant, because the “too-Perlish” fears (unreadable code,
    different ways of doing things) don’t materialise in practice; Ruby
    code tends to be elegant, readable, etc. because coders don’t abuse
    the language

Wan’t somebody here deprecating Python for it’s “one way of doing
things” a while ago? When I saw my first Python program I understood
perfectly well what it did, but when I saw my first Ruby program I
couldn’t make sense of it. Obviously, that’s a function of my
personal history (and in particular, I still believe, that my personal
history doesn’t include Perl), and readability is bound to improve
when one learns the language!

On the contrary, I have always considered Python to be too Perlish for
me at a deeper level, because although it tries hard to do OO better
than Perl, it’s still wasn’t part of the original language design.
Its OO capabilities are a “feature” in the language, rather than the
wellspring of the language.

That’s another reason I like Python (and prefer C++ to Java, FWIW) –
I don’t believe object orientation is the answer to all problems, and
sometimes it just gets in the way. Python (and C++) give me the
choice, Ruby (and Java) don’t. Now I’m getting /really/ confused
which language has “more than one way to do it” and which has “only
one way to do it” :slight_smile:

What attracted me to Ruby, then, was its elegance, which made me
believe I could use it for more than 10% of my programming, which
makes it more worth learning.

It’s curious. By all logic, I don’t think Python should work. But my
productivity is higher – vastly higher – with Python than it has
been with anything else. By the same logic, Ruby ought to be great,
but I find myself permanently stuck at first base struggling to get
the simplest stuff to work. I wish I understood why – I could design
a /really/ good language if I did :wink:

···

On Thu, 21 Aug 2003 07:15:28 +0900, Gavin Sinclair gsinclair@soyabean.com.au wrote:

Tim Rowe wrote:

Wan’t somebody here deprecating Python for it’s “one way of doing
things” a while ago? When I saw my first Python program I understood
perfectly well what it did, but when I saw my first Ruby program I
couldn’t make sense of it. Obviously, that’s a function of my
personal history (and in particular, I still believe, that my personal
history doesn’t include Perl), and readability is bound to improve
when one learns the language!

Interesting. Were the programs you first saw of equivalent complexity?
Was the Ruby one using some of the odd advanced features like
instance_eval, class << self, or something similar?

That’s another reason I like Python (and prefer C++ to Java, FWIW) –
I don’t believe object orientation is the answer to all problems, and
sometimes it just gets in the way. Python (and C++) give me the
choice, Ruby (and Java) don’t. Now I’m getting /really/ confused
which language has “more than one way to do it” and which has “only
one way to do it” :slight_smile:

This is another thing that seems strange to me. Ruby’s object
orientation has never seemed intrusive to me. You can write anything as
if it were a non-OO language, and things just live in the “main”
namespace. I can’t think of any ways in which Ruby’s OO nature made it
more difficult to do something. Can you think of a quick example?

My problem with Python was when I wanted to make things seem OO, the
language got in my way. I was always faced with lots of underscores,
and having to pass “self” into methods. In the end, I think it was
underscores that drove me away from Python.

Ben

This is really interesting because my experience is exactly the
opposite. I tried to learn Python three times, but never got past the
really trivial stuff to get productive. It was during my third attempt
that I saw Dave Thomas post about Ruby. I downloaded Ruby and was using
it productively within a day or so.

Now my background did include Perl (as well as Java, C++, Eiffel and a
little Smalltalk), and I was delibrately looking for a Perl replacment,
something with better OO facilities. I suspect it was a combination of
the Perl and Smalltalk background that prepared me for Ruby.

What in Ruby do you find yourself struggling with?

···

On Thu, 2003-08-21 at 13:34, Tim Rowe wrote:

On Thu, 21 Aug 2003 07:15:28 +0900, Gavin Sinclair > gsinclair@soyabean.com.au wrote:

Many Pyhonistas (and perhaps others?) say Ruby is too Perlish for
their taste. What they generally mean, I think, is that Ruby has $x
variables, and some syntax similarities (if, while, … modifiers).
The judgement is true but:

  • superficial, because Ruby is deeply elegant, which shouldn’t be
    ignored among surface comparisons

Maybe, but when one is struggling to get to grips with a language it’s
the syntax that’s most evident, and until one gets past that one can’t
see the more profound issues.

  • irrelevant, because the “too-Perlish” fears (unreadable code,
    different ways of doing things) don’t materialise in practice; Ruby
    code tends to be elegant, readable, etc. because coders don’t abuse
    the language

Wan’t somebody here deprecating Python for it’s “one way of doing
things” a while ago? When I saw my first Python program I understood
perfectly well what it did, but when I saw my first Ruby program I
couldn’t make sense of it. Obviously, that’s a function of my
personal history (and in particular, I still believe, that my personal
history doesn’t include Perl), and readability is bound to improve
when one learns the language!

On the contrary, I have always considered Python to be too Perlish for
me at a deeper level, because although it tries hard to do OO better
than Perl, it’s still wasn’t part of the original language design.
Its OO capabilities are a “feature” in the language, rather than the
wellspring of the language.

That’s another reason I like Python (and prefer C++ to Java, FWIW) –
I don’t believe object orientation is the answer to all problems, and
sometimes it just gets in the way. Python (and C++) give me the
choice, Ruby (and Java) don’t. Now I’m getting /really/ confused
which language has “more than one way to do it” and which has “only
one way to do it” :slight_smile:

What attracted me to Ruby, then, was its elegance, which made me
believe I could use it for more than 10% of my programming, which
makes it more worth learning.

It’s curious. By all logic, I don’t think Python should work. But my
productivity is higher – vastly higher – with Python than it has
been with anything else. By the same logic, Ruby ought to be great,
but I find myself permanently stuck at first base struggling to get
the simplest stuff to work.


– Jim Weirich jweirich@one.net http://onestepback.org

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

Jim Weirich jweirich@one.net writes:

Now my background did include Perl (as well as Java, C++, Eiffel and a
little Smalltalk), and I was delibrately looking for a Perl replacment,
something with better OO facilities. I suspect it was a combination of
the Perl and Smalltalk background that prepared me for Ruby.

Same story here (struggled with Python to no avail then become quickly
productive with Ruby), but with no Perl nor Smalltalk background, just
some Delphi.

“Ben Giddings” ben@thingmagic.com wrote in message
news:3F450EEE.5070301@thingmagic.com

This is another thing that seems strange to me. Ruby’s object
orientation has never seemed intrusive to me. You can write anything as
if it were a non-OO language, and things just live in the “main”
namespace. I can’t think of any ways in which Ruby’s OO nature made it
more difficult to do something. Can you think of a quick example?

You can’t pass a reference to function around - not without involving less
elegant Proc objects or similar.

Also I really would like delegates in Ruby - passing a reference to a method
such that it knows the class it belongs to.
This is highly useful. I know it can be implemented (an old discussion), but
it would be very nice to have in the core language.

Mikkel

Yes, and it wasn’t the advanced features that got me, it was the @s,
@@s, :s and so on that looked like line noise to me. They’re not
exactly self evident!

···

On Fri, 22 Aug 2003 03:27:00 +0900, Ben Giddings ben@thingmagic.com wrote:

Interesting. Were the programs you first saw of equivalent complexity?

I wish I could pin it down. Memorability, I think. I read how to do
something, and the next day when I want to do it I find I need to look
it up again.

···

On Fri, 22 Aug 2003 10:52:19 +0900, Jim Weirich jweirich@one.net wrote:

What in Ruby do you find yourself struggling with?