Prev: Thread error "undefined method `keys' for nil:NilClass"
Next: Getting version from PE executables
From: Martin DeMello on 4 Nov 2009 14:16 On Wed, Nov 4, 2009 at 11:46 PM, Tony Arcieri <tony(a)medioh.com> wrote: > On Wed, Nov 4, 2009 at 10:23 AM, Martin DeMello <martindemello(a)gmail.com>wrote: > >> And how exactly would you change the value of 1 in place? >> > > You don't. Are you insinuating the behavior of Fixnums isn't already > special cased to begin with? yes, i see your point, but consider: a = Foo.new() b = a c = 42 d = c a++ p b c++ p d martin > > -- > Tony Arcieri > Medioh/Nagravision >
From: RichardOnRails on 4 Nov 2009 14:20 On Nov 4, 10:59 am, Yukihiro Matsumoto <m...(a)ruby-lang.org> wrote: > Hi, > > In message "Re: Ruby doesn't implement x++ for Fixnum's because ???" > on Wed, 4 Nov 2009 23:31:46 +0900, Marnen Laibow-Koser <mar...(a)marnen.org> writes: > > |I believe you are quite wrong. If a destructive function like gsub! can > |be implemented as a method, then I see no reason that +=, |=, or postfix > |++ couldn't be. > > Only if you accept the language that can change the value of 1 to 2. > I don't. > > matz. Hi Matz, Thank you very much for your brilliant and enormous efforts in creating Ruby and offering to the programming world as gift. > Only if you accept the language that can change the value of 1 to 2. I know that you know Ruby extremely well. But I have written the following tests on this issue of whether "1" ever gets changed to "2". I assume you have not looked at my post yesterday on this issue. I'd be honored if you'd look at the following comments and code and point out anything you view as erroneous. BTW, I'm not advocating x++ for Ruby. I'm just trying to understand whether Ruby would literally change 1 to 2 as opposed to change a variable that contains 1 to subsequently contain 2. Best wishes, Richard def show(v) "Got #{v}, class = #{v.class}, object_id = #{v.object_id} (v.object_id-1)/2 = #{(v.object_id-1)/2 }" end class Fixnum def pp # We cant define ++ because of a compiler restriction. self + 1 end end These lines show that a & b values are stored in their object_ids held in the symbol table. Dont believe it? Read more. a = 1; show (a) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1 b = 1; show (b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1 a == b => true Appending these lines shows that a & b values are distinct. That is, after incrementing, a =2, b is unchanged; b is not impacted by as change a += 1; show (a) => Got 2; class = Fixnum; object_id = 5; v >> 1 = 2 show (b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1 Appending these lines shows the ++s alias pp works just find a=1; show(a.pp) => Got 2; class = Fixnum; object_id = 5; v >> 1 = 2 show(b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1 Appending these lines show that ++ crosses the Fixnum/Bignum boundary a = 2**30-1; show (a) => Got 1073741823; class = Fixnum; object_id = 2147483647; v >> 1 = 1073741823 show(a.pp) => Got 1073741824; class = Bignum; object_id = 22738520; v >> 1 = 11369260 # v >> 1 is irrelevant, of course.
From: Aldric Giacomoni on 4 Nov 2009 15:00 RichardOnRails wrote: > BTW, I'm not advocating x++ for Ruby. I'm just trying to understand > whether Ruby would literally change 1 to 2 as opposed to change a > variable that contains 1 to subsequently contain 2. I am confused. irb(main):001:0> 1.succ => 2 irb(main):002:0> 1.object_id => 3 irb(main):003:0> 1.succ.object_id => 5 irb(main):004:0> Is that good enough? If not, I'd recommend taking a look at... irb(main):004:0> 1.class => Fixnum Fixnum .. Fixed number? :) -- Posted via http://www.ruby-forum.com/.
From: Martin DeMello on 4 Nov 2009 15:18 On Thu, Nov 5, 2009 at 12:55 AM, RichardOnRails <RichardDummyMailbox58407(a)uscomputergurus.com> wrote: > > BTW, I'm not advocating x++ for Ruby. I'm just trying to understand > whether Ruby would literally change 1 to 2 as opposed to change a > variable that contains 1 to subsequently contain 2. Variables don't contain values, they refer to objects. That's the fundamental difference. So if you say, for instance a = "hello world" a.upcase! a #=> "HELLO WORLD" the message "upcase!" is sent to the *object* "hello world", not the variable a. To see this: a = "hello world" b = a a.upcase! a #=> "HELLO WORLD" b #=> "HELLO WORLD" Fixnums are immutable objects; you can't have any method that changes their value. Hence no ++ martin
From: Michael W. Ryder on 4 Nov 2009 15:22
Walton Hoops wrote: >> -----Original Message----- >> From: bascule(a)gmail.com [mailto:bascule(a)gmail.com] On Behalf Of Tony >> Arcieri >> wrote: >> I think you're missing why ++ could be useful, and it's precisely >> because >> Ruby is a "21st century language" >> >> The ++ operator, far more than just being syntactic sugar for +=1, >> would >> allow you to send an "increment" message to any object, which would >> change >> its value in place, i.e. >> >> def ++ >> incrementing_logic_goes_here >> end >> >> I could see this as being handy > > But you already can with the mechanics of the language that are already > present! > > irb(main):003:0> i=15 > => 15 > irb(main):004:0> i=i.succ > => 16 > irb(main):005:0> i="15" > => "15" > irb(main):006:0> i=i.succ > => "16" > irb(main):007:0> i=1.2 > => 1.2 > irb(main):008:0> i=i.succ > NoMethodError: undefined method `succ' for 1.2:Float > from (irb):8 > from /usr/local/bin/irb:12:in `<main>' > > In an object that it makes sense to increment, define the #succ method! > It's that easy! > > But i.succ does Not work in the following: i = 1 while (i < 10) puts i.succ end the only way to get this to work is to use: puts i; i = i.succ which is not as clean as using puts i++. |