質問

What are starred variables like *arr?

*arr = "sayuj"
=> ["sayuj"]

*arr = *%w{i am happy}
=> ["i", "am", "happy"]

*arr = %w{i am happy}
=> [["i", "am", "happy"]]
役に立ちましたか?

解決

It's called the splat operator, and it can collect elements into an array (applied to an un-bound variable) or split an array into individual elements (applied to an array).

def bar(*a)
  a[1]
end

bar(1,2,3)
=> 2

def foo(a,b,c)
  b
end

foo(*[1,2,3])
=> 2

That's a little over-simplified, read the linked post for more in-depth information.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top