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

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 馃槈 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>
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

I found it a lot of fun... trying to understand the code and see how it works. Thank you for this ingenious way to solve the problem. I have learned that there are some special method names, +@, -@, etc. which are invoked by writing them before the object...mmmm interesting. But seems it only works with +@ and -@... where could I find more information about it? Can I create other XXX@ methods? PS: When you create the object IinitContainer(@value, self) (lines 12 and 22).... the value of @value could be anything. It doesn't matter if you put +@value or -@value or nil or whatever because it won't be used. ;-) Thank you very much! :-) -------------------------------------- 1 #!/usr/bin/env ruby 2 3 class IntContainer 4 def initialize(value, origref=nil) 5 @value = value 6 @origref = origref 7 end 8 9 def +@ 10 case @origref 11 when nil 12 IntContainer.new(@value, self) 13 else 14 @origref.value += 1 15 @origref 16 end 17 end 18 19 def -@ 20 case @origref 21 when nil 22 IntContainer.new(@value, self) 23 else 24 @origref.value -= 1 25 @origref 26 end 27 end 28 29 attr_accessor :value 30 31 def method_missing(meth, ...) 32 IntContainer.new(@value.send(meth, ...)) 33 end 34 35 def inspect(...) = @value.inspect(...) 36 def to_s(...) = @value.to_s(...) 37 end 38 39 int = IntContainer.new(5) 40 41 puts int + 10 42 puts (++int) 43 puts (--int) -------------------------------------- Enviado con Proton Mail correo electr贸nico seguro. ------- Original Message ------- El martes, 29 de noviembre de 2022 a las 20:10, hmdne <hmdne@airmail.cc> escribi贸:
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 馃槈
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
Unsubscribe: mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk
______________________________________________ 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 -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-talk.ml.ruby-lang.org...

On 12/2/22, iloveruby <iloveruby@protonmail.com> wrote:
I have learned that there are some special method names, +@, -@, etc. which are invoked by writing them before the object...mmmm interesting.
But seems it only works with +@ and -@... where could I find more information about it? Can I create other XXX@ methods?
https://docs.ruby-lang.org/en/master/syntax/methods_rdoc.html#label-Method+N... To define unary methods minus and plus, follow the operator with an @ as in +@: The @ is needed to differentiate unary minus and plus operators from binary minus and plus operators. You can also follow tilde and not (!) unary methods with @, but it is not required as there are no binary tilde and not operators. Unary methods accept zero arguments. These are method names for the various Ruby operators. Each of these operators accepts only one argument: + - * ** / % & ^ >> << == != === =~ !~ <=> < <= > >=
participants (3)
-
Frank J. Cameron
-
hmdne
-
iloveruby