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

On Mon, Nov 9, 2009 at 11:15 AM, TonyMc [email protected] wrote:

Tony A. [email protected] writes:

I still find this most appropriate:

OK, that gets you killfiled. Getting ruby wrong is not so bad, you just
need to learn from the other posts in this thread. Getting real life
wrong is another, and much more serious.

Psst, it’s a joke, and a self-deprecating one at that.

That said, this thread is pretty much a dead horse at this point.

On Nov 9, 2009, at 12:28 PM, Tony A. wrote:

Psst, it’s a joke, and a self-deprecating one at that.

I too found it really offensive.

James Edward G. II

Tony A. [email protected] writes:

I still find this most appropriate:

OK, that gets you killfiled. Getting ruby wrong is not so bad, you just
need to learn from the other posts in this thread. Getting real life
wrong is another, and much more serious.

Tony

Rick Denatale wrote:

In French une langue denotes the same concept as tongue in English.
The primary meaning is that muscle in the middle of a mouth. It can
also mean a language

Okay, I knew that…

The French un Langage is first what is used by humans to express their
thoughts and sentiments by means of “articulations of the voice”, by
extension it also can mean written language, the ‘language’ of animals
(bird song, dog’s barking etc.) and finally a system of communication
by any means “The symbolic language of flowers.” “Pantomime is a
silent language.” these last being translated examples from Le
Dictionnaire.

By Jove! Getting lessons in my mother idiom (grin) on a Ruby forum. I
knew I was following you on Twitter for a reason.

On Mon, Nov 9, 2009 at 11:14 AM, Aldric G. [email protected]
wrote:

I don’t understand this block.
Aussi, si je puis me permettre - le Francais est une langue, et Ruby est
un langage (de programmation). Yet another one of those french
subtleties!

Actually I think the subtleties are similar between French and English
here

In French une langue denotes the same concept as tongue in English.
The primary meaning is that muscle in the middle of a mouth. It can
also mean a language, in the same sense that tongue can in English
phrases such as “His native tongue.” or “Speaking in tongues.”
Although such usage is more frequent in French than in English. This
is a secondary meaning though, from “Le Dictionnaire de l’Académie
Française”:

LANGUE désigne aussi l’Idiome d’une nation, d’une race

And the French word idiome (another slightly false cognate) means The
language of a Nation, or province.

The French un Langage is first what is used by humans to express their
thoughts and sentiments by means of “articulations of the voice”, by
extension it also can mean written language, the ‘language’ of animals
(bird song, dog’s barking etc.) and finally a system of communication
by any means “The symbolic language of flowers.” “Pantomime is a
silent language.” these last being translated examples from Le
Dictionnaire.


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

Just so you know I wasn’t dissing La langue belle. I try to maintain
a basic fluency. Not sure if my French or my C is more rusty!

On Mon, Nov 9, 2009 at 2:30 PM, Aldric G. [email protected]
wrote:

extension it also can mean written language, the ‘language’ of animals


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 Mon, Nov 9, 2009 at 2:48 PM, Aldric G. [email protected]
wrote:

Rick Denatale wrote:

Just so you know I wasn’t dissing La langue belle. I try to maintain
a basic fluency. Not sure if my French or my C is more rusty!

Hah! No worries, I wasn’t insulted.
So … Friend++ ?

Bien sur


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

Rick Denatale wrote:

Just so you know I wasn’t dissing La langue belle. I try to maintain
a basic fluency. Not sure if my French or my C is more rusty!

Hah! No worries, I wasn’t insulted.
So … Friend++ ?
Warning: incrementing a constant!

On Nov 9, 2009, at 6:35 AM, Seebs wrote:

And I am pretty sure that +=1 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 += 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”.

Cheers,

Josh

Joshua B. wrote:

But in general, basically, any place where we have a += 1, I’d prefer ++.

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.

On Nov 7, 4:39 am, “David A. Black” [email protected] wrote:

That’s because you’re trying to write C in Ruby. There are far more
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”]

Hi –

On Wed, 4 Nov 2009, RichardOnRails wrote:

(b) Dave Black, in his truly excellent “The Well-Grounded Rubyist”

def show(v)
“Got #{v}, class = #{v.class}, object_id = #{v.object_id}
(v.object_id-1)/2 = #{(v.object_id-1)/2 }”
end

a = 1; show (a) => Got 1; class = Fixnum; object_id = 3; v >> 1
= 1
b = 1; show (b) => Got 1; class = Fixnum; object_id = 3; v >> 1
= 1
a == b => true

And a.equal?(b) is also true.

Both “a” and “b” have their respective values held as immediate values
(embedded in their object_id’s), but they are NOT the same thing
because they’re ultimately held in their respective entries in the
symbol table.

But the symbol table, which contains :a and :b, is not the arbiter of
what constitutes the object 1. The object 1 exists independently of
the symbol table. You can create bindings between that object and one
or more identifiers, but you can do that with any object.

So, object_id’s for Fixnum’s are synthetic. To think
that those object_id’s point to a location in a memory area that
stores the 32-bit 000…001 (in a 32-bit machine/OS) is to contradict
the meaning of immediacy and defeat the very efficiency that immediate
values offer.

I don’t think object id’s exactly point anywhere. Objects happen to
have unique ids, all of which are synthetic in the sense that they
are, themselves, not the object.

  1. Now please consider the following supplement to the statements
    above:

a += 1; show (a) => Got 2; class = Fixnum; object_id = 5; v >> 1
= 2

For a += 1 you could substitute anything, like a = 300. It’s a
completely new assignment, and completely obliterates any previous
binding.

show (a) => Got 2; class = Fixnum; object_id = 5; v >> 1 = 2
show (b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1

The assignment of “a += 1” to “a” changed a’s object_id to embed a new
value: 2.

I’m not sure what you mean by the object_id embedding a value. I would
forget about object_id’s, and just look at the objects.

Despite that change of 1 to 2 in a’s object, “b” remains
set to 1. “b” did not suffer the calamity of a universal change of
all Fixnum 1’s to 2’s.

But there is no “a’s object” across multiple assignments. The
identifier a has absolutely no continuity of consciousness, so to
speak. It’s a reusable, disposable, ad hoc label that can be glued on
to any number of objects in succession. The identifier itself does not
maintain state, and does not “know” how many times it has been used.

a = “hi”
a = “bye”

The second line has absolutely no relation to, or dependency on, the
first. And a += 1 is, essentially, just shorthand for a = a + 1 (i.e.,
all the +=, -=, etc. operators are assignment operators).

So when you do a += 1, you’re explicitly saying that you are
discarding any previous binding of a, and creating a new one. Of
course, you can only use this shortcut if a has a + method. But it’s
an assignment nonetheless; the calling of the method is not, itself,
an assignment operation. You could equally do:

b = a + 1
a = b

  1. Conclusion:

a += 1 is equivalent to a++’s natural meaning. In fact, we further

“Natural”? :slight_smile:

show(b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1
it’ll work fine for non-positives, also… A compiler change to allow
“def ++” is necessary to finally add ++ to Fixnum.

I know we’ve been around the block a few times in this thread already,
but the bottom line is that method calls don’t affect local variable
bindings. The parser could presumably be trained to treat ++ as an
assignment operator, but Matz’s view of this has always been that he
doesn’t want assignment semantics to be delivered in something that
looks like a non-assignment idiom from other languages.

The most important example, I think, is this one:

str = “hi”
=> “hi”
str.object_id
=> 1309270
str.succ!
=> “hj”
str
=> “hj”
str.object_id
=> 1309270
a = 1
=> 1
a.object_id
=> 3
a.succ!
NoMethodError: undefined method `succ!’ for 1:Fixnum

You can’t increment a in place because the messages you send to a
(like: “Increment yourself in place!”) are actually being delivered
to an object, not an identifier.

In that sense, the fact that the identifier holds an immediate value
is actually of secondary interest. Even if it didn’t, you’d still be
sending a message to an object. The final destination of a message is
never an identifier; it’s always an object. So what you’re asking for
has to make complete sense in terms of the object.

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)

Seebs wrote:

On 2009-11-09, Tony A. [email protected] wrote:

The only reasons it’s impossible are cultural, not technical. If you think
there’s a valid technical reason why it’s “impossible” to implement perhaps
you’d care to state it.

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

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.

Hi –

On Thu, 5 Nov 2009, RichardOnRails wrote:

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? :slight_smile:

The fact that Robert K., 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 :slight_smile:

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 :slight_smile:

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 Nov 20, 2:48 am, Aldric G. [email protected] wrote:

Jicksta.com is for sale | HugeDomains
Well, with -that-, we definitely can add ‘++’ as syntactic sugar for
succ.

Phew! It was about time someone added to this thread! :wink:

On Thu, Nov 19, 2009 at 4:43 PM, Marnen Laibow-Koser
[email protected]wrote:

You mean thread++ ? :smiley:

NO! thread += 1

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

btw if anyone wants to discuss this in person I’m wandering around at
RubyConf :slight_smile:

Gavin S. wrote:

On Nov 20, 2:48�am, Aldric G. [email protected] wrote:

Jicksta.com is for sale | HugeDomains
Well, with -that-, we definitely can add ‘++’ as syntactic sugar for
succ.

Phew! It was about time someone added to this thread! :wink:

You mean thread++ ? :smiley:

Best,

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

Sebastian H. wrote:

Am Donnerstag 19 November 2009 16:48:52 schrieb Aldric G.:

Jicksta.com is for sale | HugeDomains
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.

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

Am Donnerstag 19 November 2009 16:48:52 schrieb Aldric G.:

Jicksta.com is for sale | HugeDomains
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.