On Nov 19, 2013, at 2:29 AM, Previn Lin <lists / ruby-forum.com> wrote:

> Gennady Bystritsky wrote in post #1127842:
>> ......
>>=20
>> %x[ csh -c 'source test.csh; env' ].lines.each do |_line|
>>   ENV.store *_line.chomp.split('=3D', 2)
>> end
>=20
> Hi Gennady,
>=20
> May I ask one more question, how to understand the star(*) in=20
> *_line.chomp.split('=3D', 2)?

Previn,=20

Sorry for the delay in replying, did not have an opportunity to review =
the list communication. Thanks to Michael Bostler, you got an =
explanation earlier :-). Ruby=92s * in front of an array is called =
=93splat operator=94. For one, it is used to perform in-place array =
expansion for method parameters. The following irb session may help in =
understanding the concept:

irb(main):001:0> def f(p1, p2)
irb(main):002:1> puts "P1: #{p1.inspect}, P2: #{p2.inspect}"
irb(main):003:1> end
=3D> nil
irb(main):004:0> a =3D [ 1, 2 ]
=3D> [1, 2]
irb(main):005:0> f(a)
ArgumentError: wrong number of arguments (1 for 2)
        from (irb):5:in `f'
        from (irb):5
        from :0
irb(main):006:0> f(*a)
P1: 1, P2: 2
=3D> nil
irb(main):007:0> a =3D [ 1, 2, 3 ]
=3D> [1, 2, 3]
irb(main):008:0> f(*a)
ArgumentError: wrong number of arguments (3 for 2)
        from (irb):8:in `f'
        from (irb):8
        from :0
irb(main):009:0>

Notice that method f(p1, p2) accepts exactly 2 parameters. So invocation =
f(a) fails as it gets only one parameter (array a). However f(*a) =
succeeds when array a contains exactly 2 parameters, with a[0] becoming =
p1 and a[1] - p2. However, when a has 3 elements, f(*a) fails with =
diagnostics that 3 parameters are passed while 2 expected.

More discussions may be found here:=20
http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator

Hope this helps,
Gennady.

>=20
> --=20
> Posted via http://www.ruby-forum.com/.