On Sat, 23 Nov 2002 01:35:38 +0900, Carlos wrote:
>>| The findings of fact (well, OK, my opinion) are these:
>>| *array is only allowed at the end of arrays because it's
>>| implemented as a hack.
>> This is a false assumption. The reason is it's not encouraged. If
>> you need array expansion in the middle of arguments, there must
>> be something wrong, perhaps in method argument design.
> Yes, but sometimes you don't design the method, and expansion is
> useful to encapsulate arguments. For example, this is what I tried
> to do some weeks ago, and seemed very natural:

> line(*p1, *p2, attrs) # parse error
vs.
> line(p1.x, p1.y, p2.x, p2.y, attrs)

It's not as pretty, but:

line(*[p1.to_a, p2.to_a, "boo"].flatten)

Alternately:

class Point
  attr_accessor :x, :y

  def initialize(x = 0, y = 0)
    @x = x
    @y = y
  end

  def to_a
    [@x, @y]
  end

  def self.[](p)
    [p.x, p.y]
  end
end

p1 = Point.new(5, 4)
p2 = Point.new(10, 10)

def line(x1, y1, x2, y2, attrs)
  puts "line(#{x1}, #{y1}, #{x2}, #{y2}, #{attrs})"
end

line(*[p1.to_a, p2.to_a, "boo"].flatten)
line(*[Point[p1], Point[p2], "boo"].flatten)

Ideal? No. But I do think that while line(*p1, *p2, attrs) looks
cleaner, it makes too many assumptions about the interfaces of both
classes to be a valid general case.

-austin
-- Austin Ziegler, austin / halostatue.ca on 2002.11.22 at 12.12.28