 |
|
| Computers Forum Index » Computer Languages (Ruby) » Ruby doesn't implement x++ for Fixnum's because ???... |
|
Page 2 of 3 Goto page Previous 1, 2, 3 Next |
|
| Author |
Message |
| Marnen Laibow-Koser... |
Posted: Sat Nov 07, 2009 2:34 am |
|
|
|
Guest
|
Michael W. Ryder wrote:
[...]
Quote: 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
Quote: 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.)
Quote: While I realize this thread is about the ++ operator the
-- operator is complementary.
snip
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen at (no spam) marnen.org
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Matthew K. Williams... |
Posted: Sat Nov 07, 2009 2:37 am |
|
|
|
Guest
|
On Sat, 7 Nov 2009, Michael W. Ryder wrote:
Quote: Walton Hoops wrote:
-----Original Message-----
From: marnen at (no spam) marnen.org [mailto:marnen at (no spam) marnen.org]
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.
what about the downto method?
Matt |
|
|
| Back to top |
|
|
|
| David A. Black... |
Posted: Sat Nov 07, 2009 2:41 am |
|
|
|
Guest
|
Hi --
On Sat, 7 Nov 2009, Marnen Laibow-Koser wrote:
Quote: 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.)
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) |
|
|
| Back to top |
|
|
|
| Seebs... |
Posted: Sat Nov 07, 2009 4:25 am |
|
|
|
Guest
|
On 2009-11-06, David A. Black <dblack at (no spam) rubypal.com> wrote:
Quote: 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
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam at (no spam) seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! |
|
|
| Back to top |
|
|
|
| Michael W. Ryder... |
Posted: Sat Nov 07, 2009 4:47 am |
|
|
|
Guest
|
Marnen Laibow-Koser wrote:
Quote: 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.)
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.
Quote: While I realize this thread is about the ++ operator the
-- operator is complementary.
snip
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen at (no spam) marnen.org |
|
|
| Back to top |
|
|
|
| David A. Black... |
Posted: Sat Nov 07, 2009 5:41 am |
|
|
|
Guest
|
Hi --
On Sat, 7 Nov 2009, Michael W. Ryder wrote:
Quote: Marnen Laibow-Koser wrote:
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.)
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.
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) |
|
|
| Back to top |
|
|
|
| Walton Hoops... |
Posted: Sat Nov 07, 2009 6:17 am |
|
|
|
Guest
|
Quote: -----Original Message-----
From: Michael W. Ryder [mailto:_mwryder55 at (no spam) gmail.com]
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.
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? |
|
|
| Back to top |
|
|
|
| Marnen Laibow-Koser... |
Posted: Sat Nov 07, 2009 6:17 am |
|
|
|
Guest
|
Michael W. Ryder wrote:
[...]
Quote: . I much prefer the
simplicity of Basic and C with for loops that can go either direction.
That's because you're trying to write C in Ruby. There are far more
idiomatic ways of doing things -- and they *are* clearer, at least in a
Ruby context.
Quote: 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.
That's great in a language like C that doesn't have very good string
handling. The Ruby way to do this would be
city, state, zip = string.split(/\s+/)
No incrementing. No iteration. Just a clear declarative syntax.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen at (no spam) marnen.org
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Phrogz... |
Posted: Mon Nov 09, 2009 3:37 am |
|
|
|
Guest
|
On Nov 8, 12:37 pm, Tony Arcieri <t... at (no spam) medioh.com> wrote:
Quote: On Sun, Nov 8, 2009 at 12:32 PM, Tony Arcieri <t... at (no spam) medioh.com> wrote:
I cannot begin to answer this question because Ruby is doing strange and
unexpected things here, at least from my perspective...
Never mind, bar= was still defined because I was reopening the class.
Dude, you just about gave me a heart attack. Phewsh - sanity is
restored. |
|
|
| Back to top |
|
|
|
| Tony Arcieri... |
Posted: Mon Nov 09, 2009 3:44 am |
|
|
|
Guest
|
On Sun, Nov 8, 2009 at 2:30 PM, Florian Aßmann <florian.assmann at (no spam) email.de>wrote:
Quote: Gaah, close this thread! ;)
I suppose the whole discussion is moot as Ruby will likely never see a ++
operator.
I was just trying to make clear the limitation wasn't a technical one, and
further show how a ++ operator could be "Ruby-like" while still retaining
C/C++/Java-like semantics.
--
Tony Arcieri
Medioh/Nagravision |
|
|
| Back to top |
|
|
|
| Gavin Sinclair... |
Posted: Mon Nov 09, 2009 3:48 am |
|
|
|
Guest
|
On Nov 9, 9:44 am, Tony Arcieri <t... at (no spam) medioh.com> wrote:
Quote:
I suppose the whole discussion is moot as Ruby will likely never see a ++
operator.
I was just trying to make clear the limitation wasn't a technical one, and
further show how a ++ operator could be "Ruby-like" while still retaining
C/C++/Java-like semantics.
If implementing ++ requires changes to the parser, that seems like a
pretty technical limitation to me! :)
Matter of interpretation?
BTW in all your posts on the topic, you don't seem to address pre-
increment vs post-incrememt. (Forgive me if I'm wrong.) If Ruby
implemented ++ and didn't address that, it wouldn't be C or C++
semantics at all.
--
Gavin Sinclair |
|
|
| Back to top |
|
|
|
| Marnen Laibow-Koser... |
Posted: Mon Nov 09, 2009 3:49 am |
|
|
|
Guest
|
Tony Arcieri wrote:
Quote: On Sun, Nov 8, 2009 at 2:30 PM, Florian Aßmann
florian.assmann at (no spam) email.de>wrote:
Gaah, close this thread! ;)
I suppose the whole discussion is moot as Ruby will likely never see a
++
operator.
I was just trying to make clear the limitation wasn't a technical one,
and
further show how a ++ operator could be "Ruby-like" while still
retaining
C/C++/Java-like semantics.
And as others have made clear, you are incorrect.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen at (no spam) marnen.org
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Tony Arcieri... |
Posted: Mon Nov 09, 2009 6:17 am |
|
|
|
Guest
|
[Note: parts of this message were removed to make it a legal post.]
On Sun, Nov 8, 2009 at 8:40 PM, Phrogz <phrogz at (no spam) mac.com> wrote:
Quote: Dude, you just about gave me a heart attack. Phewsh - sanity is
restored.
My bad, it will serve as a reminder to double check my work before posting
:)
--
Tony Arcieri
Medioh/Nagravision |
|
|
| Back to top |
|
|
|
| Joel VanderWerf... |
Posted: Mon Nov 09, 2009 6:17 am |
|
|
|
Guest
|
Tony Arcieri wrote:
Quote: BTW in all your posts on the topic, you don't seem to address pre-
increment vs post-incrememt. (Forgive me if I'm wrong.) If Ruby
implemented ++ and didn't address that, it wouldn't be C or C++
semantics at all.
That's a can of worms I've been trying to avoid, as there are lexing/parsing
ambiguities surrounding the combination of both. How do you parse a+++b vs
a++++b vs a+++++b?
The only sane answer is to do what C does, as far as parsing. But the
deeper problem in ruby is what do pre- and post- really mean? In C there
are expressions and there are other constructs, so we can define natural
boundaries for pre and post in terms of expressions. In ruby we have
mostly expressions. So the hard questions are "before what?" and "after
what?"
Does
foo {x++}
yield the incremented x to foo, or the original x? What if foo yields
several times? Increment once, or once per iteration?
What about
x = 0
y = case; when true; x++; end
Is y 0 or 1?
It's kind of an ink-blot test for what you think "expression" means, but
of course everything is an expression in ruby.
It's not an answer to say
'x++' should have the same effect as 'x+=1'
because that's not even true in C.
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 |
|
|
| Back to top |
|
|
|
| Tony Arcieri... |
Posted: Mon Nov 09, 2009 6:17 am |
|
|
|
Guest
|
[Note: parts of this message were removed to make it a legal post.]
On Sun, Nov 8, 2009 at 8:50 PM, Gavin Sinclair <gsinclair at (no spam) gmail.com> wrote:
Quote: If implementing ++ requires changes to the parser, that seems like a
pretty technical limitation to me! :)
Assuredly it would require changes to the parser, as "++" presently
lexes/parses as "plus (unary+ value)", and that's not to mention how it
parses in method definitions. The resulting operation is equivalent to
binary +, unless you're using something like
Methodphitamine<http://jicksta.com/posts/the-methodphitamine>
Quote: BTW in all your posts on the topic, you don't seem to address pre-
increment vs post-incrememt. (Forgive me if I'm wrong.) If Ruby
implemented ++ and didn't address that, it wouldn't be C or C++
semantics at all.
That's a can of worms I've been trying to avoid, as there are lexing/parsing
ambiguities surrounding the combination of both. How do you parse a+++b vs
a++++b vs a+++++b?
--
Tony Arcieri
Medioh/Nagravision |
|
|
| Back to top |
|
|
|
|
|
All times are GMT
The time now is Sat Nov 28, 2009 9:26 am
|
|