> Steve Litt wrote:
> >I converted the array of arguments to a string, and it worked.
> > But it should have worked the other way too. What am I missing?
>
> exec *argarray
Confirmed! Thanks Florian.
Now I can run a command with several words quoted together to make
one argument.
Curious -- what does the asterisk do to make it work when it didn't
work without the asterisk. What does the asterisk do? I know it
doesn't mean "the contents of this address" -- that's another
language
It flattens the array (argarray) into an argument list.
This means the first element of argarray becomes the first
argument to exec, the second element of argarray becomes
the second argument to exec and so on.
I've seen it also called the 'splat' operator, and (I think?) David Black called it the 'unary unarray' operator, which I a good name for it. It doesn't just work with method arguments, and it doesn't just work with arrays:
p = *File.open('/etc/passwd')
gives an array of all the lines in the file. I was most impressed when I found that out recently
(p.s. I'm not advocating that method of reading files _at all_ - it's just an example)
--
Ross Bamford - rosco@roscopeco.remove.co.uk
This scared me, so I did some experiments
logan:/Users/logan% irb
irb(main):001:0> lines = *File.open('test.yml')
=> ["---\n", "words:\n", " - yes\n", " - put\n", " - it\n", " - on\n", " - the\n", " - off\n", " - setting\n"]
irb(main):002:0> h = { :a=>:b, :c=>:d }
=> {:a=>:b, :c=>:d}
irb(main):003:0> keysvals = *h
=> [[:a, :b], [:c, :d]]
irb(main):004:0> class Q
irb(main):005:1> def to_a
irb(main):006:2> "Hello!"
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> q = Q.new
=> #<Q:0xc13e4>
irb(main):010:0> qarray = *q
TypeError: `to_a' did not return Array
from (irb):10
irb(main):011:0>
Apparently splat will splat almost anything as long as its to_a method returns an array (a reasonable assumption). I don't know if I really like this. I always kind of thought of splat as syntax (like &), not really as a method (operator). Apparently its not the unary unarray operator, its the unary to array then unarray operator.
···
On Dec 18, 2005, at 8:38 AM, Ross Bamford wrote:
On Sun, 18 Dec 2005 12:58:09 -0000, Stefan Lang <langstefan@gmx.at> > wrote:
On Sunday 18 December 2005 13:43, Steve Litt wrote:
On Sunday 18 December 2005 04:46 am, Florian Frank wrote:
from :0