splat

What does the * (star) mean in Ruby? [duplicate]

此生再无相见时 提交于 2019-11-26 22:57:51
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: What is the * operator doing to this string in Ruby Probably there is answer for that elsewhere, but I just don't know how to find it... If I am right, the * means multiple parameters if used in function definition: def hero(name, *super_powers) But what does * do in the code like this: Hash[*[[:first_name, 'Shane'], [:last_name, 'Harvie']].flatten] # => {:first_name=>"Shane", :last_name=>"Harvie"} 回答1: Variable

What does a double * (splat) operator do

柔情痞子 提交于 2019-11-26 21:20:48
Have you seen a function declared like this? def foo a, **b ... end I understand that a single * is the splat operator. What does ** mean? Ruby 2.0 introduced keyword arguments, and ** acts like * , but for keyword arguments. It returns a Hash with key / value pairs. For this code: def foo(a, *b, **c) [a, b, c] end Here's a demo: > foo 10 => [10, [], {}] > foo 10, 20, 30 => [10, [20, 30], {}] > foo 10, 20, 30, d: 40, e: 50 => [10, [20, 30], {:d=>40, :e=>50}] > foo 10, d: 40, e: 50 => [10, [], {:d=>40, :e=>50}] That is the double splat operator which is available since Ruby 2.0. It captures all

Change what the *splat and **splatty-splat operators do to my object

狂风中的少年 提交于 2019-11-26 21:16:00
问题 How do you override the result of unpacking syntax *obj and **obj ? For example, can you somehow create an object thing which behaves like this: >>> [*thing] ['a', 'b', 'c'] >>> [x for x in thing] ['d', 'e', 'f'] >>> {**thing} {'hello world': 'I am a potato!!'} Note: the iteration via __iter__ ("for x in thing") returns different elements from the *splat unpack. I had a look in operator.mul and operator.pow , but those functions only concern usages with two operands, like a*b and a**b , and

Where is it legal to use ruby splat operator?

若如初见. 提交于 2019-11-26 16:09:16
问题 Splats are cool. They're not just for exploding arrays, although that is fun. They can also cast to Array and flatten arrays (See http://github.com/mischa/splat/tree/master for an exhaustive list of what they do.) It looks like one cannot perform additional operations on the splat, but in 1.8.6/1.9 the following code throws "unexpected tSTAR": foo = bar || *zap #=> unexpected tSTAR Whereas this works: foo = *zap || bar #=> works, but of limited value Where can the splat appear in an

What does a double * (splat) operator do

不问归期 提交于 2019-11-26 07:54:04
问题 Have you seen a function declared like this? def foo a, **b ... end I understand that a single * is the splat operator. What does ** mean? 回答1: Ruby 2.0 introduced keyword arguments, and ** acts like * , but for keyword arguments. It returns a Hash with key / value pairs. For this code: def foo(a, *b, **c) [a, b, c] end Here's a demo: > foo 10 => [10, [], {}] > foo 10, 20, 30 => [10, [20, 30], {}] > foo 10, 20, 30, d: 40, e: 50 => [10, [20, 30], {:d=>40, :e=>50}] > foo 10, d: 40, e: 50 => [10

What does the (unary) * operator do in this Ruby code?

自闭症网瘾萝莉.ら 提交于 2019-11-25 23:39:33
问题 Given the Ruby code line = \"first_name=mickey;last_name=mouse;country=usa\" record = Hash[*line.split(/=|;/)] I understand everything in the second line apart from the * operator - what is it doing and where is the documentation for this? (as you might guess, searching for this case is proving hard...) 回答1: The * is the splat operator. It expands an Array into a list of arguments, in this case a list of arguments to the Hash.[] method. (To be more precise, it expands any object that responds

proper name for python * operator?

时光怂恿深爱的人放手 提交于 2019-11-25 23:18:26
问题 What is the correct name for operator * , as in function(*args) ? unpack, unzip, something else? 回答1: In Ruby and Perl 6 this has been called "splat", and I think most people from those communities will figure out what you mean if you call it that. The Python tutorial uses the phrase "unpacking argument lists", which is long and descriptive. I haven't heard any other particular name for it in Python. 回答2: I call it "positional expansion", as opposed to ** which I call "keyword expansion". 回答3