Ruby doesn't implement x++ for Fixnum's because?

On Nov 5, 7:22 am, “Michael W. Ryder” [email protected] wrote:

which is not as clean as using puts i++.

Maybe you’ll just have to find another way to print the numbers 1 to
10 :wink:

On 05/11/2009, at 1:31 AM, Marnen Laibow-Koser wrote:

I believe you are quite wrong. If a destructive function like gsub!
can
be implemented as a method, then I see no reason that +=, |=, or
postfix
++ couldn’t be.

It can be done, if you are willing to make your numbers mutable:

class MutableNum
def initialize n
@n = n
end

def pp
@n += 1
@n - 1
end

def method_missing symbol, *args
@n.method(symbol).call(*args)
end

def to_s
@n.to_s
end
end

a = MutableNum.new 1
puts a.pp #=> 1
puts a #=> 2

Having said that, I agree with others that the post-increment operator
is not needed in Ruby at all.

Hi –

On Thu, 5 Nov 2009, Tony A. wrote:

On Wed, Nov 4, 2009 at 2:41 PM, Marnen Laibow-Koser [email protected]wrote:

What semantics do you intend here? I’m not sure I understand.

You can think of it like:

alias_method :++, :increment!

But what would it mean to send the message increment! to, say, the
object 10? In the same vein:

10.succ!
NoMethodError: undefined method `succ!’ for 10:Fixnum

David


The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

David A. Black wrote:

But what would it mean to send the message increment! to, say, the
object 10? In the same vein:

10.succ!
NoMethodError: undefined method `succ!’ for 10:Fixnum

I agree with David… At which point are we completely violating the
principle of least surprise?
A number is a number is a number. It took me all of half an hour to
forget about ++ and I haven’t looked back since. I -like- typing +=1
because it, simply put, makes sense.

Hi–

On Fri, 6 Nov 2009, Tony A. wrote:

def inc
incrementing_logic_goes_here
end

How is that any different?

What’s wrong with Array#push? Why do we need Array#<<? How is that any
different?

irb(main):001:0> [].push(1,2,3)
=> [1, 2, 3]
irb(main):002:0> [].<<(1,2,3)
ArgumentError: wrong number of arguments (3 for 1)

:slight_smile: But I know that’s not what you meant. The thing is, a method
called ++ that did in-place incremention would not be meaningful for
numbers (if I understand correctly that you mean it would be similar
to succ!), and having it for other objects would probably just lead to
more confusion. That’s my hunch, at least.

David


The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

On Wed, Nov 4, 2009 at 11:55 AM, Seebs [email protected] wrote:

Hmph. Fortran can change constants, why’s Ruby so much less powerful?

But Fortran fixed that bug in its first revision!


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

On Thu, Nov 5, 2009 at 3:40 AM, Gavin S. [email protected]
wrote:

end

How is that any different?

What’s wrong with Array#push? Why do we need Array#<<? How is that any
different?

What’s wrong with Array#push? Why do we need Array#<<? How is that any
different?

Apart from David’s response, you’re proposing that a currently-invalid
method name be allowed. For such a change, there needs to be a good
reason.

Gavin

On Thu, Nov 5, 2009 at 5:28 PM, David A. Black [email protected]
wrote:

The thing is, a method called ++ that did in-place incremention would not
be meaningful for
numbers (if I understand correctly that you mean it would be similar
to succ!), and having it for other objects would probably just lead to
more confusion. That’s my hunch, at least.

There’s no point at all if it doesn’t work on numbers.

It would require special case behavior. Application to literal numbers
would be strange. But there’s certainly no reason it can’t be done, and
you
have one Ruby implementer on this thread attesting that it can.

On 2009-11-06, David A. Black [email protected] wrote:

It seems like a lot of special-casing and strangeness, though. I’m a
little bit hampered in discussing it, I guess, because I don’t see
what benefit it would confer in exchange for the anomaly. So I’m
probably going in circles.

I have found a lot of idioms which are amenable to use with ++,
especially
postincrement, but they are often not nearly so useful outside of the
C-like
languages.

Consider the canonical inner loop for copying an array in C:
s[i++] = t[j++];

There’s really no idiomatic equivalent – nor a need for one, usually.

-s

Hi –

On Sat, 7 Nov 2009, Seebs wrote:

Consider the canonical inner loop for copying an array in C:
s[i++] = t[j++];

There’s really no idiomatic equivalent – nor a need for one, usually.

Oh, I have no problem with ++ per se. It just seems against the grain
in Ruby.

David


The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

Walton H. wrote:

-----Original Message-----
From: [email protected] [mailto:[email protected]]

Now consider the ruby way:

10.times do |i|
print “#{i},”
end

Some length as the C code, but much more readable. Heck, it’s
almost English!

But if you wanted to do something like:
i = 10;
while (i > 0)
{
printf(“%d/n”, i–);
}
in Ruby you would have to do something like:
i = 10
while (i > 0)
puts i
i -= 1
end
As far as I can tell there is no way in Ruby to use .each or .times to
go backwards. While I realize this thread is about the ++ operator the
– operator is complementary.

Michael W. Ryder wrote:
[…]

But if you wanted to do something like:
i = 10;
while (i > 0)
{
printf(“%d/n”, i–);
}
in Ruby you would have to do something like:
i = 10
while (i > 0)
puts i
i -= 1
end

No.

10.downto(1) do |i|
puts i
end

As far as I can tell there is no way in Ruby to use .each or .times to
go backwards.

That’s what .downto is for. (Have you ever needed this? I have not.)

While I realize this thread is about the ++ operator the
– operator is complementary.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hi –

On Sat, 7 Nov 2009, Tony A. wrote:

On Thu, Nov 5, 2009 at 5:28 PM, David A. Black [email protected] wrote:

The thing is, a method called ++ that did in-place incremention would not
be meaningful for
numbers (if I understand correctly that you mean it would be similar
to succ!), and having it for other objects would probably just lead to
more confusion. That’s my hunch, at least.

There’s no point at all if it doesn’t work on numbers.

It depends how you define “work” :slight_smile: I’ll stick with my formulation,
though: in-place incrementation of a numeric is not meaningful. So if
++ is understood to be in-place incrementation (like succ!), which is
how I interpreted your earlier post, then it wouldn’t be meaningful
for numbers.

It would require special case behavior. Application to literal numbers
would be strange. But there’s certainly no reason it can’t be done, and you
have one Ruby implementer on this thread attesting that it can.

It seems like a lot of special-casing and strangeness, though. I’m a
little bit hampered in discussing it, I guess, because I don’t see
what benefit it would confer in exchange for the anomaly. So I’m
probably going in circles.

David


The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

Hi –

On Sat, 7 Nov 2009, Marnen Laibow-Koser wrote:

while (i > 0)
As far as I can tell there is no way in Ruby to use .each or .times to
go backwards.

That’s what .downto is for. (Have you ever needed this? I have not.)

And in 1.9:

puts *10.downto(1)

and there’s also #reverse_each.

David


The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

On 2009-11-06, David A. Black [email protected] wrote:

Oh, I have no problem with ++ per se. It just seems against the grain
in Ruby.

Yeah. I think fundamentally it’s the “variables aren’t objects” thing.
++ is intended to operate on a specific object, and doesn’t make sense
if “x = x + 1” doesn’t really mean “modify the specific object x”.

-s

On Sat, 7 Nov 2009, Michael W. Ryder wrote:

almost English!
puts i
i -= 1
end
As far as I can tell there is no way in Ruby to use .each or .times to go
backwards. While I realize this thread is about the ++ operator the –
operator is complementary.

what about the downto method?

Matt

Marnen Laibow-Koser wrote:

while (i > 0)
As far as I can tell there is no way in Ruby to use .each or .times to
go backwards.

That’s what .downto is for. (Have you ever needed this? I have not.)

I missed the downto method, I guess that is a problem when you have so
many different ways to do the same basic things. I much prefer the
simplicity of Basic and C with for loops that can go either direction.
As far as going backwards I use it a lot to parse strings of the form
“city name ST 12345-6789” to City, State, and Zip Code fields. I look
for the first blank from the end of the string and assume everything
after it is the Zip Code, I then find the next two non-blank characters
and assign them to State, and everything else is the City name.

Hi –

On Sat, 7 Nov 2009, Michael W. Ryder wrote:

i = 10

12345-6789" to City, State, and Zip Code fields. I look for the first blank
from the end of the string and assume everything after it is the Zip Code, I
then find the next two non-blank characters and assign them to State, and
everything else is the City name.

I think one thing that’s getting lost in the sauce here is that Ruby
does have idioms like:

a -= 1

for bumping things up and down and other operations. So you can
maintain a manual index on a collection or string traversal easily if
you need to. I’d say that most of the time, though, you won’t need to.

David


The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

while (i > 0)
puts i
i -= 1
end
As far as I can tell there is no way in Ruby to use .each or .times to
go backwards. While I realize this thread is about the ++ operator the
– operator is complementary.

Others have mentioned reverse each and downto, so I’ll just throw in one
more. If you are determined to save that line, you also have:
(untested, so if I’m off my one, from the C feel free to tar and feather
me)

i=11
while (i-=1) > 0
puts i
end

or:

i=11;
while i > 0
puts (i-=1)
end

I also think this demonstrates my previous point, that playing Perl golf
serves no one. I’ve found it rare that saving a line at the cost of
readability improves code in any way. If the interpreter is any good,
your 5 line example should execute just as fast as my 4 line, and it’s
certainly much clearer what is actually being done. I’ve seen plenty of
C programmers who really should know better, get confused by ++ in
unexpected places.

What it all comes down to in my personal opinion (as no one of any real
note)
is that while ++ can be a cool little operator, it really doesn’t give
enough
benefit to be worth the confusion involved in implementing it in Ruby.
It’s
been made plenty clear by others why ++ would have to be a special case
operator
in Ruby (as opposed to the other operators which are simple methods),
but why
write special logic for this one silly operator that at BEST saves us
one line
of code here and there?