in search of a compelling reason to use ruby....

R

Ryan Paul

i'm a python programmer, and I have recently been hearing a lot about
ruby. A lot of python people seem to think ruby is in some way inferior,
but I have yet to find any significant differences between the languages.
The only differences I have found so far have been superficial syntactic
things. The only real significant difference I have detected, is the
absence of a module system in ruby.

I would like to be able to make an objective and educated comparison. Ruby
seems to have a relatively small, but intensely devoted following.
Software that inspires such fervent support from a small community usually
possesses tremendous power that most people are just too dense to discern
(eg: ocaml, and BeOS). Consequently, I figured it would be worth my time
to find out what it is that the fanatics find appealing.

I am looking for:
- examples of tangible features that ruby has, which python doesnt.
- examples of scenarios where some facet of, or property specific to
ruby contributed to the simplification or improvement of a programming
project.
- facets of the language which may not necessarily be useful, but are
notably creative/innovative.

Additionally, I realize that there is more to a language than its syntax
and functionality. As a result, I am also interested in hearing about
facets of the ruby community and ruby ideology, and anything else that
ruby programmers feel contribute to their affinity for the language.

I have a feeling that most of the things that make ruby worth using can
also be found in python. From the perspective a python programmer who has
taken only a precursory glance at ruby, it looks like ruby suggests a
functional style, while python suggests an imperative style. It also looks
like ruby may be better suited for quick shell scripts, whereas python is
better suited for bigger programs. I realize that I probably suffer from
misconceptions, and i'm looking forward to having them corrected.

I sincerely hope nobody finds any of my comments offensive, I dont mean
them as criticisms of ruby. I intentionally withhold criticisms until I am
better versed in the benefits!

Thanks in advance!
-- SegPhault
 
G

Gavin Sinclair

I am looking for:
- examples of tangible features that ruby has, which python doesnt.
- examples of scenarios where some facet of, or property specific to
ruby contributed to the simplification or improvement of a programming
project.
- facets of the language which may not necessarily be useful, but are
notably creative/innovative.


I suggest you take a look at http://phrogz.net/ProgrammingRuby. It's
a high-quality dead-tree textbook that's freely readable online. It
assumes some decent background knowledge, which you obviously have.
The preface and the first couple of chapters will tell you most of
what you want to know, and from there you can pick out more advanced
things if you wish.

I certainly welcome discussions about particular features, but direct
Python/Ruby comparisons are very common on this list and almost always
generate more heat than light. That's despite the best of intentions.
The reality is that it's hard to think outside your own
experience/background. There's no quick answer to your questions, as
I have found in the past. For the curious, learning Ruby is extremely
worthwhile, even if you never write a line of code in it. You *will*
learn some novel approaches, no matter what your background, as Ruby
draws from many languages.

But like I say, there's no shortcut. If you spend an hour or so
reading the material in "Programming Ruby" (known fondly as "The
Pickaxe"), then you will understand much better where Ruby is coming
from and will (hopefully) start to appreciate some of its distinctive
features.

All the best,
Gavin
 
G

gabriele renzi

il Mon, 10 May 2004 10:42:02 GMT, Ryan Paul <[email protected]>
ha scritto::

<snip>

you better look at the *many times* debated stuff:
http://www.ruby-doc.org/RubyEyeForThePythonGuy.html
I believe with come patience you may get better and more complete
answer to anything by looking at old threads.

I am looking for:
- examples of tangible features that ruby has, which python doesnt.

I believe the strongest one (and totally incompatible with python's
__call__ methods) is the usage of methods as syntax enhancer[1]

Say, when you type:

class Foo
private
def foo
end
end

you're setting a state calling the private() method, not actually
using a keyword. This, toghether with blocks, allows
you to write stuff like this:

irb(main):001:0> def with(obj,&block)
irb(main):002:1> obj.instance_eval(&block)
irb(main):003:1> end
=> nil
irb(main):004:0> with 10 do
irb(main):005:1* print id
irb(main):006:1> end
21=> nil
irb(main):007:0> 10.id
=> 21


but, in the end, ruby and python evolved toward common features via
different approaches (i.e. python property() is similar in effects to
ruby accessor methods, even if completely different)

And one thing that I don't see spotted in the oldest thread is ruby's
SAFE levels (controlling stuff like tainted input, FS access, and so
on)
I sincerely hope nobody finds any of my comments offensive, I dont mean
them as criticisms of ruby. I intentionally withhold criticisms until I am
better versed in the benefits!

you've been kind enough to still be far from flaming ;)

[1]
I can't say we define new syntax in ruby, but it looks like we do :)
 
P

Phil Tomson

i'm a python programmer, and I have recently been hearing a lot about
ruby.
Good.

A lot of python people seem to think ruby is in some way inferior,
but I have yet to find any significant differences between the languages.
The only differences I have found so far have been superficial syntactic
things. The only real significant difference I have detected, is the
absence of a module system in ruby.

?? absence of a module system in ruby? What do you mean by that? We've
got modules.
I would like to be able to make an objective and educated comparison. Ruby
seems to have a relatively small, but intensely devoted following.
Software that inspires such fervent support from a small community usually
possesses tremendous power that most people are just too dense to discern
(eg: ocaml, and BeOS). Consequently, I figured it would be worth my time
to find out what it is that the fanatics find appealing.

I am looking for:
- examples of tangible features that ruby has, which python doesnt.

Continuations, blocks.
- examples of scenarios where some facet of, or property specific to
ruby contributed to the simplification or improvement of a programming
project.

Here's one: Ruby's code blocks. Deceptively simple, but very powerful in
practice. They allow you to easily create domain specific languages very
easily in Ruby.

Quick example: I'm taking a Quantum Computing class. The prof is looking
for a language for designing quantum circuits. I signed up for the
project and createed a 'language' I call QDL (Quantum Design Language),
but it's reeally just plain Ruby under the covers - no parser needs be
created.

Here's an example of QDL:

circuit = QCircuit {
QBits = 4
l0 = QLayer {
feynmann(0,3)
feynmann(1,2)
}
l1 = QLayer {
toffoli(2,3,0)
}
}

Basically this describes the structure of a simple quantum circuit with
two layers and 3 gates. It can be simulated. I can now have my
classmates write QDL without having to know anything about Ruby at all.
Of course, if they're willing to learn a little Ruby it becomes much more
powerful since they can access all of Ruby from QDL.

The magic here is Ruby's code blocks (the code between '{' and '}'). They
can be passed around and 'called' or 'yield'ed to. I suspect it would be
very difficult to implement a QDL-like language in Python because Python
doesn't have code blocks like this (you would have to create a parser).

(BTW: QDL is implemented in about 400 line of Ruby, that includes all the
additional matrix operations (Kronecker product) and some simple error
checking).
- facets of the language which may not necessarily be useful, but are
notably creative/innovative.



Additionally, I realize that there is more to a language than its syntax
and functionality. As a result, I am also interested in hearing about
facets of the ruby community and ruby ideology, and anything else that
ruby programmers feel contribute to their affinity for the language.

I have a feeling that most of the things that make ruby worth using can
also be found in python. From the perspective a python programmer who has
taken only a precursory glance at ruby, it looks like ruby suggests a
functional style, while python suggests an imperative style. It also looks
like ruby may be better suited for quick shell scripts, whereas python is
better suited for bigger programs. I realize that I probably suffer from
misconceptions, and i'm looking forward to having them corrected.

You can use Ruby for large projects as well. There are rumors of 20,000
to 30,000 line Ruby programs here. Personally, I don't think I've written
anything that was more than ~5000 lines of Ruby - of course, in my
experience, 5K lines of Ruby packs about the same functionality as
20K to 25K lines of C++ code.
I sincerely hope nobody finds any of my comments offensive, I dont mean
them as criticisms of ruby. I intentionally withhold criticisms until I am
better versed in the benefits!


Mostly you need to try using Ruby on a small test project. Actually using
the language will give you a better feel for it than us telling you about
it.

Phil
 
J

Jim Weirich

Phil said:
Quick example: I'm taking a Quantum Computing class. The prof is looking
for a language for designing quantum circuits. I signed up for the
project and createed a 'language' I call QDL (Quantum Design Language),
but it's reeally just plain Ruby under the covers - no parser needs be
created.

This is the same approach I used when writing a Build Control Language
to replace make. I used Ruby blocks to describe the actions associated
with tasks.

Example:

task :compile do |t|
java.compile t.source
end

By making making the entire build file Ruby code, I have an incredibly
powerful build language. This would have been much more clumsy without
the use of blocks.
 
S

Sascha Ebach

I just wanted to thank everybody for the great responses. After going
through some of the links you folks provided, and reading through some
documentation, my curiosity has transformed into utter maniacal glee. Ruby
solves a lot of the problems I had with python. Blocks are particularly
enticing. I have spent a lot of time hacking around with python, trying to
abuse metaclasses so I could use the python parser for domain specific
languages. I'm delighted to find that ruby provides such an elegant
mechnaism to facilitate the task. Additionally, it looks like ruby's
syntax is significantly more elegant than python, in many respects.

Now i'm going to port a bunch of my python code for practice! =D


Hphew. That was fast and easy, wasn't it? Glad you found solutions to
your problems so quickly.
 
J

James Britt

Ryan said:
I just wanted to thank everybody for the great responses.

You're welcome.

I have a favor to ask. I'm guessing that most people on this list know
Ruby far better than they do Python. The Ruby/Python comparison is a
recurring theme, and we try to collect resources (such as the Ruby Eye
For The Python Guy page) so folks don't get callouses retyping the same
info. You being a Pythonista and all, I'd appreciate any comparisons
you can offer, or critiques of existing comparisons. There's little
point in referring people to resources that misrepresent Python, or put
up straw man arguments.



Thanks,


James Britt
 
R

Ryan Paul

You're welcome.

I have a favor to ask. I'm guessing that most people on this list know
Ruby far better than they do Python. The Ruby/Python comparison is a
recurring theme, and we try to collect resources (such as the Ruby Eye
For The Python Guy page) so folks don't get callouses retyping the same
info. You being a Pythonista and all, I'd appreciate any comparisons
you can offer, or critiques of existing comparisons. There's little
point in referring people to resources that misrepresent Python, or put
up straw man arguments.



Thanks,


James Britt

I've been taking a lot of notes while learning ruby, comparing the ruby
examples to python equivalents, and recording how both ruby and
python solve certain problems. I think I could probably clean up my notes
a bit, and restructure them into a relatively good comparison between the
languages. When I fill it out a bit more, i'll make it publicly available,
and drop a link here and in comp.lang.python (where I will probably be
lynched for my impertinence). So far, most of the criticisms of ruby that
flourish in the python community are based on misconceptions. (for
instance, a lot of people seem to know that ruby only supports single
inheritance, but what they dont know is that the mixin system MORE than
makes up for it)

I also want to write parts of it like a cookbook so that python
programmers can see how ruby handles some of the specific things that are
commonly done with python. The idea of 'switching languages' is really
unnerving for a lot of programmers, and I think its really important to
illustrate just how minimal the learning curve is. Ruby feels almost like
a logical extension of python, with some pleasant perl tricks thrown in
for good measure.

Presentation is always an issue, and i'm still deciding what the best way
to express the information would be. If I started a conversation with a
python programmer saying something like "have you ever tried ruby? it does
some neat things!" they would probably scoff, but if I said "what if you
could do assignments in anonymous functions?" I might capture their
interest.

There is a growing community of python malcontents that might be
interested in ruby. They have a lot of really creative syntax ideas, most
of which are ignored by the mainstream python community. I think it would
be of benefit to the ruby community to be aware of some of their interests
and motivations. Some of those programmers might be interested in
contributing to ruby, rather than working on their own independent
python forks, if I can illustrate ways in which ruby meets their
individual needs. To that end, I will probably spend some time looking at
python derivatives to see where the divergences are, and how (if at all)
their python extensions compare to some of ruby's development idioms.
'Prothon' is a good example of a python derivative on my list. I also mean
to show how ruby provides native support for many of the features hacked
into python by Xoltar's partial, functional, and datastruct libraries.

Ultimately, i'm interested in a flexible and powerful interpreted
language, and what I have seen of ruby's style indicates a progressive and
intuitive development paradigm that is bold enough to cast aside standards
and cliches to return the power to the user.

--segphault
 
G

gabriele renzi

il Tue, 11 May 2004 07:26:02 GMT, Ryan Paul <[email protected]>
ha scritto::

There is a growing community of python malcontents that might be
interested in ruby.

Well, it's true that there is a constant flux of people trying to make
ruby their own language. I tried this too :)

We just don't have forks/clones, stuff that is in many places for
python (CPython's core,Stackless' continuations,Prothon's prototypes
and '=' as an expression) is all available in core ruby or can be
easily done through smart usage of few features.
They have a lot of really creative syntax ideas, most
of which are ignored by the mainstream python community. I think it would
be of benefit to the ruby community to be aware of some of their interests
and motivations.

I use to read c.l.py from time to time, and I found prothon initially
interesting.
But, the core idea from Prothon (i.e. classless prototyped OO) is
already available in ruby[1], (provided that you define with()
properly and use Object.clone()), while many others like the funny
obj$met syntax and many things in David Mc Quigg's proposals (the
'python 3' and 'python 4' ones) does not sound really nice to me..
Some of those programmers might be interested in
contributing to ruby, rather than working on their own independent
python forks, if I can illustrate ways in which ruby meets their
individual needs.

You're missing the social point of view:
everybody wants to write the next big language :)

[1]
well, partially joking :)
 
D

Dave Burt

Ryan said:
I've been taking a lot of notes while learning ruby, comparing the ruby
examples to python equivalents, and recording how both ruby and
python solve certain problems. I think I could probably clean up my notes
a bit, and restructure them into a relatively good comparison between the
languages. When I fill it out a bit more, i'll make it publicly available
<snip>
I also want to write parts of it like a cookbook so that python
programmers can see how ruby handles some of the specific things that are
commonly done with python.
<snip>
Presentation is always an issue, and i'm still deciding what the best way
to express the information would be. If I started a conversation with a
python programmer saying something like "have you ever tried ruby? it does
some neat things!" they would probably scoff, but if I said "what if you
could do assignments in anonymous functions?" I might capture their
interest.
<snip>
... flexible and powerful interpreted language ... a progressive and
intuitive development paradigm ... bold enough to cast aside standards
and cliches to return the power to the user.

--segphault

Sounds to me like you might have a great contribution to make to
Why Ruby? <http://whyruby.rubyforge.org/>
which is a project discussed in a thread on this group:
Promoting Ruby <The aim of Why Ruby is to produce advocacy and presentation materials, to
the end of improving Ruby's take-up in the workplace.

I'm glad, too, that you've found Ruby such a help.

Dave
 
P

Phil Tomson

On Tue, 11 May 2004 12:50:24 +0900, James Britt wrote:


I've been taking a lot of notes while learning ruby, comparing the ruby
examples to python equivalents, and recording how both ruby and
python solve certain problems. I think I could probably clean up my notes
a bit, and restructure them into a relatively good comparison between the
languages. When I fill it out a bit more, i'll make it publicly available,
and drop a link here and in comp.lang.python (where I will probably be
lynched for my impertinence).

Wear asbestos clothing that day ;-)
So far, most of the criticisms of ruby that
flourish in the python community are based on misconceptions. (for
instance, a lot of people seem to know that ruby only supports single
inheritance, but what they dont know is that the mixin system MORE than
makes up for it)

We often hear that there's not much difference between Ruby and Python
(and some Pythonistas suggest that Ruby is redundant). I don't have
extensive Python experience, but my intuition is that this isn't true at
all once you get past superficial comparisons. You seem to be debunking
the myth that the two languages are essentially equivilent.
I also want to write parts of it like a cookbook so that python
programmers can see how ruby handles some of the specific things that are
commonly done with python. The idea of 'switching languages' is really
unnerving for a lot of programmers, and I think its really important to
illustrate just how minimal the learning curve is. Ruby feels almost like
a logical extension of python, with some pleasant perl tricks thrown in
for good measure.

They're not real big on mentioning Perl over there ;-)
Presentation is always an issue, and i'm still deciding what the best way
to express the information would be. If I started a conversation with a
python programmer saying something like "have you ever tried ruby? it does
some neat things!" they would probably scoff, but if I said "what if you
could do assignments in anonymous functions?" I might capture their
interest.

There is a growing community of python malcontents that might be
interested in ruby. They have a lot of really creative syntax ideas, most
of which are ignored by the mainstream python community. I think it would
be of benefit to the ruby community to be aware of some of their interests
and motivations. Some of those programmers might be interested in
contributing to ruby, rather than working on their own independent
python forks, if I can illustrate ways in which ruby meets their
individual needs. To that end, I will probably spend some time looking at
python derivatives to see where the divergences are, and how (if at all)
their python extensions compare to some of ruby's development idioms.
'Prothon' is a good example of a python derivative on my list. I also mean
to show how ruby provides native support for many of the features hacked
into python by Xoltar's partial, functional, and datastruct libraries.

Interesting.

Ultimately, i'm interested in a flexible and powerful interpreted
language, and what I have seen of ruby's style indicates a progressive and
intuitive development paradigm that is bold enough to cast aside standards
and cliches to return the power to the user.

Welcome to Ruby-land.

Phil
 
R

Ryan Paul

i'm a python programmer, and I have recently been hearing a lot about
ruby. A lot of python people seem to think ruby is in some way inferior,
but I have yet to find any significant differences between the languages.
The only differences I have found so far have been superficial syntactic
things. The only real significant difference I have detected, is the
absence of a module system in ruby.

I would like to be able to make an objective and educated comparison. Ruby
seems to have a relatively small, but intensely devoted following.
Software that inspires such fervent support from a small community usually
possesses tremendous power that most people are just too dense to discern
(eg: ocaml, and BeOS). Consequently, I figured it would be worth my time
to find out what it is that the fanatics find appealing.

I am looking for:
- examples of tangible features that ruby has, which python doesnt.
- examples of scenarios where some facet of, or property specific to
ruby contributed to the simplification or improvement of a programming
project.
- facets of the language which may not necessarily be useful, but are
notably creative/innovative.

Additionally, I realize that there is more to a language than its syntax
and functionality. As a result, I am also interested in hearing about
facets of the ruby community and ruby ideology, and anything else that
ruby programmers feel contribute to their affinity for the language.

I have a feeling that most of the things that make ruby worth using can
also be found in python. From the perspective a python programmer who has
taken only a precursory glance at ruby, it looks like ruby suggests a
functional style, while python suggests an imperative style. It also looks
like ruby may be better suited for quick shell scripts, whereas python is
better suited for bigger programs. I realize that I probably suffer from
misconceptions, and i'm looking forward to having them corrected.

I sincerely hope nobody finds any of my comments offensive, I dont mean
them as criticisms of ruby. I intentionally withhold criticisms until I am
better versed in the benefits!

Thanks in advance!
-- SegPhault

Some general conclusions:

I've learned quite a bit more ruby, and i've ported a bit of python code
by hand for exercise, and I think I can now safely make some useful
generalizations about the differences between the two languages.

Python is simple and consistent, it promotes uniform style between
programs and programmers. Python also works very hard to prevent the
programmer from doing something that might make the language inconsistent,
and it's design often enforces a clear, and concise imperative design.
Syntactic shortcuts and things like native regular expressions are
sacrificed to ensure maximum maintainability and readability.

Ruby is flexible and syntactically mutable. It allows programmers to alter
the behavior of the language itself in contexts where it may be beneficial
to do so. It promotes self extension and alteration, and it introduces a
lot of additional (largely optional) complexity that imbues the language
with the capacity to be, at times, rather arcane. In many cases, ruby
provides convenient syntactic shortcuts to automate things that would have
to be done manually in python.

Ruby idioms dont feel as instantly intuitive as python idioms, but they
seem a great deal more natural. I would definitely say that learning ruby
is a greater challenge than learning python. I would describe several
facets of ruby as 'mind altering'.

Ruby's OO is significantly better than python's. Ruby promotes (and
practically implies) good OO techniques and methodology. This is mostly a
result of Ruby's practice of making all instance variables private, and
the use of a rather elegant means of customizing variable access.

A few examples:

Blocks are a rather simple concept, but they are, initially, extremely
hard to understand, simply because there is nothing else like them. Python
does not provide any profoundly novel or innovative syntactic constructs,
so it easy for any object oriented programmer to grasp.

The axiomatic syntactic classes in python (lists, strings, integers) are
completely and totally immutable. Python does NOT allow you to change or
extend these classes in any way. This is done to ensure utter uniformity,
but it cripples the programmer immensely. Guido Van Rossum, in one of his
essays, says that python could easily support this feature, but he
intentionally chose to disallow it to ensure that programmers wouldn't
accidentally break each others libraries, or the python base libraries for
that matter. Programming around ideology like that is like trying to carve
a sculpture with safety scissors.

In python, we have map, filter, and list comprehensions, all of which can
be used to manipulate lists in very general ways. In order to do certain
things, python users have to stack multiple functions into map statements.
Generally, this is actually pretty simple to do, and makes things somewhat
intuitive. In ruby, there are a multitude of rather oddly named array
functions that have very specific behaviors, most of which can be used
with blocks to concisely describe extremely sophisticated operations.

In conclusion, I feel that python, because of its steadfast consistency,
might be more appropriate for corporate applications, but for power
scripters, who need power and flexibility, nothing beats ruby. I'd like to
know if ruby supports something like C++ namespaces, because something
like that would probably vastly decrease the destructive potential of
inconsistency, without killing its usefulness. I've suggested such a
feature several times in the python world, but nobody seemed to see the
benefit.

--SegPhault
 
M

Mark Hubbart

I'd like to
know if ruby supports something like C++ namespaces, because something
like that would probably vastly decrease the destructive potential of
inconsistency, without killing its usefulness. I've suggested such a
feature several times in the python world, but nobody seemed to see the
benefit.

Ruby 2.0 will (probably) support selector namespaces. For more info:
http://www.rubygarden.org/ruby?Rite

I agree that there should be some flexible namespace mechanism to help
contain possible damage done by class redefinitions. It would be nice
to, say, allow an external script to change core classes without
worrying about it breaking someone else's code (in a shared interpreter
context).

cheers,
--Mark
 
G

gabriele renzi

il Wed, 12 May 2004 16:37:42 GMT, Ryan Paul <[email protected]>
ha scritto::


In python, we have map, filter, and list comprehensions, all of which can
be used to manipulate lists in very general ways. In order to do certain
things, python users have to stack multiple functions into map statements.
Generally, this is actually pretty simple to do, and makes things somewhat
intuitive. In ruby, there are a multitude of rather oddly named array
functions that have very specific behaviors, most of which can be used
with blocks to concisely describe extremely sophisticated operations.


In ruby there are a lot of Enumerable functions, not array's :)
What method do you think is oddly named ?
I for one, would say inject() but using reduce() or fold() is equally
non intuitive for me :)
 
R

Ryan Paul

il Wed, 12 May 2004 16:37:42 GMT, Ryan Paul <[email protected]>
ha scritto::





In ruby there are a lot of Enumerable functions, not array's :)
What method do you think is oddly named ?
I for one, would say inject() but using reduce() or fold() is equally
non intuitive for me :)

Its not so much that their names are strange, its the fact that they exist
at all. In python, lists only have like 8 or 9 functions, and they are all
pretty standard and self explanatory (append, count, extend, insert,
remove, reverse, and maybe two or three others). Having things like
collect, replace, assoc, and compact built into arrays is something of a
novelty for a python programmer. I'm used to having to do replacement like
this, for example:
[y for x in listobj if x == z]

Personally, I feel that it is better to have functions that do these
things, because list comprehensions scale grotesquely, so I prefer the way
ruby does it. Not only does ruby provide a ton of useful functions to
start with, but I can add as many of my own as I want!

Also, the fact that collect doesnt automatically compact really threw me
for a spin. I was using "x.collect {whatever}.compact" until I figured out
that it is easier to do a reject on the inverse of what I would have put
in a python 'filter'. I added a 'filter' function to the array class that
fixes this for me.

Additionally, having two different names for the same thing (eg: collect
and map) was initially confusing, but only because i'm not used to such
things. Python would NEVER do that.

--segphault
 
L

Linus Sellberg

gabriele said:
What method do you think is oddly named ?
I for one, would say inject() but using reduce() or fold() is equally
non intuitive for me :)

There are a really really nice name for it out there.

Unfortunately, collect are already used.
 
J

Joel VanderWerf

Linus said:
There are a really really nice name for it out there.

Unfortunately, collect are already used.

accumulate might be a better name, but it's too loooong.
 
J

Jim Freeze

On Mon, 10 May 2004 10:42:02 +0000, Ryan Paul wrote:

In conclusion, I feel that python, because of its steadfast consistency,
might be more appropriate for corporate applications, but for power
scripters, who need power and flexibility, nothing beats ruby. I'd like to
know if ruby supports something like C++ namespaces, because something
like that would probably vastly decrease the destructive potential of
inconsistency, without killing its usefulness. I've suggested such a
feature several times in the python world, but nobody seemed to see the
benefit.

--SegPhault

Hmm, that is an interesting comment about the corporate use of Ruby.
I considered the opposite...that python is too restrictive
and not generally compatible with peoples tastes
(especially the tab/indent issue and invisible ends) unlike Ruby,
which promotes good OO and generally liked by most people.
 
R

Robert Klemme

Ryan Paul said:
il Wed, 12 May 2004 16:37:42 GMT, Ryan Paul <[email protected]>
ha scritto::





In ruby there are a lot of Enumerable functions, not array's :)
What method do you think is oddly named ?
I for one, would say inject() but using reduce() or fold() is equally
non intuitive for me :)

Its not so much that their names are strange, its the fact that they exist
at all. In python, lists only have like 8 or 9 functions, and they are all
pretty standard and self explanatory (append, count, extend, insert,
remove, reverse, and maybe two or three others). Having things like
collect, replace, assoc, and compact built into arrays is something of a
novelty for a python programmer. I'm used to having to do replacement like
this, for example:
[y for x in listobj if x == z]

Personally, I feel that it is better to have functions that do these
things,

Did you mean to say "methods"? I ask because strictly speaking there are
no functions in Ruby. They are all tied to some instance.
because list comprehensions scale grotesquely, so I prefer the way
ruby does it. Not only does ruby provide a ton of useful functions to
start with, but I can add as many of my own as I want!

Also, the fact that collect doesnt automatically compact really threw me
for a spin. I was using "x.collect {whatever}.compact" until I figured out
that it is easier to do a reject on the inverse of what I would have put
in a python 'filter'.

Normally I would first select and then map. That's more efficient IMHO:

irb(main):015:0> a = %w{a bb ccc}
=> ["a", "bb", "ccc"]
irb(main):016:0> a.select {|x| /^[ab]/ =~ x}.map {|x| "(#{x})"}
=> ["(a)", "(bb)"]
irb(main):017:0> a.select {|x| x.length > 2}.map {|x| "(#{x})"}
=> ["(ccc)"]
I added a 'filter' function to the array class that
fixes this for me.

It can be far easier if your criterion happens to support ===, like Regexp
does. Use #grep:

irb(main):008:0> a = %w{a bb ccc}
=> ["a", "bb", "ccc"]
irb(main):009:0> a.grep(/^[ab]/) {|x| "(#{x})"}
=> ["(a)", "(bb)"]

You can easily make up your own criterion:

irb(main):010:0> crit = Object.new
=> #<Object:0x101a2920>
irb(main):011:0> def crit.===(x); x.length > 2; end
=> nil
irb(main):012:0> a.grep(crit) {|x| "(#{x})"}
=> ["(ccc)"]

You can make your life even simpler with:

module Criterion
def self.create(&b)
def b.===(x); call(x);end
b
end
end

irb(main):031:0> a = %w{a bb ccc}
=> ["a", "bb", "ccc"]
irb(main):032:0> crit = Criterion.create {|x| x.length > 2}
=> #<Proc:0x100c4978@(irb):32>
irb(main):033:0> a.grep(crit) {|x| "(#{x})"}
=> ["(ccc)"]
Additionally, having two different names for the same thing (eg: collect
and map) was initially confusing, but only because i'm not used to such
things. Python would NEVER do that.

Ruby tries to be nice to people coming from different other languages.
Some have map and some have collect. :)

Regards

robert
 
G

Gavin Sinclair

Robert said:
You can make your life even simpler with:

module Criterion
def self.create(&b)
def b.===(x); call(x);end
b
end
end

irb(main):031:0> a = %w{a bb ccc}
=> ["a", "bb", "ccc"]
irb(main):032:0> crit = Criterion.create {|x| x.length > 2}
=> #<Proc:0x100c4978@(irb):32>
irb(main):033:0> a.grep(crit) {|x| "(#{x})"}
=> ["(ccc)"]

How is this simpler than

a.select { |x| x.length > 2 }

?

OIC. You're using the block form of grep to save yourself a map. I don't
much like it, though, because the filtering predicate is removed from the
place where it's used, wasting the beuaty of blocks.

Clever, though, and probably useful in some situations (e.g. where you're
doing *lots* of filtering and mapping :)

Cheers,
Gavin
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top