From: Rick DeNatale Date: 2009-11-01T06:29:49+09:00 Subject: Re: Ruby doesn't implement x++ for Fixnum's because ??? On Sat, Oct 31, 2009 at 1:35 PM, Simon Krahnke wrote: > * Robert Klemme (23:19) schrieb: > >> Yes - and in C++ you can have it otherwise: >> >> robert@fussel:~$ g++ x.cc && ./a.out >> a++=1 >> a=2 >> b=2 >> robert@fussel:~$ cat x.cc >> >> #include >> >> int main( int argc, const char* argv[] ) >> { >>   int a = 1; >>   int &b = a; >>   printf("a++=%d\n", a++); >>   printf("a=%d\n", a); >>   printf("b=%d\n", b); >>   return 0; >> } > > No, you can't: > > $ ./ab > a: 2 @ 0xbfe413f8 > b: 2 @ 0xbfe413f8 > > $ cat ab.cc > #include > > int main(void) { >  int a = 1; >  int &b = a; >  a++; >  std::cout << "a: " << a << " @ " << &a << std::endl; >  std::cout << "b: " << b << " @ " << &b << std::endl; >  return 0; > } > > There is only one variable, b is just another name for it. I'm not going to run the experiment, but I believe that if the int &b = a; were changed to int b = a; Then the result would be the same as in Robert's 'surprising' ruby example. Which only points out the different in meaning between a C 'reference' variable, which aliases another variable, and the concept of a ruby variable as a reference to an object. And no, I don't think that adding syntactic sugar to Ruby to make a++ the same as either a += 1 or a = a.succ is a good idea. As a matter of fact, neither of this is REALLY the same as a++, since things like b = a++ should assign the ORIGINAL value of a to b, as opposed to b = ++a Things like post and pre increment/decrement operators in C like languages are best reserved to languages with the same basic structure. To my mind they are akin to the different rules for gender, and agreement in different families of languages. -- 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