[ruby-talk:443060] i++ does not work?

I found for a int the "++" operator doesn't work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so "++" is not supported in ruby really? thanks

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]

···

--
Simple Mail
https://simplemail.co.in/

Quoting Henrik P (henrik@simplemail.co.in):

I found for a int the "++" operator doesn't work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so "++" is not supported in ruby really? thanks

No. A conscious choice, I believe.

Carlo

···

Subject: [ruby-talk:443060] i++ does not work?
  Date: Fri 14 Oct 22 10:39:52AM +0800

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

i++ works in C and similar languages because a variable is a concrete
object, representing a specific chunk of memory. i++ increments the
contents of that chunk of memory.

in ruby, a variable is a transparent reference to an object, so any
operation on a variable is actually an operation on the object it points
to. i++ would mean "mutate the object i points to by incrementing it by 1",
and since numbers are immutable you cannot do that. on the other hand,
"i += 1" is just shorthand for "i = i + 1", which means "point the variable
i to a new object that is 1 more than the object it currently points to",
which works fine with ruby's semantics.

here's an illustration by contrast with strings, which do have mutating
methods:

irb(main):001:0> a = "hello"
=> "hello"
irb(main):002:0> b = a
=> "hello"
irb(main):003:0> a.upcase!
=> "HELLO"
irb(main):004:0> b
=> "HELLO"
irb(main):005:0> i = 1
=> 1
irb(main):006:0> j = i
=> 1
irb(main):007:0* i++ # <- what would you expect j to be after this?

martin

···

On Thu, Oct 13, 2022 at 7:40 PM Henrik P <henrik@simplemail.co.in> wrote:

I found for a int the "++" operator doesn't work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so "++" is not supported in ruby really? thanks

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]

--
Simple Mail
https://simplemail.co.in/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Is it posible define new method :++ to Integer class?

Thanks!

Enviado desde Proton Mail móvil

···

-------- Mensaje original --------
El 14 oct 2022 4:53, Martin DeMello escribió:

i++ works in C and similar languages because a variable is a concrete object, representing a specific chunk of memory. i++ increments the contents of that chunk of memory.

in ruby, a variable is a transparent reference to an object, so any operation on a variable is actually an operation on the object it points to. i++ would mean "mutate the object i points to by incrementing it by 1", and since numbers are immutable you cannot do that. on the other hand, "i += 1" is just shorthand for "i = i + 1", which means "point the variable i to a new object that is 1 more than the object it currently points to", which works fine with ruby's semantics.

here's an illustration by contrast with strings, which do have mutating methods:

irb(main):001:0> a = "hello"
=> "hello"
irb(main):002:0> b = a
=> "hello"
irb(main):003:0> a.upcase!
=> "HELLO"
irb(main):004:0> b
=> "HELLO"
irb(main):005:0> i = 1
=> 1
irb(main):006:0> j = i
=> 1
irb(main):007:0* i++ # <- what would you expect j to be after this?

martin

On Thu, Oct 13, 2022 at 7:40 PM Henrik P <henrik@simplemail.co.in> wrote:

I found for a int the "++" operator doesn't work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so "++" is not supported in ruby really? thanks

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]

--
Simple Mail
https://simplemail.co.in/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

No.

Number objects are immutable.

-a

···

On Fri, Oct 14, 2022 at 1:02 PM iloveruby <iloveruby@protonmail.com> wrote:

Is it posible define new method :++ to Integer class?

Thanks!

Enviado desde Proton Mail móvil

-------- Mensaje original --------
El 14 oct 2022 4:53, Martin DeMello < martindemello@gmail.com> escribió:

i++ works in C and similar languages because a variable is a concrete
object, representing a specific chunk of memory. i++ increments the
contents of that chunk of memory.

in ruby, a variable is a transparent reference to an object, so any
operation on a variable is actually an operation on the object it points
to. i++ would mean "mutate the object i points to by incrementing it by 1",
and since numbers are immutable you cannot do that. on the other hand,
"i += 1" is just shorthand for "i = i + 1", which means "point the variable
i to a new object that is 1 more than the object it currently points to",
which works fine with ruby's semantics.

here's an illustration by contrast with strings, which do have mutating
methods:

irb(main):001:0> a = "hello"
=> "hello"
irb(main):002:0> b = a
=> "hello"
irb(main):003:0> a.upcase!
=> "HELLO"
irb(main):004:0> b
=> "HELLO"
irb(main):005:0> i = 1
=> 1
irb(main):006:0> j = i
=> 1
irb(main):007:0* i++ # <- what would you expect j to be after this?

martin

On Thu, Oct 13, 2022 at 7:40 PM Henrik P <henrik@simplemail.co.in> wrote:

I found for a int the "++" operator doesn't work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so "++" is not supported in ruby really? thanks

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]

--
Simple Mail
https://simplemail.co.in/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

I guess immutability is not the reason behind this lack. In JavaScript,
integers are also immutable but "++" operator works. Again I guess this is
a design decision.

This increment operator has two forms as prefix (++i) and postfix (i++)
which arrange the order of incrementing the value and returning it.
Actually it comes from C and I find it confusing so I agree with this
design decision.

14 Eki 2022 Cum 20:16 tarihinde Austin Ziegler <halostatue@gmail.com> şunu
yazdı:

···

No.

Number objects are immutable.

-a

On Fri, Oct 14, 2022 at 1:02 PM iloveruby <iloveruby@protonmail.com> > wrote:

Is it posible define new method :++ to Integer class?

Thanks!

Enviado desde Proton Mail móvil

-------- Mensaje original --------
El 14 oct 2022 4:53, Martin DeMello < martindemello@gmail.com> escribió:

i++ works in C and similar languages because a variable is a concrete
object, representing a specific chunk of memory. i++ increments the
contents of that chunk of memory.

in ruby, a variable is a transparent reference to an object, so any
operation on a variable is actually an operation on the object it points
to. i++ would mean "mutate the object i points to by incrementing it by 1",
and since numbers are immutable you cannot do that. on the other hand,
"i += 1" is just shorthand for "i = i + 1", which means "point the variable
i to a new object that is 1 more than the object it currently points to",
which works fine with ruby's semantics.

here's an illustration by contrast with strings, which do have mutating
methods:

irb(main):001:0> a = "hello"
=> "hello"
irb(main):002:0> b = a
=> "hello"
irb(main):003:0> a.upcase!
=> "HELLO"
irb(main):004:0> b
=> "HELLO"
irb(main):005:0> i = 1
=> 1
irb(main):006:0> j = i
=> 1
irb(main):007:0* i++ # <- what would you expect j to be after this?

martin

On Thu, Oct 13, 2022 at 7:40 PM Henrik P <henrik@simplemail.co.in> wrote:

I found for a int the "++" operator doesn't work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so "++" is not supported in ruby really? thanks

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]

--
Simple Mail
https://simplemail.co.in/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

++ does make people confused. so maybe the explicit i+=1 is better.

$ perl -le 'sub test { $x=1;$x++} print(test)'
1
$ perl -le 'sub test { $x=1;++$x} print(test)'
2

perl has the same problem as C.

İsmail Arılık wrote:

···

This increment operator has two forms as prefix (++i) and postfix (i++) which arrange the order of incrementing the value and returning it. Actually it comes from C and I find it confusing so I agree with this design decision.

--
Simple Mail
https://simplemail.co.in/

Is it true that these confuse people? These operators exist in several
languages and they seem pretty simple to me. Guess tons of people use them
routinely no prob?

In addition to the side-effect, ++i and i++ are expressions, and they
evaluate to different things. In common idioms, their value is not even
used. Easy?

You’d be wrong in your guess.

In JavaScript, like in C and in Perl and other languages that have ++, variables are cells that can be manipulated. So i++ changes the value in the cell i.

Variables in Ruby aren’t cells, but labels. Thus, a hypothetical #++ would need to mutate the item which the variables labels, which would be an object. For integers, this would mean mutating the immutable integer object.

-a

···

On Oct 15, 2022, at 00:38, İsmail Arılık arilik.ismail@gmail.com wrote:

I guess immutability is not the reason behind this lack. In JavaScript, integers are also immutable but “++” operator works. Again I guess this is a design decision.

This increment operator has two forms as prefix (++i) and postfix (i++) which arrange the order of incrementing the value and returning it. Actually it comes from C and I find it confusing so I agree with this design decision.

https://ismailarilik.com

14 Eki 2022 Cum 20:16 tarihinde Austin Ziegler <halostatue@gmail.com> şunu yazdı:

No.

Number objects are immutable.

-a

On Fri, Oct 14, 2022 at 1:02 PM iloveruby <iloveruby@protonmail.com> wrote:

Is it posible define new method :++ to Integer class?

Thanks!

Enviado desde Proton Mail móvil

-------- Mensaje original --------
El 14 oct 2022 4:53, Martin DeMello < martindemello@gmail.com> escribió:

i++ works in C and similar languages because a variable is a concrete object, representing a specific chunk of memory. i++ increments the contents of that chunk of memory.

in ruby, a variable is a transparent reference to an object, so any operation on a variable is actually an operation on the object it points to. i++ would mean “mutate the object i points to by incrementing it by 1”, and since numbers are immutable you cannot do that. on the other hand, “i += 1” is just shorthand for “i = i + 1”, which means “point the variable i to a new object that is 1 more than the object it currently points to”, which works fine with ruby’s semantics.

here’s an illustration by contrast with strings, which do have mutating methods:

irb(main):001:0> a = “hello”
=> “hello”
irb(main):002:0> b = a
=> “hello”
irb(main):003:0> a.upcase!
=> “HELLO”
irb(main):004:0> b
=> “HELLO”
irb(main):005:0> i = 1
=> 1
irb(main):006:0> j = i
=> 1
irb(main):007:0* i++ # ← what would you expect j to be after this?

martin

On Thu, Oct 13, 2022 at 7:40 PM Henrik P <henrik@simplemail.co.in> wrote:

I found for a int the “++” operator doesn’t work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so “++” is not supported in ruby really? thanks

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]


Simple Mail
https://simplemail.co.in/

Unsubscribe: mailto:[ruby-talk-request@ruby-lang.org](mailto:ruby-talk-request@ruby-lang.org)?subject=unsubscribe
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: mailto:[ruby-talk-request@ruby-lang.org](mailto:ruby-talk-request@ruby-lang.org)?subject=unsubscribe
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Austin Ziegler • halostatue@gmail.comaustin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

Unsubscribe: mailto:[ruby-talk-request@ruby-lang.org](mailto:ruby-talk-request@ruby-lang.org)?subject=unsubscribe
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe
http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk

You could simply use 1.next or var.next to perform the same task as #++
does in other languages. 1.pred or var.pred would effectively replace #--.

···

On Sat, Oct 15, 2022, 07:10 Austin Ziegler <halostatue@gmail.com> wrote:

You’d be wrong in your guess.

In JavaScript, like in C and in Perl and other languages that have ++,
variables are cells that can be manipulated. So i++ changes the value in
the cell i.

Variables in Ruby aren’t cells, but labels. Thus, a hypothetical #++ would
need to mutate the item which the variables labels, which would be an
object. For integers, this would mean mutating the immutable integer object.

-a

On Oct 15, 2022, at 00:38, İsmail Arılık <arilik.ismail@gmail.com> wrote:

I guess immutability is not the reason behind this lack. In JavaScript,
integers are also immutable but "++" operator works. Again I guess this is
a design decision.

This increment operator has two forms as prefix (++i) and postfix (i++)
which arrange the order of incrementing the value and returning it.
Actually it comes from C and I find it confusing so I agree with this
design decision.

https://ismailarilik.com

14 Eki 2022 Cum 20:16 tarihinde Austin Ziegler <halostatue@gmail.com>
şunu yazdı:

No.

Number objects are immutable.

-a

On Fri, Oct 14, 2022 at 1:02 PM iloveruby <iloveruby@protonmail.com> >> wrote:

Is it posible define new method :++ to Integer class?

Thanks!

Enviado desde Proton Mail móvil

-------- Mensaje original --------
El 14 oct 2022 4:53, Martin DeMello < martindemello@gmail.com> escribió:

i++ works in C and similar languages because a variable is a concrete
object, representing a specific chunk of memory. i++ increments the
contents of that chunk of memory.

in ruby, a variable is a transparent reference to an object, so any
operation on a variable is actually an operation on the object it points
to. i++ would mean "mutate the object i points to by incrementing it by 1",
and since numbers are immutable you cannot do that. on the other hand,
"i += 1" is just shorthand for "i = i + 1", which means "point the variable
i to a new object that is 1 more than the object it currently points to",
which works fine with ruby's semantics.

here's an illustration by contrast with strings, which do have mutating
methods:

irb(main):001:0> a = "hello"
=> "hello"
irb(main):002:0> b = a
=> "hello"
irb(main):003:0> a.upcase!
=> "HELLO"
irb(main):004:0> b
=> "HELLO"
irb(main):005:0> i = 1
=> 1
irb(main):006:0> j = i
=> 1
irb(main):007:0* i++ # <- what would you expect j to be after this?

martin

On Thu, Oct 13, 2022 at 7:40 PM Henrik P <henrik@simplemail.co.in> >>> wrote:

I found for a int the "++" operator doesn't work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so "++" is not supported in ruby really? thanks

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]

--
Simple Mail
https://simplemail.co.in/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

El ds, 15 oct 2022 a les 16:04 kyonides eupator <kyonides@gmail.com> va
escriure:

You could simply use 1.next or var.next to perform the same task as #++
does in other languages. 1.pred or var.pred would effectively replace #--.

Yes as an expression, but no for the side-effect, which is the main use
case of these operators.

···

I don't know too much about internal structure of variables in JS and Ruby.
But as I know, variables may be thought as *references* to *values* in
those languages. So for JS, *i++* or *++i* doesn't change the *value* *i*
*refers* to; it creates a new *value* which is *i + 1* and arranges *i* so
that it *refers* to this increased new value. In Ruby, it might be like
this, right? Please correct me if I am wrong in any way.

15 Eki 2022 Cmt 16:10 tarihinde Austin Ziegler <halostatue@gmail.com> şunu
yazdı:

···

You’d be wrong in your guess.

In JavaScript, like in C and in Perl and other languages that have ++,
variables are cells that can be manipulated. So i++ changes the value in
the cell i.

Variables in Ruby aren’t cells, but labels. Thus, a hypothetical #++ would
need to mutate the item which the variables labels, which would be an
object. For integers, this would mean mutating the immutable integer object.

-a

On Oct 15, 2022, at 00:38, İsmail Arılık <arilik.ismail@gmail.com> wrote:

I guess immutability is not the reason behind this lack. In JavaScript,
integers are also immutable but "++" operator works. Again I guess this is
a design decision.

This increment operator has two forms as prefix (++i) and postfix (i++)
which arrange the order of incrementing the value and returning it.
Actually it comes from C and I find it confusing so I agree with this
design decision.

https://ismailarilik.com

14 Eki 2022 Cum 20:16 tarihinde Austin Ziegler <halostatue@gmail.com>
şunu yazdı:

No.

Number objects are immutable.

-a

On Fri, Oct 14, 2022 at 1:02 PM iloveruby <iloveruby@protonmail.com> >> wrote:

Is it posible define new method :++ to Integer class?

Thanks!

Enviado desde Proton Mail móvil

-------- Mensaje original --------
El 14 oct 2022 4:53, Martin DeMello < martindemello@gmail.com> escribió:

i++ works in C and similar languages because a variable is a concrete
object, representing a specific chunk of memory. i++ increments the
contents of that chunk of memory.

in ruby, a variable is a transparent reference to an object, so any
operation on a variable is actually an operation on the object it points
to. i++ would mean "mutate the object i points to by incrementing it by 1",
and since numbers are immutable you cannot do that. on the other hand,
"i += 1" is just shorthand for "i = i + 1", which means "point the variable
i to a new object that is 1 more than the object it currently points to",
which works fine with ruby's semantics.

here's an illustration by contrast with strings, which do have mutating
methods:

irb(main):001:0> a = "hello"
=> "hello"
irb(main):002:0> b = a
=> "hello"
irb(main):003:0> a.upcase!
=> "HELLO"
irb(main):004:0> b
=> "HELLO"
irb(main):005:0> i = 1
=> 1
irb(main):006:0> j = i
=> 1
irb(main):007:0* i++ # <- what would you expect j to be after this?

martin

On Thu, Oct 13, 2022 at 7:40 PM Henrik P <henrik@simplemail.co.in> >>> wrote:

I found for a int the "++" operator doesn't work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so "++" is not supported in ruby really? thanks

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]

--
Simple Mail
https://simplemail.co.in/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

I don't know too much about internal structure of variables in JS and
Ruby. But as I know, variables may be thought as *references* to *values*
in those languages. So for JS, *i++* or *++i* doesn't change the *value*
*i* *refers* to; it creates a new *value* which is *i + 1* and arranges
*i* so that it *refers* to this increased new value. In Ruby, it might be
like this, right? Please correct me if I am wrong in any way.

Again, you’re wrong. In JS (and other languages which support `++`), a
variable is a cell. It’s a place where something gets stored and can be
manipulated. It holds a value. Variables work like this in JavaScript for
immediate (non-object) values. And number values aren’t objects in
JavaScript.

          ┌───┬───┐ ┌───┬───┐
var i, j; │ i │ │ │ j │ │
          └───┴───┘ └───┴───┘
          ┌───┬───┐ ┌───┬───┐
i = 2; │ i │ 2 │ │ j │ 2 │
j = i; └───┴───┘ └───┴───┘
          ┌───┬───┐ ┌───┬───┐
i++; │ i │ 3 │ │ j │ 2 │
          └───┴───┘ └───┴───┘

In Ruby, variables are just labels to objects and numbers, like everything
else in Ruby, are objects. (They happen to be invariant / immutable). Every
variable that "holds" the value 2 in Ruby is just *pointing* to an object
whose identity is 2.

          ┌───┐
i = 2 │ i │───────────────┐
          └───┘ ▼
          ┌───┐ ┌───┐
j = i │ j │────────────>│ 2 │
          └───┘ └───┘

          ┌───┐ ┌───┐
i += 1 │ j │────────────>│ 2 │
          └───┘ └───┘
          ┌───┐ ┌───┐
          │ i │────────────>│ 3 │
          └───┘ └───┘

If `#++` were a method on Integer objects, then we would *necessarily* be
mutating 2, since the receiving objects know nothing about the variable
with which they are referred to.

This also gets to a subtlety in Ruby’s calling convention. C is mostly pass
by value (even if the value you are passing is a pointer); Java is mostly
pass by reference (except unboxed values). Ruby is to pass reference by
value. To give an easier to read example, you’d look at something
Pascal-ish (also PG/PLSQL or Oracle PLSQL or Ada):

PROCEDURE example(IN a: INT, INOUT b: INT, OUT c: int)
BEGIN
  b := b * 2
  c := 15
END

# Pretend there are declarations here
b := 4
c := 5

example(a, b, c)

In this example, a is unchanged, b becomes 8, and c becomes 15. You’re
allowed to change the value of the variable because it is itself a cell
used for storage.

There is no equivalent in Ruby:

def example(a, b, c)
  a = 5
  b = b * 2
  c = 15
end

a, b, c = 3, 4, 5
example(a, b, c)
[a, b, c] # => 3, 4, 5

*But wait*, you say—I can modify a string that’s passed in or another
object. Yes, but *only* if you use mutable methods. Otherwise, you need to
do something like `a, b, c = example(a, b, c)` and the *return* value from
`example` needs to be `[a, b, c]`. You *cannot* change the value of a
variable by reassignment in a different context because the calling
convention is pass by value, but all values are *references* to objects.

-a

···

On Sat, Oct 15, 2022 at 2:32 PM İsmail Arılık <arilik.ismail@gmail.com> wrote:
  a := 5
a := 3

15 Eki 2022 Cmt 16:10 tarihinde Austin Ziegler <halostatue@gmail.com>
şunu yazdı:

You’d be wrong in your guess.

In JavaScript, like in C and in Perl and other languages that have ++,
variables are cells that can be manipulated. So i++ changes the value in
the cell i.

Variables in Ruby aren’t cells, but labels. Thus, a hypothetical #++
would need to mutate the item which the variables labels, which would be an
object. For integers, this would mean mutating the immutable integer object.

-a

On Oct 15, 2022, at 00:38, İsmail Arılık <arilik.ismail@gmail.com> wrote:

I guess immutability is not the reason behind this lack. In JavaScript,
integers are also immutable but "++" operator works. Again I guess this is
a design decision.

This increment operator has two forms as prefix (++i) and postfix (i++)
which arrange the order of incrementing the value and returning it.
Actually it comes from C and I find it confusing so I agree with this
design decision.

https://ismailarilik.com

14 Eki 2022 Cum 20:16 tarihinde Austin Ziegler <halostatue@gmail.com>
şunu yazdı:

No.

Number objects are immutable.

-a

On Fri, Oct 14, 2022 at 1:02 PM iloveruby <iloveruby@protonmail.com> >>> wrote:

Is it posible define new method :++ to Integer class?

Thanks!

Enviado desde Proton Mail móvil

-------- Mensaje original --------
El 14 oct 2022 4:53, Martin DeMello < martindemello@gmail.com> >>>> escribió:

i++ works in C and similar languages because a variable is a concrete
object, representing a specific chunk of memory. i++ increments the
contents of that chunk of memory.

in ruby, a variable is a transparent reference to an object, so any
operation on a variable is actually an operation on the object it points
to. i++ would mean "mutate the object i points to by incrementing it by 1",
and since numbers are immutable you cannot do that. on the other hand,
"i += 1" is just shorthand for "i = i + 1", which means "point the variable
i to a new object that is 1 more than the object it currently points to",
which works fine with ruby's semantics.

here's an illustration by contrast with strings, which do have mutating
methods:

irb(main):001:0> a = "hello"
=> "hello"
irb(main):002:0> b = a
=> "hello"
irb(main):003:0> a.upcase!
=> "HELLO"
irb(main):004:0> b
=> "HELLO"
irb(main):005:0> i = 1
=> 1
irb(main):006:0> j = i
=> 1
irb(main):007:0* i++ # <- what would you expect j to be after this?

martin

On Thu, Oct 13, 2022 at 7:40 PM Henrik P <henrik@simplemail.co.in> >>>> wrote:

I found for a int the "++" operator doesn't work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so "++" is not supported in ruby really? thanks

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]

--
Simple Mail
https://simplemail.co.in/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

[snip]

···

On Sat, 15 Oct 2022 at 14:40, İsmail Arılık <arilik.ismail@gmail.com> wrote:

Again I guess this is a design decision.

Matz has said he wants assignment operations to include an '=' to
avoid confusion.

BTW, `1++` would do what?

Cheers
--
  Matthew Kerwin
  https://matthew.kerwin.net.au/

BTW, `1++` would do what?

syntax error (though 1+++++++++1 is 2).

i++ in C/C++/Java/Perl/Go, etc. changes the value the variable stores,
incrementing an integer is as meaningless.

···

On Sun, Oct 16, 2022 at 3:23 AM Matthew Kerwin <matthew@kerwin.net.au> wrote:

There is really only one Ruby 'operator' that works on variables:
assignment `=`. That's why Matz has said any syntactic sugar for
assignment has to include an `=`.

`1 + 1` is `1.+(1)`

`i += 1` is `i = i.+(1)`

`i++` looks like `i.++@` which is not an assignment.

Cheers

···

On Sun, 16 Oct 2022 at 11:34, Xavier Noria <fxn@hashref.com> wrote:

On Sun, Oct 16, 2022 at 3:23 AM Matthew Kerwin <matthew@kerwin.net.au> wrote:

BTW, `1++` would do what?

syntax error (though 1+++++++++1 is 2).

i++ in C/C++/Java/Perl/Go, etc. changes the value the variable stores, incrementing an integer is as meaningless.

--
  Matthew Kerwin
  https://matthew.kerwin.net.au/

(Or `i.@++`, pre-op, post-op, blah blah)

···

On Wed, 19 Oct 2022 at 09:32, Matthew Kerwin <matthew@kerwin.net.au> wrote:

`i++` looks like `i.++@` which is not an assignment.

--
  Matthew Kerwin
  https://matthew.kerwin.net.au/

Scala operator also works with variables.

var i = 1

var i: Int = 1

i++

         ^
        error: value ++ is not a member of Int

i += 1

i.+=(1)

i

val res3: Int = 3

which doesn't support i++ as well.

Matthew Kerwin wrote:

···

There is really only one Ruby 'operator' that works on variables:

i += 1

···

On Fri, Oct 14, 2022, 9:38 PM İsmail Arılık <arilik.ismail@gmail.com> wrote:

I guess immutability is not the reason behind this lack. In JavaScript,
integers are also immutable but "++" operator works. Again I guess this is
a design decision.

This increment operator has two forms as prefix (++i) and postfix (i++)
which arrange the order of incrementing the value and returning it.
Actually it comes from C and I find it confusing so I agree with this
design decision.

https://ismailarilik.com

14 Eki 2022 Cum 20:16 tarihinde Austin Ziegler <halostatue@gmail.com>
şunu yazdı:

No.

Number objects are immutable.

-a

On Fri, Oct 14, 2022 at 1:02 PM iloveruby <iloveruby@protonmail.com> >> wrote:

Is it posible define new method :++ to Integer class?

Thanks!

Enviado desde Proton Mail móvil

-------- Mensaje original --------
El 14 oct 2022 4:53, Martin DeMello < martindemello@gmail.com> escribió:

i++ works in C and similar languages because a variable is a concrete
object, representing a specific chunk of memory. i++ increments the
contents of that chunk of memory.

in ruby, a variable is a transparent reference to an object, so any
operation on a variable is actually an operation on the object it points
to. i++ would mean "mutate the object i points to by incrementing it by 1",
and since numbers are immutable you cannot do that. on the other hand,
"i += 1" is just shorthand for "i = i + 1", which means "point the variable
i to a new object that is 1 more than the object it currently points to",
which works fine with ruby's semantics.

here's an illustration by contrast with strings, which do have mutating
methods:

irb(main):001:0> a = "hello"
=> "hello"
irb(main):002:0> b = a
=> "hello"
irb(main):003:0> a.upcase!
=> "HELLO"
irb(main):004:0> b
=> "HELLO"
irb(main):005:0> i = 1
=> 1
irb(main):006:0> j = i
=> 1
irb(main):007:0* i++ # <- what would you expect j to be after this?

martin

On Thu, Oct 13, 2022 at 7:40 PM Henrik P <henrik@simplemail.co.in> >>> wrote:

I found for a int the "++" operator doesn't work in ruby.
such as,

i=0
i++

not working. but instead it would write as:

i=0
i += 1

so "++" is not supported in ruby really? thanks

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]

--
Simple Mail
https://simplemail.co.in/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

It isn't possible to implement i++, but it is... kinda... possible to implement ++i. Take a look at the following code. Just please don't use it in production :wink:

class IntContainer
def initialize(value, origref=nil)
@value = value
@origref = origref
end

def +@
case @origref
when nil
IntContainer.new(+@value, self)
else
@origref.value += 1
@origref
end
end

def -@
case @origref
when nil
IntContainer.new(-@value, self)
else
@origref.value -= 1
@origref
end
end

attr_accessor :value

def method_missing(meth, ...)
IntContainer.new(@value.send(meth, ...))
end

def inspect(...) = @value.inspect(...)
def to_s(...) = @value.to_s(...)
end

int = IntContainer.new(5)
p int + 10
++int
p int
--int
p int

···

On 10/14/22 19:01, iloveruby wrote:

Is it posible define new method :++ to Integer class?

Thanks!

Enviado desde Proton Mail móvil

-------- Mensaje original --------
El 14 oct 2022 4:53, Martin DeMello < martindemello@gmail.com> escribió:

    i++ works in C and similar languages because a variable is a
    concrete object, representing a specific chunk of memory. i++
    increments the contents of that chunk of memory.

    in ruby, a variable is a transparent reference to an object, so
    any operation on a variable is actually an operation on the object
    it points to. i++ would mean "mutate the object i points to by
    incrementing it by 1", and since numbers are immutable you cannot
    do that. on the other hand, "i += 1" is just shorthand for "i =
    i + 1", which means "point the variable i to a new object that is
    1 more than the object it currently points to", which works fine
    with ruby's semantics.

    here's an illustration by contrast with strings, which do have
    mutating methods:

    irb(main):001:0> a = "hello"
    => "hello"
    irb(main):002:0> b = a
    => "hello"
    irb(main):003:0> a.upcase!
    => "HELLO"
    irb(main):004:0> b
    => "HELLO"
    irb(main):005:0> i = 1
    => 1
    irb(main):006:0> j = i
    => 1
    irb(main):007:0* i++ # <- what would you expect j to be after this?

    martin

    On Thu, Oct 13, 2022 at 7:40 PM Henrik P <henrik@simplemail.co.in> > wrote:

        I found for a int the "++" operator doesn't work in ruby.
        such as,

        i=0
        i++

        not working. but instead it would write as:

        i=0
        i += 1

        so "++" is not supported in ruby really? thanks

        $ ruby -v
        ruby 2.6.3p62 (2019-04-16 revision 67580)
        [universal.x86_64-darwin20]

        -- Simple Mail
        https://simplemail.co.in/

        Unsubscribe:
        <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
        <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org