CORE - Altering Behaviour of "each do" (default param "item")

On 6 Ιούν, 12:24, Yukihiro M. [email protected] wrote:

So being “speakable” is important from your point of view, is it?
Yes, usually I prefer to have source code that can be “spoken”.

|> Currently I have no plan to change the core like that.
|
|So this would be a low-priority issue?
|Would a thoroughly worked out patch be accepted?

I am concerned about compatibility for corner-case behavior and
performance loss, both of which does not seem to be in your interest.

I understand (and share) those concerns.

Hypothetically, if a patch would have no performance losses and would
not influence behaviour in any way, would it be considered?

.

On 06.06.2011 11:12, Ilias L. wrote:

names.each { puts item }
names.each { puts e) # e = entry
In any case, you would need to create a kind of new keyword - what may
break current code.

Think about:

e = “sth”
names = %w(tom frank eva)
names.each { puts e }

On 6 , 13:20, Yukihiro M. [email protected] wrote:

start considering it positively
ok, very nice.

(but I still prefer “it”).

The positive thing with “it” is:

  • it’s shorter
  • it’s used in another language (I rate construct adoption as a good
    thing)
  • can be read as shortform for “item”.

.

it’s used in another language (I rate construct adoption as a good thing)

But Ruby is beautiful as it is. It does not need to look at other
languages to remain beautiful.

Matz once included some perlism into ruby. I myself still struggle to be
able to tell you the difference between $$ or $_ or $` without looking
at a reference table… (which is why I tend to use the English library
always, and the longer variable names.)

But perl never really had the focus on elegance, unlike ruby. Perl
always just wanted to be hackable and practical and didn’t bother about
how elegant the code looked.

Ruby is beautiful.

can be read as shortform for “item”.

But “it” in itself has a meaning in the english language already! It
isn’t in general thought as a shortcut for “item”. So your reasoning
isn’t perfectly sound IMO, Ilias.

Hi,

In message “Re: CORE - Altering Behaviour of “each do” (default param
“item”)”
on Mon, 6 Jun 2011 19:00:29 +0900, Ilias L.
[email protected] writes:

|Hypothetically, if a patch would have no performance losses and would
|not influence behaviour in any way, would it be considered?

It’s not that easy decision before evaluating the impact to the whole
language design. But if those two assumptions were cleared, I will
start considering it positively (but I still prefer “it”).

          matz.

On 06.06.2011 10:15, Ilias L. wrote:

This approach is not usable in the given context (Altering Behaviours

I don’t have to “look close enough”

That speaks for itself.

Your solution does not work in this context.

The solution from Gray works 100%.

Well, yes and no. From what I read I’d say you are not aware of all the
consequences. The reason is probably that you do not look closer. It’s
your choice of course but it makes me feel uncomfortable to see people
running away with solutions which they do not seem to understand.
Usually this causes some smaller or larger disaster later on.

Over and out.

robert

On 06.06.2011 16:57, David M. wrote:

names.each { puts item }

names.each { puts e) # e = entry

Currently I have no plan to change the core like that.

So this would be a low-priority issue?

Would a thoroughly worked out patch be accepted?

I’m not sure that I have a vote, but I’d also vote against.

Why wouldn’t you? (Whether our votes are counted somewhere is a
different question though.) :wink:

I like the
motivation, and that’s why we have clever hacks like Symbol#to_proc:

names.map(&:to_s)

Lots of good reasoning

while() {
actual variables mixed and matched with automagic ones.
That’s because Perl got OO retrofitted later - in a bad way. (Well,
there was probably no other choice given the language as it was at the
time.) Ah, how much nicer is the clean OO of Ruby!

And that’s only one kind of default variable, and there are all sorts of other
variables Perl magically defines for you. Ruby’s borrowed a few of them, but
they’re all well-defined by now, and generally obviously ‘special’ in some
way, like FILE and $1.

Ironically $_ is one of them. :slight_smile:

$ seq 1 5 | ruby19 -ne ‘p $_’
“1\n”
“2\n”
“3\n”
“4\n”
“5\n”

$

Sometimes, verbosity is good. I’d much rather do a little extra typing from
time to time than have Perl-like black magic going on behind the scenes.

+1

Kind regards

robert

On Monday, June 06, 2011 03:35:30 AM Ilias L. wrote:

names.each { puts e) # e = entry

Currently I have no plan to change the core like that.

So this would be a low-priority issue?

Would a thoroughly worked out patch be accepted?

I’m not sure that I have a vote, but I’d also vote against. I like the
motivation, and that’s why we have clever hacks like Symbol#to_proc:

names.map(&:to_s)

But adding a magic variable seems like it would be very surprising in a
bad
way. Suppose I left an empty argument list because I actually don’t want
any
arguments? For example:

10.times { puts ‘Hello, world!’ }

It doesn’t really help if it only applies to ‘each’, either. Then we
have the
very surprising situation where these two lines do different things:

item = ‘world’
10.times { puts “Hello, #{item}!” }
10.times.each { puts “Hello, #{item}!” }

And ‘item’ is a common word. In fact, the suggestion for ‘it’ seems like
it
would be even worse in this respect. On the other hand, choosing
anything
that’s deliberately uncommon, like, say, item, would defeat the
purpose.

One approach might be to somehow let other variables take precedence,
so, for
example, if I’d defined ‘item’ elsewhere, it wouldn’t be overridden by
this.
But that opens up its own can of worms. Do you consider only local
variables?
Then a local variable would be different than a method call on the
current
object, which is surprising. And if you do consider methods, how do you
find
out whether the current object responds to the method you want? If you
only
look at methods actually defined, you miss any method_missing hacks,
which is
surprising. If you call respond_to?, that’s still not foolproof, and
it’s
likely to have serious performance implications, unless you cache the
result
– and if you do, then I can’t define methods inside that loop and
expect to
use them immediately, which seems like a reasonable thing to do.

I don’t really see a good way to make this work.

One of the more interesting features of Perl is the default variable, $_

most builtin functions will automatically default to this if you don’t
provide
an explicit variable. It certainly prevents some repetitive code, makes
some
things seem shorter and cleaner, and it seems pretty cool. But then you
end up
with code like this:

while() {
chomp;
print “$1\n” if /^Hello, (.*)/;
}

…and that’s a short, mostly-readable example because I frankly don’t
remember enough about Perl to make it really dangerous. Even so, look at
how
twisted it is already. If you’re not a Perlist already, it’s not obvious
that
“while()” actually loops over each line of standard input. Once
you get
that, you might figure out that chomp and the regex should apply to
these
lines, but it’s much more common to find a somewhat larger loop body,
with
actual variables mixed and matched with automagic ones.

And that’s only one kind of default variable, and there are all sorts of
other
variables Perl magically defines for you. Ruby’s borrowed a few of them,
but
they’re all well-defined by now, and generally obviously ‘special’ in
some
way, like FILE and $1. Adding new ones at this point, especially in
such a
way that they could easily be confused with common variables, seems like
a
very bad idea.

Sometimes, verbosity is good. I’d much rather do a little extra typing
from
time to time than have Perl-like black magic going on behind the scenes.

On Mon, Jun 06, 2011 at 11:57:09PM +0900, David M. wrote:

that, you might figure out that chomp and the regex should apply to these
lines, but it’s much more common to find a somewhat larger loop body, with
actual variables mixed and matched with automagic ones.

Actually, within the context of Perl, I see no problem with that code.
There are little bits of elegance that one can eke out of code via the
implicit scalar in Perl that justify the way $_ works – which is
actually not the same thing as the proposed “item” at all. Where $_ in
Perl is an implicit scalar variable, “item” in Ruby as proposed would be
a default explicit argument. Given its requirement for explicit
calling,
I really don’t think it buys us nearly enough to justify the negative
consequences of it, and I don’t think the idea of a Perl-style implicit
scalar variable is consistent with the “flavor” of Ruby code in general,
so I think we agree in effect even if I don’t find your specific example
at all compelling.

Basically any programming language keyword and bit of syntactic sugar
can
become a problem if misused (including the implicit scalar in Perl). It
is up to the programmer’s sense of how to write clear code to keep
syntactic sugar from becoming a deteriment to code clarity rather than a
help. It is up to the language’s definers and implementors to ensure
that syntactic sugar that mostly consists of detriments doesn’t make it
into the language. I don’t think $_ in Perl mostly consists of
detriments for that language; I do think the proposed default argument
in
Ruby mostly consists of detriments.

On Tue, Jun 07, 2011 at 01:40:25AM +0900, Robert K. wrote:

$ seq 1 5 | ruby19 -ne ‘p $_’
“1\n”
“2\n”
“3\n”
“4\n”
“5\n”

Holy crap. I had no idea.

I don’t think the existence of $_ is really a completely egregious
problem (though I do dislike its ill fit in Ruby). For a moment there I
thought it was an incomplete implementation of $_, but I realized that
for some reason “puts” and “p” do not work as I would expect in this
context. Specifically, I would expect these three commands to be
equivalent in effect:

seq 1 5 | perl -ne 'print'
seq 1 5 | ruby -ne 'puts'
seq 1 5 | ruby -ne 'p'

For some reason, though, they are not. The puts and p versions in Ruby
do not output anything at all (other than blank lines in the case of
puts). If I use print in Ruby, though, the behavior is equivalent:

seq 1 5 | ruby -ne 'print'

I suspect I missed something that should be obvious but I have no idea
why puts and p do not seem to be aware of $_ while print does behave as
though aware of it in Ruby.

On 6 Ιούν, 19:29, Robert K. [email protected] wrote:
[…]

If you look close enough you will notice that both approaches work
(or don’t) the same way with regard to #each - for the same reason!

I don’t have to “look close enough”

That speaks for itself.

I don’t have to “look close enough” (only in the given context that
I’ve choose to set)

Your solution does not work in this context.

The solution from Gray works 100%.

Well, yes and no. From what I read I’d say you are not aware of all the
consequences. The reason is probably that you do not look closer. It’s
your choice of course but it makes me feel uncomfortable to see people
running away with solutions which they do not seem to understand.
Usually this causes some smaller or larger disaster later on.

I understand perfectly (in the given context that I’ve choose to set)

In a production app, I would set a complete different context.

Over and out.

Hope this is a promise.

Cu in another thread (hopefully less verbose and more in context)

.

I’m looking for a recipe for “slow roasted Lizard”

On Mon, Jun 6, 2011 at 12:19 PM, Chad P. [email protected] wrote:


Chad P. [ original content licensed OWL: http://owl.apotheon.org ]

Also Perlish for regex within conditionals:

$ echo hello world | ruby -W0 -ne ‘puts $_ if “strings are objects”’
hello world

$ echo hello world | ruby -W0 -ne ‘puts $_ if /regex are objects/’

$ echo hello world | ruby -W0 -ne ‘puts $_ if /i just came to
say|hello/’
hello world

On Mon, Jun 6, 2011 at 7:19 PM, Chad P. [email protected] wrote:

For some reason, though, they are not. The puts and p versions in Ruby
do not output anything at all (other than blank lines in the case of
puts). If I use print in Ruby, though, the behavior is equivalent:

seq 1 5 | ruby -ne ‘print’

I suspect I missed something that should be obvious but I have no idea
why puts and p do not seem to be aware of $_ while print does behave as
though aware of it in Ruby.

There’s also -p:

Robert@babelfish ~
$ seq 1 5 | ruby19 -pe ‘’
1
2
3
4
5

Robert@babelfish ~
$ seq 1 5 | ruby19 -pe ‘gsub /^/, “X”’
X1
X2
X3
X4
X5

Robert@babelfish ~
$

Kind regards

robert

On Monday, June 06, 2011 11:40:25 AM Robert K. wrote:

“3\n”
“4\n”
“5\n”

That doesn’t seem to be the same thing, though. In Perl, $_ is the
“default”
variable – anything you’d ordinarily do to a specific variable or
expression
can often have that variable/expression ommitted, in which case, it’ll
use $,
@
, or %_.

In Ruby, $_ is much narrower in scope – a quick Google says, “The last
input
line of string by gets or readline.” That puts it more in the same
category as
the regex matchers.

I’d probably still rather not have them, but I definitely don’t see a
good way
to add more at this point. It’d be like trying to add a keyword.

2011/6/7 Jörg W Mittag [email protected]

Would probably be the most idiomatic way.

That’s a fairly sweeping statement. I know more than the tiniest bit of
Ruby
and I prefer { |name| puts name } to &method(:puts). More than prefer,
in
fact, I would never use the latter.

Ilias L. wrote:

I’ve just noticed repetitions within code like that:

names.each { |name| puts name} # 3 times “name(s)”

That’s because nobody who knows even the tiniest bit of Ruby would
ever write it like that.

names.each(&method(:puts))

Would probably be the most idiomatic way.

BTW: if you’re such a big fan of point-free programming, you should
seriously consider using a language like J (they call it tacit
programming, but it’s the same thing), which actually supports it.

jwm

On Tue, Jun 7, 2011 at 12:54 PM, Robert K.
[email protected]wrote:

In this case you’d probably rather want to use “puts names” and avoid
explicit iteration altogether. :slight_smile:

True!

On 6 Ιούν, 02:42, David M. [email protected] wrote:

http://en.wikipedia.org/wiki/Wikipedia:Articles_for_deletion/Ilias_La

It doesn’t seem that “violation” was the primary concern, so much as that
there wasn’t enough reliable information to back it up. If that wasn’t the
case, I have to imagine that “notability” would’ve kicked in.

" The article is one of these “I want to attack the guy without
crossing the Wikipedia blatant personnal attack line” "

The most important thing to learn is:

when is it “expressing negative feelings” and when is it “defamation
of character”:

Legal threats again?

There is no threat, you just interpret one.

Is this the only way you can get anyone to take you seriously?
Alright, I’ll use your source this time:
[…] - aborted reading, due to time constraints.

I don’t care if you (or people of your kind) take me serious.

Simply because I don’t take you serious.

Within open discussion groups, I respect and take serious (on a
professional level) only those who stay in-topic and in-context. Those
who not bombard me with tons of irrelevant details, just to cover
their own inablitiy or ignorance, or simply just to have to say
something in a lonely evening.

I respect people which have the discipline to stay unbiased, even if
they have possible negative personal feelings against me (e.g. because
the dislike mey personal writing style, which is part of my
individuality.).

I respect people which understand that there is a difference between
“analytic ability” and “knowledge”, and that knowledge can many times
reduce the analytic ability, thus it must be assimilated with care.

I respect them and take them serious, because those are the people
which can evolve a system (like a programming language) in an
efficient way.

Please!

Stay in-topic and in-context - or stay out of the topics.

You owe this not only to me, but to every current and future reader of
the archives.

If you have an unresistable need to express yourself, simply open a
new topic with an “[OT]” (off-topic) marker, e.g.:

[OT] The unbelieavable attitude of Mr. Ilias L.

and write whatever you like. It’s far more professional than to
destroy a clearly technical thread with 80% irrelevant content.

.

On Tuesday, June 07, 2011 06:26:32 AM Adam P. wrote:

names.each(&method(:puts))

Would probably be the most idiomatic way.

That’s a fairly sweeping statement. I know more than the tiniest bit of
Ruby and I prefer { |name| puts name } to &method(:puts). More than
prefer, in fact, I would never use the latter.

While I would use &method(:puts), it honestly never occurred to me, and
it
seems like a clever hack. It also only solves that one problem, and not,
say:

names.map{|name| name.split.last}

I suppose I could do this instead, but it doesn’t save any typing and
results
in an extra intermediate array being constructed:

names.map(&:split).map(&:last)

I’m sure I’m missing something, though.