Keyword "with"

It might be better to replace foo++ with (foo=foo.succ) because it will work
on things other than numbers - except for some reason there doesn’t seem to
be any ‘pred’, so you can’t implement foo-- in this way :frowning:

irb(main):003:0> “fred”.succ
=> “free”

Regards,

Brian.

···

On Thu, Jul 17, 2003 at 06:54:44PM +0900, Anders Borch wrote:

Now, any file loaded with loadpp may contain the ++ operator, and it
will be replaced with ‘+= 1’. Have fun with it if you like :slight_smile:

Processes calling ruby scripts as subprocesses and scraping their output
might have to be retooled if they start getting new warnings that weren’t
there…?

···

Brian Candler B.Candler@pobox.com wrote:

Just put “#!/usr/local/bin/ruby -w” at the start of all your scripts. As
Matz has mused before, it should have been compulsory (with an option to
turn it off)

It seems to me that that could be done at any point, without ‘breaking’
anything (your code will still run; it’ll just spit out warnings). What
are the cons?

This would annoy me:

[pbrannan@zaphod borg]$ ruby -w which irb
/usr/local/lib/ruby/1.6/irb/context.rb:129: warning: method redefined; discarding old irb_name
/usr/local/lib/ruby/1.6/irb/context.rb:200: warning: method redefined; discarding old math_mode=
/usr/local/lib/ruby/1.6/irb/context.rb:214: warning: method redefined; discarding old use_readline=
/usr/local/lib/ruby/1.6/irb/context.rb:201: warning: instance variable @math_mode not initialized
/usr/local/lib/ruby/1.6/irb/context.rb:171: warning: instance variable @use_tracer not initialized
irb(main):001:0>

but perhaps having -w be default would convince the author to fix the
code that caused the warnings.

Paul

···

On Thu, Jul 17, 2003 at 02:30:09AM +0900, Martin DeMello wrote:

It seems to me that that could be done at any point, without ‘breaking’
anything (your code will still run; it’ll just spit out warnings). What
are the cons?

Brian Candler wrote:

···

On Thu, Jul 17, 2003 at 06:54:44PM +0900, Anders Borch wrote:

Now, any file loaded with loadpp may contain the ++ operator, and it
will be replaced with ‘+= 1’. Have fun with it if you like :slight_smile:

It might be better to replace foo++ with (foo=foo.succ) because it will work
on things other than numbers - except for some reason there doesn’t seem to
be any ‘pred’, so you can’t implement foo-- in this way :frowning:

irb(main):003:0> “fred”.succ
=> “free”

true… I wouldn’t be able to implement that in as few lines, since
#succ does not modify the receiver the way +=1 and ++ does.

Just put “#!/usr/local/bin/ruby -w” at the start of all your scripts.
As
Matz has mused before, it should have been compulsory (with an option
to
turn it off)

It seems to me that that could be done at any point, without ‘breaking’
anything (your code will still run; it’ll just spit out warnings). What
are the cons?

Processes calling ruby scripts as subprocesses and scraping their output
might have to be retooled if they start getting new warnings that weren’t
there…?

Nice guess, but I don’t think that’s an issue… the
warnings seem to go to stderr, not stdout.

Hal

···

----- Original Message -----
From: “Michael Campbell” michael_s_campbell@yahoo.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, July 16, 2003 1:08 PM
Subject: Re: Other languages’ features in Ruby

Brian Candler B.Candler@pobox.com wrote:


Hal Fulton
hal9000@hypermetrics.com

So how about this: pick a letter of the alphabet that isn’t used on the
current ruby commandline. Make it the new “run with warnings disabled”
option. Give it higher priority than “-w” so if both are specified then it
runs silently rather than with warnings. Announce that the “-w” switch is
deprecated and that the new default will be to run with warnings enabled
unless that switch is specified on the next major release (after 1.8). After
that, get rid of the “-w” commandline option. People will then have 1 major
release cycle to add the “run silently” option to their programs, but
eventually the warnings will always be there unless they’re turned off.

Ben

···

On Wed July 16 2003 2:08 pm, Michael Campbell wrote:

Brian Candler B.Candler@pobox.com wrote:

Just put “#!/usr/local/bin/ruby -w” at the start of all your scripts. As
Matz has mused before, it should have been compulsory (with an option to
turn it off)

It seems to me that that could be done at any point, without ‘breaking’
anything (your code will still run; it’ll just spit out warnings). What
are the cons?

Processes calling ruby scripts as subprocesses and scraping their output
might have to be retooled if they start getting new warnings that weren’t
there…?

Anders Borch wrote:

Brian Candler wrote:

Now, any file loaded with loadpp may contain the ++ operator, and it
will be replaced with ‘+= 1’. Have fun with it if you like :slight_smile:

It might be better to replace foo++ with (foo=foo.succ) because it
will work
on things other than numbers - except for some reason there doesn’t
seem to
be any ‘pred’, so you can’t implement foo-- in this way :frowning:

irb(main):003:0> “fred”.succ
=> “free”

true… I wouldn’t be able to implement that in as few lines, since
#succ does not modify the receiver the way +=1 and ++ does.

actually I was wrong… gsub solves this quite nicely…

def loadpp(l)
s = ‘’
f = File.new(l,‘r’)
f.each_line do |line|
s += line.gsub(/(\w)++)/,‘\1=\1.succ’) + “\n”
end
eval(s)
end

/Anders

···

On Thu, Jul 17, 2003 at 06:54:44PM +0900, Anders Borch wrote:

Processes calling ruby scripts as subprocesses and scraping their output
might have to be retooled if they start getting new warnings
that weren’t
there…?

Nice guess, but I don’t think that’s an issue… the
warnings seem to go to stderr, not stdout.

It was a guess.

How about:

output = ruby some_script 2>&1 # ?

“Anders Borch” spam@deck.dk schrieb im Newsbeitrag
news:3F168404.2060602@deck.dk…

Anders Borch wrote:

Brian Candler wrote:

Now, any file loaded with loadpp may contain the ++ operator, and it
will be replaced with ‘+= 1’. Have fun with it if you like :slight_smile:

It might be better to replace foo++ with (foo=foo.succ) because it
will work
on things other than numbers - except for some reason there doesn’t
seem to
be any ‘pred’, so you can’t implement foo-- in this way :frowning:

irb(main):003:0> “fred”.succ
=> “free”

true… I wouldn’t be able to implement that in as few lines, since
#succ does not modify the receiver the way +=1 and ++ does.

actually I was wrong… gsub solves this quite nicely…

def loadpp(l)
s = ‘’
f = File.new(l,‘r’)
f.each_line do |line|
s += line.gsub(/(\w)++)/,‘\1=\1.succ’) + “\n”
end
eval(s)
end

There’s an error in the regexp. It should read /(\w+)++)/ to be able to
work with identifiers that are longer than one char.

robert
···

On Thu, Jul 17, 2003 at 06:54:44PM +0900, Anders Borch wrote:

def loadpp(l)
s = ‘’
f = File.new(l,‘r’)
f.each_line do |line|
s += line.gsub(/(\w)++)/,‘\1=\1.succ’) + “\n”
end
eval(s)
end

I haven’t played with this at all… but let me
remind you that the ++ operator comes in both
pre and post forms.

Until these code fragments both work, I’d say
you haven’t actually implemented it, even as a
preprocessor kind of thing:

x = 5
y = x++
puts y # 5

y = ++x
puts y # 6

Cheers,
Hal

···

----- Original Message -----
From: “Anders Borch” spam@deck.dk
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, July 17, 2003 6:15 AM
Subject: Re: Keyword “with”


Hal Fulton
hal9000@hypermetrics.com

Hal E. Fulton wrote:

From: “Anders Borch” spam@deck.dk
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, July 17, 2003 6:15 AM
Subject: Re: Keyword “with”

def loadpp(l)
s = ‘’
f = File.new(l,‘r’)
f.each_line do |line|
s += line.gsub(/(\w)++)/,‘\1=\1.succ’) + “\n”
end
eval(s)
end

I haven’t played with this at all… but let me
remind you that the ++ operator comes in both
pre and post forms.

Until these code fragments both work, I’d say
you haven’t actually implemented it, even as a
preprocessor kind of thing:

x = 5
y = x++
puts y # 5

y = ++x
puts y # 6

Cheers,
Hal

I was merely showing a proof of concept (implementing pre-increment
since that was the easy one), but if you like, I shall implement both
pre- and postincrement. I saw the flaw you pointed out shortly after
posting, but saw no need to complicate my implementation unless someone
actually made the explicit distinction between the two forms of
incrementation.

I am personally against using these tricks in any serious form (remember
ruby goto in an earlier post?) or have you ever looked at the AND, OR,
NOT macros defined for C? I feel no need to change the language merely
for the sake of change. Especially when the change does not add to
readability. AND, OR, and NOT are not very readable IMHO there is a need
to explicitly specify && or & as logical or binary AND. The == operator
does not really add any functionality to ruby, but it would add
readability since a lot of programmers out there knows pre and post
increment.

/Anders

···

----- Original Message -----

Is there any better way than
++x => (begin x = x.succ; x end)
?

IIRC C doesn’t guarantee any evaluation order, so
a = x++ - x++
is undefined (can be -1, 0 or 1 depending on how things are done).

···

On Fri, Jul 18, 2003 at 02:59:00AM +0900, Anders Borch wrote:

I was merely showing a proof of concept (implementing pre-increment
since that was the easy one), but if you like, I shall implement both
pre- and postincrement. I saw the flaw you pointed out shortly after
posting, but saw no need to complicate my implementation unless someone
actually made the explicit distinction between the two forms of
incrementation.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Do you mean to say that I can read mail with vi too? :wink:
Didn’t you know that?
:r /var/spool/mail/jk
– debian-mentors

I was merely showing a proof of concept (implementing pre-increment
since that was the easy one), but if you like, I shall implement both
pre- and postincrement. I saw the flaw you pointed out shortly after
posting, but saw no need to complicate my implementation unless someone
actually made the explicit distinction between the two forms of
incrementation.

Don’t consider it a challenge. I’m sure it’s doable.

I am personally against using these tricks in any serious form (remember
ruby goto in an earlier post?)

:slight_smile:

or have you ever looked at the AND, OR,
NOT macros defined for C? I feel no need to change the language merely
for the sake of change. Especially when the change does not add to
readability. AND, OR, and NOT are not very readable IMHO there is a need
to explicitly specify && or & as logical or binary AND. The == operator
does not really add any functionality to ruby, but it would add
readability since a lot of programmers out there knows pre and post
increment.

True, it might help readability. But I don’t think
it will ever be in Ruby.

Hal

···

----- Original Message -----
From: “Anders Borch” spam@deck.dk
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, July 17, 2003 12:59 PM
Subject: Re: Keyword “with”


Hal Fulton
hal9000@hypermetrics.com

It’s even more insidious than that, IIRC:

x++ = i[x];

The value of x on the right hand side of that expression is undefined.
I’ve actually been bitten by this problem (in code written by other
people :). The value of x in this case can even change based on the
optimization level of the compiler!

Chris

···

On Thursday, July 17, 2003, at 12:17 PM, Mauricio Fernández wrote:

On Fri, Jul 18, 2003 at 02:59:00AM +0900, Anders Borch wrote:

I was merely showing a proof of concept (implementing pre-increment
since that was the easy one), but if you like, I shall implement both
pre- and postincrement. I saw the flaw you pointed out shortly after
posting, but saw no need to complicate my implementation unless
someone
actually made the explicit distinction between the two forms of
incrementation.

Is there any better way than
++x => (begin x = x.succ; x end)
?

IIRC C doesn’t guarantee any evaluation order, so
a = x++ - x++
is undefined (can be -1, 0 or 1 depending on how things are done.

It won’t help readability, as the construct x+=1 is rarely, if ever,
used in readable Ruby code. Manually incrementing variables is a
relic of previous-generation languages which require explicit loops
for iterations.

Since x+=1 is rarely used in readable code, if x++ were introduced to
Ruby, it would hardly make a difference.

Gavin

···

On Friday, July 18, 2003, 5:30:00 AM, Hal wrote:

The ++ operator does not really add any functionality to ruby, but
it would add readability since a lot of programmers out there
knows pre and post increment.

True, it might help readability. But I don’t think
it will ever be in Ruby.

Since x+=1 is rarely used in readable code, if x++ were introduced to
Ruby, it would hardly make a difference.

Ok, then I have a question.

Say I’m parsing a web page and have stuffed all the information into an
array. I find out that somehow the data that I parsed out of the web
page is screwy. The code that I previously had to deal with the
information I parsed looks like:

array_of_stuff.each {

element>
result = doSomethingWithElement(element)

}

So I decide to take a look at each element before I do something with
it:

array_of_stuff.each {

element>
if elementIsScrewy(element)
raise “Whoa, element is screwy! #{element.inspect}”
end
result = doSomethingWithElement(element)

}

So now it catches that screwy element, but I want to go look at the
source data to see why it’s creating that screwy element, so I want to
have an idea of how many elements into the page it is before there’s
something odd happening. Now I could change things so that I now use
Array#each_index but I’m just putting in code I’ll rip out once I
figure out what’s causing the error. Is there a better way of doing
this than setting a counter to zero outside the loop and incrementing
it inside the loop, like this:

counter = 1
array_of_stuff.each {

element>
if elementIsScrewy(element)
raise "Whoa, element is screwy! #{element.inspect}, " +
“look for the problem #{counter} elements into the page”
end
result = doSomethingWithElement(element)

counter += 1
}

···

On Saturday, July 19, 2003, at 03:08 AM, Gavin Sinclair wrote:

It won’t help readability, as the construct x+=1 is rarely, if ever,
used in readable Ruby code.

Out of curiosity, do you have any numbers to back this up? Or is your
assertion that “+= 1” makes code unreadable?

Ben Giddings wrote:

Is there a better way of doing this than
setting a counter to zero outside the loop and incrementing it inside
the loop, like this:

Perhaps Array#each_with_index?

array_of_stuff.each_With_index do | element, i |
if element.screwy?
raise "Whoa, element is screwy! #{element.inspect}, " +
“look for the problem #{i} elements into the page”
end
result = doSomethingWithElement(element)
end

HTH
alex

···

On Saturday, July 19, 2003, at 03:08 AM, Gavin Sinclair wrote:
__
alex fenton

counter = 1
array_of_stuff.each {

element>
if elementIsScrewy(element)
raise "Whoa, element is screwy! #{element.inspect}, " +
“look for the problem #{counter} elements into the page”
end
result = doSomethingWithElement(element)

counter += 1
}

First, I simply don’t agree with the statement that “x += 1” is a rarity
in readable code. But that’s not something we can measure without an
objective measure of readibility, so let’s move on.

However, to address the specific example, I would just use each_with_index:

counter = 0
array_of_stuff.each {
>element>
if elementIsScrewy(element)
raise "Whoa, element is screwy! #{element.inspect}, " +
“look for the problem #{counter} elements into the page”
end
result = doSomethingWithElement(element)

counter += 1
}

array_of_stuff.each_with_index {
>element, counter|
if elementIsScrewy(element)
raise "Whoa, element is screwy! #{element.inspect}, " +
“look for the problem #{counter} elements into the page”
end
result = doSomethingWithElement(element)

}

Neither, and certainly not the latter. It’s just that I rarely see
“+= 1” in any Ruby code, readable or otherwise. There may be some
programming domains where it’s needed, but they’re (I assert) rare.
Nearly every reason for using “x++” in C-like languages is redundant
in Ruby because of iterators, and rich data-processing methods in the
standard library.

Occasionally I count things explicitly. More often, I gather the data
I need and call #size. I certainly program in limited domains,
however.

Obviously, from the messages in this thread, it is in reasonably
common use. The files under /usr/local/lib/ruby/1.8 on my machine
yield 155 uses of /[±]= *1/, most of which, at a glance, appear
stylistically justified.

Gavin

···

On Monday, July 21, 2003, 10:29:20 PM, Michael wrote:

It won’t help readability, as the construct x+=1 is rarely, if ever,
used in readable Ruby code.

Out of curiosity, do you have any numbers to back this up? Or is your
assertion that “+= 1” makes code unreadable?