Syntax

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

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 pabs in #gah (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

i += 1

···

— daniel 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

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

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

1.inc

:slight_smile:

“Stephan Kämper” Stephan.Kaemper@Schleswig-Holstein.de schrieb im
Newsbeitrag 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

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

:slight_smile:

“Stephan Kämper” Stephan.Kaemper@Schleswig-Holstein.de schrieb im
Newsbeitrag 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

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

:slight_smile:


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.

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
well, it should be. :slight_smile:

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

Cheers,
Hal

···

----- Original Message -----
From: rodrigo.bermejo@ps.ge.com
To: “ruby-talk ML” 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

:slight_smile:

In article 20030326231043.GA1684@math.umd.edu,

···

Daniel Carrera 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

:slight_smile:

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

which is a tad different…

Mike


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA