为了账号安全,请及时绑定邮箱和手机立即绑定

在这个Ruby代码中,(一元)*操作符是做什么的?

在这个Ruby代码中,(一元)*操作符是做什么的?

噜噜哒 2019-07-11 16:33:33
在这个Ruby代码中,(一元)*操作符是做什么的?给定Ruby代码line = "first_name=mickey;last_name=mouse;country=usa" record = Hash[*line.split(/=|;/)]我明白第二行的每一件事,除了*操作员-它在做什么,它的文档在哪里?(你可能猜到,寻找这个案子很难.)
查看完整描述

3 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

Splat运算符解压缩传递给函数的数组,以便将每个元素作为单独的参数发送给函数。


一个简单的例子:


>> def func(a, b, c)

>>   puts a, b, c

>> end

=> nil


>> func(1, 2, 3)  #we can call func with three parameters

1

2

3

=> nil


>> list = [1, 2, 3]

=> [1, 2, 3]


>> func(list) #We CAN'T call func with an array, even though it has three objects

ArgumentError: wrong number of arguments (1 for 3)

    from (irb):12:in 'func'

    from (irb):12


>> func(*list) #But we CAN call func with an unpacked array.

1

2

3

=> nil

就这样!


查看完整回答
反对 回复 2019-07-11
  • 3 回答
  • 0 关注
  • 436 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信