-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
It is splat. Here's some more examples:
(1)
Hash[:abc, 1, :def, 2] => { :abc => 1, :def => 2 }
Hash[*[:abc, 1, :def, 2]] => { :abc => 1, :def => 2 }
==
(2)
bar, *foo = [1,2,3,4]
bar => 1
foo => [2,3,4]
==
(3)
a, b = *{:a => 'one', :b => 'two', :c => 'three'}
a => [:b, "two"]
b => [:c, "three"]
==
(4)
def foo(first, *rest)
~ "#{first.inspect}, #{rest.inspect}"
end
foo(3,4,5,6,7) => "3, [4,5,6,7]"
==
I'm not quite sure how to summarize the function of the splat operator
in a single sentence, although I guess you can think of it as a
general-purpose operator for slicing and binding enumerables to separate
variables in (2) and (3), and achieving the opposite effect in (1) and (4).
The behavior in (4) is reminiscent of the behavior of improper lists in
Scheme lambda argument lists:
(lambda (first . rest) ...)
- - David
Ernest Prabhakar wrote:
| That's probably a ruby "splat", which I don't quite understand either,
| but I think it turns a set of arguments into a single array or vice
| versa. Or something like that...
|
| -enp
| On Feb 4, 2008, at 12:00 PM, cbeok wrote:
|
|> So, I've never seen this before Shoes, so I dont know whether I have a
|> Ruby question or a Shoes question, bear with me.
|>
|> Can anybody tell me why
|>
|> blah = para
|>
|> blah.replace *(ary.map {|x| [link(x) {action}]}.flatten)
|>
|>
|> does what I want, but
|>
|> blah.replace(ary.map {|x| [linke(x) {action}]}.flatten)
|>
|> does not?
|>
|> Whats with that asterisk?
|>
|> Thanks.
|>
|> -colin
|> --
|> col·ly·wob·bles –noun (used with a singular or plural verb)
|> Informal.
|> 1. intestinal cramps or other intestinal disturbances.
|> 2. a feeling of fear, apprehension, or nervousness.
|
|
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFHp3WomNQ94Hj+bPcRAr7JAKCp3UKJnceZHyCpiuHb/z6ZcUp+mwCffcNb
CnQvGvr89NgSIZnzU3CR7Q0=
=JwIN
-----END PGP SIGNATURE-----