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>
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
--
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>
Unsubscribe: <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>
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/ • http://twitter.com/halostatue