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

J

Joshua Ballanco

=20
Huh! That is prettier. Didn't know it existed.
=20
But in general, basically, any place where we have a +=3D 1, I'd = prefer ++.
=20
And I am pretty sure that +=3D1 is occasionally useful.

...and *this* is where, in the long long history of this thread, where I =
finally realized the *real* issue here.

Let's take Rails... rails, activerecord, activeresource, actionpack, =
actionmailer, and activesupport...

106,518 lines of Ruby code

66 instances of foo +=3D 1 (or foo +=3D 100)

That's 0.06% of the lines of code! If you're writing Ruby code and your =
fraction is higher, chances are you're "doing it wrong".

Cheers,

Josh=
 
T

t3ch.dude

Hi --



Michael W. Ryder wrote:
[...]
.  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.
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+/)
irb(main):004:0> str = "Washington Court House OH 43160"
=> "Washington Court House OH 43160"
irb(main):005:0> zip, state, city = str.reverse.split(/ /,3).map {|e|
e.reverse}
=> ["43160", "OH", "Washington Court House"]
 
M

Michael W. Ryder

Joshua said:
..and *this* is where, in the long long history of this thread, where I finally realized the *real* issue here.

Let's take Rails... rails, activerecord, activeresource, actionpack, actionmailer, and activesupport...

106,518 lines of Ruby code

66 instances of foo += 1 (or foo += 100)

That's 0.06% of the lines of code! If you're writing Ruby code and your fraction is higher, chances are you're "doing it wrong".
Or your style of programming is different than the programmer(s) in the
code you researched. As an example earlier in the thread I showed one
way to separate the city, state, and zip code from a string, at least
two other authors showed totally different methods. They all returned
the same results so which one was right? One used regular expressions
and the other used split and map. x = x + 1 returns the same result as
x += 1 but is probably easier for some people to understand quickly.
 
D

David A. Black

Hi --

Hi David,

First, Thank you for The Well-Grounded Rubyist. I study like other
pour over scriptures or the Koran. Your topics are well chose,
beautifully explicated. And Manning adding typesetting that enhanced
the your work.
Thanks!

I started this thread because some of your comments on page 54, e.g.
"The un-reference ..." were a blemish among your excellent analyses.

Oh dear -- the whole thread is my fault? :)
The fact that Robert Klemme, whom I also respect highly as a Rubyist,
agrees with you gives me pause.

But nevertheless, I maintain that my corrected post of today refutes
such claims as "... any object that's represented as an immediate
value is always the same object."

I'm afraid I don't see the refutation, but as per my previous post,
the immediate value thing is only part of the picture.
Russel & Whitehead dealt with this
kind of issue perhaps a century ago when the defined the first Natural
Number, 1, as "the set of all sets that are in one-to-one
correspondence with the set containing the Null Set." Plato dealt with
this in The Parable of the Caves" with the claim that allegedly
concrete things were merely reflections of the "real" objects.

Well... any given system of symbolic representation may or may not
take a Platonic view of things. Plato's allegory is of course of great
importance in the history of thought, but it doesn't really dictate
that 2000+ years later, there can't be a computer language with
identifiers housing immediate values :)
I'm not clamoring for a Ruby implementation. I only posted my
analysis on this issue to get other people's opinions. And I find it
hard compose a mistake free exposition, e.g. the last code lines in
yesterday evening's post:

a = 2**30-1; show (a) => Got 1073741823; class = Fixnum; object_id
= 2147483647; v >> 1 = 1073741823
a = 2**30; show (a) => Got 1073741824; class = Bignum; object_id =
22737670; v >> 1 = 11368835

should have read:

a = 2**30-1; show (a) => Got 1073741823; class = Fixnum; object_id
= 2147483647; v >> 1 = 1073741823
show(a.pp) => Got 1073741824; class = Bignum; object_id =
22738520; v >> 1 = 11369260 # Of course, "v >> 1" is irrelevant
here

to make the point that "pp" crossed the Fixnum/Bignum boundary
smoothly.

You'll see a lot of people cutting-and-pasting entire shell sessions,
like this:

$ cat myfile.rb
... code here ...

$ ruby myfile.rb
... output here ...

which is a good way to ensure that your output is from your input. I
rely a lot (depending on the example) on pasting things in and out of
irb.
Bottom line: Please keep up you great work! I appreciate it very
much!

I'll do me best :)


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)
 
A

Aldric Giacomoni

Seebs said:
It's impossible to implement as a method. You could introduce it as
syntactic sugar, but it's not so clear that this would be worth the
trouble. In particular, incrementing is inefficient in Ruby because
it would involve creating many new objects to iterate.

-s

http://jicksta.com/posts/superators-add-new-operators-to-ruby
Well, with -that-, we definitely can add '++' as syntactic sugar for
succ.

I'm sorry? I'm beating a what? I can't hear you, there's too many flies
buzzing around the carcass.
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

You mean thread++ ? :D

NO! thread += 1

thread++ isn't Ruby-like. DUH!

btw if anyone wants to discuss this in person I'm wandering around at
RubyConf :)
 
S

Sebastian Hungerecker

Am Donnerstag 19 November 2009 16:48:52 schrieb Aldric Giacomoni:
http://jicksta.com/posts/superators-add-new-operators-to-ruby
Well, with -that-, we definitely can add '++' as syntactic sugar for
succ.

But what good would that do except confuse people?

If x++ were equivalent to x.succ neither x++; p x nor p(x++) would behave
as expected (were "as expected" means "like they do in other languages").

For x++ to behave as expected, it would have to be sugar for x.succ! (and
x.succ! would have to return x's previous value), which, as has been pointed
out, doesn't and can't exist.
 
A

Aldric Giacomoni

Sebastian said:
Am Donnerstag 19 November 2009 16:48:52 schrieb Aldric Giacomoni:

But what good would that do except confuse people?

If x++ were equivalent to x.succ neither x++; p x nor p(x++) would
behave
as expected (were "as expected" means "like they do in other
languages").

For x++ to behave as expected, it would have to be sugar for x.succ!
(and
x.succ! would have to return x's previous value), which, as has been
pointed
out, doesn't and can't exist.

Hey. You see that horse on the ground? It's dead. Dead dead dead. D-E-D,
dead. I was just making a joke :)
 

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