# Syntax

**URL:** <https://rubytalk.org/t/syntax/5361>\
**Category:** ruby-talk\
**Created:** [26 March 2003 20:29 UTC](https://rubytalk.org/t/syntax/5361 "2003-03-26T20:29:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel11](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@daniel11](https://rubytalk.org/u/daniel11)\
**Post date:** [26 March 2003 20:29 UTC](https://rubytalk.org/t/syntax/5361/1 "2003-03-26T20:29:40Z")

</div>

hello,

in c++ I can write `i++´´. The same in ruby:`i=i+1´´.

Is there a better (shorter) way to increase a variable in ruby?

daniel

---

<div class="post-metadata">

**Author:** ![Paul\_Duncan](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@Paul\_Duncan](https://rubytalk.org/u/Paul_Duncan)\
**Post date:** [26 March 2003 20:34 UTC](https://rubytalk.org/t/syntax/5361/2 "2003-03-26T20:34:24Z")

</div>

> hello,
> 
> in c++ I can write `i++´´. The same in ruby: `i=i+1´´.
> 
> Is there a better (shorter) way to increase a variable in ruby?

i += 1

> **···**
>
> - daniel (offstuff@aon.at) wrote:
> 
> > daniel
> 
> –  
> Paul Duncan [pabs@pablotron.org](mailto:pabs@pablotron.org) pabs in #gah (OPN IRC)  
> [http://www.pablotron.org/](http://www.pablotron.org/) OpenPGP Key ID: 0x82C29562

---

<div class="post-metadata">

**Author:** ![Michael\_Campbell1](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Michael\_Campbell1](https://rubytalk.org/u/Michael_Campbell1)\
**Post date:** [26 March 2003 20:35 UTC](https://rubytalk.org/t/syntax/5361/3 "2003-03-26T20:35:54Z")

</div>

i += 1

> **···**
>
> — daniel [offstuff@aon.at](mailto:offstuff@aon.at) wrote:
> 
> > hello,
> > 
> > in c++ I can write `i++´´. The same in ruby: `i=i+1´´.
> > 
> > Is there a better (shorter) way to increase a variable in ruby?
> 
> * * *
> 
> Do you Yahoo!?  
> Yahoo! Platinum - Watch CBS’ NCAA March Madness, live on your desktop!  
> [http://platinum.yahoo.com](http://platinum.yahoo.com)

---

<div class="post-metadata">

**Author:** ![Stephan\_Kamper3](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stephan\_Kamper3](https://rubytalk.org/u/Stephan_Kamper3)\
**Post date:** [26 March 2003 20:49 UTC](https://rubytalk.org/t/syntax/5361/4 "2003-03-26T20:49:48Z")

</div>

daniel wrote:

> hello,
> 
> in c++ I can write `i++´´. The same in ruby: `i=i+1´´.

Hi Daniel,

how about

i += 1 ?

And, no there’s no way to do i++ in Ruby. For a discussion search this  
news group (resp. mailing list).

Happy programming

Stephan

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [26 March 2003 22:09 UTC](https://rubytalk.org/t/syntax/5361/5 "2003-03-26T22:09:59Z")

</div>

class Fixnum  
def inc(val=1)  
self += val  
end  
end

1.inc

🙂

“Stephan Kämper” [Stephan.Kaemper@Schleswig-Holstein.de](mailto:Stephan.Kaemper@Schleswig-Holstein.de) schrieb im  
Newsbeitrag [news:b5t2t0$2cuq4p$1@ID-96582.news.dfncis.de](mailto:news:b5t2t0$2cuq4p$1@ID-96582.news.dfncis.de)…

> **···**
>
> > daniel wrote:
> > 
> > > hello,
> > > 
> > > in c++ I can write `i++´´. The same in ruby: `i=i+1´´.
> > 
> > Hi Daniel,
> > 
> > how about
> > 
> > i += 1 ?
> > 
> > And, no there’s no way to do i++ in Ruby. For a discussion search this  
> > news group (resp. mailing list).
> > 
> > Happy programming
> > 
> > Stephan

---

<div class="post-metadata">

**Author:** ![Bermejo\_Rodrigo](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@Bermejo\_Rodrigo](https://rubytalk.org/u/Bermejo_Rodrigo)\
**Post date:** [26 March 2003 22:15 UTC](https://rubytalk.org/t/syntax/5361/6 "2003-03-26T22:15:36Z")

</div>

irb(main):006:0\> class Fixnum  
irb(main):007:1\> def inc(val=1)  
irb(main):008:2\> self += val  
irb(main):009:2\> end  
irb(main):010:1\> end  
SyntaxError: compile error  
(irb):8: Can’t change the value of self  
self += val  
^  
from (irb):10  
irb(main):011:0\> ^C  
(%:~)- ruby -v  
ruby 1.6.7 (2002-03-19) [i386-linux]

Robert wrote:

> **···**
>
> > class Fixnum  
> > def inc(val=1)  
> > self += val  
> > end  
> > end
> > 
> > 1.inc
> > 
> > 🙂
> > 
> > “Stephan Kämper” [Stephan.Kaemper@Schleswig-Holstein.de](mailto:Stephan.Kaemper@Schleswig-Holstein.de) schrieb im  
> > Newsbeitrag [news:b5t2t0$2cuq4p$1@ID-96582.news.dfncis.de](mailto:news:b5t2t0$2cuq4p$1@ID-96582.news.dfncis.de)…
> > 
> > > daniel wrote:
> > > 
> > > > hello,
> > > > 
> > > > in c++ I can write `i++´´. The same in ruby: `i=i+1´´.
> > > 
> > > Hi Daniel,
> > > 
> > > how about
> > > 
> > > i += 1 ?
> > > 
> > > And, no there’s no way to do i++ in Ruby. For a discussion search this  
> > > news group (resp. mailing list).
> > > 
> > > Happy programming
> > > 
> > > Stephan

---

<div class="post-metadata">

**Author:** ![Daniel\_Carrera](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Daniel\_Carrera](https://rubytalk.org/u/Daniel_Carrera)\
**Post date:** [26 March 2003 23:10 UTC](https://rubytalk.org/t/syntax/5361/7 "2003-03-26T23:10:48Z")

</div>

That doesn’t work, but ‘1.succ’ does.

> **···**
>
> On Thu, Mar 27, 2003 at 07:09:59AM +0900, Robert wrote:
> 
> > class Fixnum  
> > def inc(val=1)  
> > self += val  
> > end  
> > end
> > 
> > 1.inc
> > 
> > 🙂
> 
> ## – Daniel Carrera Graduate Teaching Assistant. Math Dept. University of Maryland. (301) 405-5137
> 
> en banc: en banc (ahn-BAHNK) adjective, adverb  
> Having all the judges of a court present in a hearing.

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [26 March 2003 23:02 UTC](https://rubytalk.org/t/syntax/5361/8 "2003-03-26T23:02:20Z")

</div>

Rodrigo,

I think Robert was being facetious.

This can’t work for a variety of reasons.

Robert’s point (I think) was: What do you  
get if you increment a constant? It’s surely  
wrong to change all values of 1 to something  
else.

For example, if you actually _could_ transform  
the object 1 into the object 2, then this code  
fragment

x = 1  
y = 1.inc  
puts x

would print 2 rather than 1.

The lack of a ++ operator (and the rationale  
behind it) has been discussed many times over  
the last three years or so.

If it’s not in the FAQ at  
[http://www.rubygarden.org/iowa/faqtotum](http://www.rubygarden.org/iowa/faqtotum) –  
well, it should be. 🙂

And you can also search the list archives at  
[www.ruby-talk.org](http://www.ruby-talk.org) (search for ++ or increment).

Cheers,  
Hal

> **···**
>
> ----- Original Message -----  
> From: [rodrigo.bermejo@ps.ge.com](mailto:rodrigo.bermejo@ps.ge.com)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Sent: Wednesday, March 26, 2003 4:15 PM  
> Subject: Re: syntax
> 
> > irb(main):006:0\> class Fixnum  
> > irb(main):007:1\> def inc(val=1)  
> > irb(main):008:2\> self += val  
> > irb(main):009:2\> end  
> > irb(main):010:1\> end  
> > SyntaxError: compile error  
> > (irb):8: Can’t change the value of self  
> > self += val  
> > ^  
> > from (irb):10  
> > irb(main):011:0\> ^C  
> > (%:~)- ruby -v  
> > ruby 1.6.7 (2002-03-19) [i386-linux]
> > 
> > Robert wrote:
> > 
> > > class Fixnum  
> > > def inc(val=1)  
> > > self += val  
> > > end  
> > > end
> > > 
> > > 1.inc
> > > 
> > > 🙂

---

<div class="post-metadata">

**Author:** ![Mike\_Stok](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Mike\_Stok](https://rubytalk.org/u/Mike_Stok)\
**Post date:** [27 March 2003 03:10 UTC](https://rubytalk.org/t/syntax/5361/9 "2003-03-27T03:10:57Z")

</div>

In article [20030326231043.GA1684@math.umd.edu](mailto:20030326231043.GA1684@math.umd.edu),

> **···**
>
> Daniel Carrera [dcarrera@math.umd.edu](mailto:dcarrera@math.umd.edu) wrote:
> 
> > On Thu, Mar 27, 2003 at 07:09:59AM +0900, Robert wrote:
> > 
> > > class Fixnum  
> > > def inc(val=1)  
> > > self += val  
> > > end  
> > > end
> > > 
> > > 1.inc
> > > 
> > > 🙂
> > 
> > That doesn’t work, but ‘1.succ’ does.
> 
> But surely the original poster was really trying to get something like
> 
> i = 1  
> i.succ! # hypothetical 🙂
> 
> which is a tad different…
> 
> Mike
> 
> –  
> [mike@stok.co.uk](mailto:mike@stok.co.uk) | The “`Stok’ disclaimers” apply.  
> [http://www.stok.co.uk/~mike/](http://www.stok.co.uk/~mike/) | GPG PGP Key 1024D/059913DA  
> [mike@exegenix.com](mailto:mike@exegenix.com) | Fingerprint 0570 71CD 6790 7C28 3D60  
> [http://www.exegenix.com/](http://www.exegenix.com/) | 75D2 9EC4 C1C0 0599 13DA
