HAL_9000
(HAL 9000)
24 January 2003 04:24
1
OK, I was thinking today about "stacked"
assignment:
x, y, z = 0
Fine and dandy, since Fixnums are immediate.
But when there’s a “real” object on the
right, you get multiple references to the
same one. Fine if that’s what you want –
if not, oops.
My silly solution:
class Fixnum
alias :mult :*
def *(other)
puts other
if other.is_a? Numeric
self.mult(other)
else
arr = []
self.times { arr << other.dup }
arr
end
end
end
a, b, c, d, e = 5 * []
s1, s2, s3 = 3 * “hello”
I also thought of another method that would take
a block and thus the index could be used in
generating values. The code is trivial. I won’t
cloud the issue by showing it to you.
Comments?
Hal
OK, I was thinking today about “stacked”
assignment:
x, y, z = 0
a, b, c = 0
=> [0]
a
=> 0
b
=> nil
c
=> nil
x = y = z = 0 does what you want though. Or are we running different
Ruby versions? 1.6.8 for me.
And I don’t see the point of doing this with non-numerical objects,
either. One variable is enough.
Gavin
···
On Friday, January 24, 2003, 3:24:59 PM, Hal wrote:
“Hal E. Fulton” hal9000@hypermetrics.com writes:
OK, I was thinking today about “stacked”
assignment:
x, y, z = 0
<…>
a, b, c, d, e = 5 *
s1, s2, s3 = 3 * “hello”
% irb
x, y, z = [0] * 3
=> [0, 0, 0]
a, b, c, d, e = [ ] * 5
=> [ , , , , ]
s1, s2, s3 = [“hello”] * 3
=> [“hello”, “hello”, “hello”]
···
–
eban
HAL_9000
(HAL 9000)
24 January 2003 04:42
4
OK, I was thinking today about “stacked”
assignment:
x, y, z = 0
So sorry, thanks for pointing out my stupid mistake.
I did mean x = y = z = 0
And I don’t see the point of doing this with non-numerical objects,
either. One variable is enough.
Eh, I don’t mean many variables to one object. I mean
avoiding that.
E.g., rather than say:
x =
y =
z =
I’d prefer
x, y, z = 3 *
Hey, I said it was silly. Want your money back??
Hal
···
----- Original Message -----
From: “Gavin Sinclair” gsinclair@soyabean.com.au
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, January 23, 2003 10:32 PM
Subject: Re: Silly idea: map-like method produces array
On Friday, January 24, 2003, 3:24:59 PM, Hal wrote:
HAL_9000
(HAL 9000)
24 January 2003 04:47
5
Ah, never thought of that!
Too bad it requires an array…
the brackets make it a little uglier.
Thanks!
Hal
···
----- Original Message -----
From: “WATANABE Hirofumi” eban@os.rim.or.jp
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, January 23, 2003 10:42 PM
Subject: Re: Silly idea: map-like method produces array
% irb
x, y, z = [0] * 3
=> [0, 0, 0]
a, b, c, d, e = [ ] * 5
=> [ , , , , ]
s1, s2, s3 = [“hello”] * 3
=> [“hello”, “hello”, “hello”]
Hi –
From: “WATANABE Hirofumi” eban@os.rim.or.jp
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, January 23, 2003 10:42 PM
Subject: Re: Silly idea: map-like method produces array
% irb
x, y, z = [0] * 3
=> [0, 0, 0]
a, b, c, d, e = [ ] * 5
=> [ , , , , ]
s1, s2, s3 = [“hello”] * 3
=> [“hello”, “hello”, “hello”]
Ah, never thought of that!
Too bad it requires an array…
the brackets make it a little uglier.
That doesn’t do what you want, though:
irb(main):001:0> a,b,c = [ ] * 3
[ , , ]
irb(main):002:0> a[0] = 1
1
irb(main):003:0> b
[1]
I think eban’s point was that * already has a defined behavior for
arrays.
David
···
On Fri, 24 Jan 2003, Hal E. Fulton wrote:
----- Original Message -----
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav