Prev: Thread error "undefined method `keys' for nil:NilClass"
Next: Getting version from PE executables
From: RichardOnRails on 4 Nov 2009 15:29 On Nov 4, 2:13 pm, Seebs <usenet-nos...(a)seebs.net> wrote: > On 2009-11-04, RichardOnRails <RichardDummyMailbox58...(a)USComputerGurus.com> wrote: > > > class Fixnum > > def pp # We can?t define ++ because of a compiler restriction. > > self + 1 > > end > > end > > This doesn't seem to do the right thing. > > a = 1 > a.pp > > Is a now 2? If not, you haven't implemented an increment operator. > > > Appending these lines shows that a & b values are distinct. > > That is, after incrementing, a =2, b is unchanged; b is not > > impacted by a?s 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 > > Right. You've changed which object a refers to, because you've reassigned > a. > > > 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 > > Not the same. The key is that, after "a += 1", not only do you get 2, > but a is now 2. > > > Do you agree? > > No. > > For "a.pp" to be the same as a++ in other languages, you'd have to do: > > a = 1; a.pp; show(a) => Got 2 > > If you don't get a "2" by using a.pp, it's not an increment, just a "one > more than". > > Consider a loop: > > a = 1 > while ((a += 1) < 10) do > puts a > end > > Now, try: > > a = 1 > while (a.pp < 10) do > puts a > end > > Doesn't do the same thing. > > -s > -- > Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nos...(a)seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! Hi Peter, > This doesn't seem to do the right thing. > > a = 1 > a.pp > > Is a now 2? If not, you haven't implemented an increment operator. Thanks for this question. You're so right. I couldn't see it until you pointed it out. class Fixnum def pp # We cant define ++ because of a compiler restriction. self + 1 end end doesn't change self (which Ruby won't allow for Fixnum's!!! Which, of course, is why your: a = 1 while (a.pp < 10) do puts a end produces an infinite number of 1's ..., or would if RAM were infinite and hardware addressing mechanisms were infinite. I grateful for you taking the time to identify my deficiencies. Best wishes, Richard
From: Seebs on 4 Nov 2009 15:37 On 2009-11-04, RichardOnRails <RichardDummyMailbox58407(a)USComputerGurus.com> wrote: > I grateful for you taking the time to identify my deficiencies. I'd call it an oversight, not a personal failing. It might make sense to want to be able to do def pp() old = self self = self + 1 old end or something similar, but it's not well-defined. The problem is that fundamentally, when you have two variables, a and b, which both contain the object 1 (a Fixnum), there's no way to say "I want the object a points to to change, but not the object b points to" *in a method call*. Because the method call works on the object, not the variable. -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Tony Arcieri on 4 Nov 2009 15:40 [Note: parts of this message were removed to make it a legal post.] On Wed, Nov 4, 2009 at 11:21 AM, Paul Smith <paul(a)pollyandpaul.co.uk> wrote: > a = 1 > > a.class #Fixnum > > a++ # a is now 2 > > 1.class #Fixnum > > 1++ # Illegal > > So although a is a Fixnum, and 1 is a Fixnum, they respond to ++ > differently? > Why would 1++ be illegal? I'd think it would just evaluate to 1. -- Tony Arcieri Medioh/Nagravision
From: Marnen Laibow-Koser on 4 Nov 2009 15:46 Michael W. Ryder wrote: [...] > 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++. This is unidiomatic Ruby. In fact, it's becoming clear to me that just about *any* use case for postfix ++ is unidiomatic Ruby. The cleanest way *in Ruby* of doing what you did would be (1..10).each {|i| puts i} Incrementing is handled under the hood. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen(a)marnen.org -- Posted via http://www.ruby-forum.com/.
From: Tony Arcieri on 4 Nov 2009 15:48
[Note: parts of this message were removed to make it a legal post.] On Wed, Nov 4, 2009 at 1:46 PM, Marnen Laibow-Koser <marnen(a)marnen.org>wrote: > This is unidiomatic Ruby. In fact, it's becoming clear to me that just > about *any* use case for postfix ++ is unidiomatic Ruby. The cleanest > way *in Ruby* of doing what you did would be > > (1..10).each {|i| puts i} > > Incrementing is handled under the hood. > What if it's not? m = CounterInMemcacheOrSomething.new :foobar m++ -- Tony Arcieri Medioh/Nagravision |