splat

proper name for python * operator?

依然范特西╮ 提交于 2019-12-04 02:37:01
What is the correct name for operator * , as in function(*args) ? unpack, unzip, something else? 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. I call it "positional expansion", as opposed to ** which I call "keyword expansion". Danilo Piazzalunga The Python Tutorial simply calls it 'the * -operator'. It performs unpacking of arbitrary

How to define a method in ruby using splat and an optional hash at the same time? [duplicate]

陌路散爱 提交于 2019-12-04 00:42:26
问题 This question already has answers here : Optional argument after splat argument (6 answers) Closed 5 years ago . I am able to define a method like this: def test(id, *ary, hash_params) # Do stuff here end But this makes the hash_params argument mandatory. These don't work either: def t(id, *ary, hash_params=nil) # SyntaxError: unexpected '=', expecting ')' def t(id, *ary, hash_params={}) # SyntaxError: unexpected '=', expecting ')' Is there a way to make it optional? 回答1: You can't do that.

What is the standalone splat operator (*) used for in Ruby?

谁都会走 提交于 2019-12-03 18:05:24
问题 I just came across this example where the splat operator is used by itself in a method definition: def print_pair(a,b,*) puts "#{a} and #{b}" end print_pair(1,2,3,:cake,7) #=> 1 and 2 It is clear what and why you would use it in a context like so: def arguments_and_opts(*args, opts) puts "arguments: #{args} options: #{opts}" end arguments_and_opts(1,2,3, a: 5) #=> arguments: [1, 2, 3] options: {:a=>5} But why and how would you use it in the first example? Since it is defined in the Ruby specs

Why does splatting create a tuple on the rhs but a list on the lhs?

流过昼夜 提交于 2019-12-03 10:22:27
问题 Consider, for example, squares = *map((2).__rpow__, range(5)), squares # (0, 1, 4, 9, 16) *squares, = map((2).__rpow__, range(5)) squares # [0, 1, 4, 9, 16] So, all else being equal we get a list when splatting on the lhs and a tuple when splatting on the rhs. Why? Is this by design, and if yes, what's the rationale? Or, if not, are there any technical reasons? Or is this just how it is, no particular reason? 回答1: The fact that you get a tuple on the RHS has nothing to do with the splat. The

Why does splatting create a tuple on the rhs but a list on the lhs?

谁说我不能喝 提交于 2019-12-03 01:58:24
Consider, for example, squares = *map((2).__rpow__, range(5)), squares # (0, 1, 4, 9, 16) *squares, = map((2).__rpow__, range(5)) squares # [0, 1, 4, 9, 16] So, all else being equal we get a list when splatting on the lhs and a tuple when splatting on the rhs. Why? Is this by design, and if yes, what's the rationale? Or, if not, are there any technical reasons? Or is this just how it is, no particular reason? The fact that you get a tuple on the RHS has nothing to do with the splat. The splat just unpacks your map iterator. What you unpack it into is decided by the fact that you've used tuple

Pass arguments by reference to a block with the splat operator

南笙酒味 提交于 2019-12-01 21:00:37
问题 It seems that the arguments are copied when using the splat operator to pass arguments to a block by reference. I have this: def method a = [1,2,3] yield(*a) p a end method {|x,y,z| z = 0} #=> this puts and returns [1, 2, 3] (didn't modified the third argument) How can I pass these arguments by reference? It seems to work if I pass the array directly, but the splat operator would be much more practical, intuitive and maintainable here. 回答1: In Ruby when you write x = value you are creating a

splat over JavaScript object (with new)?

烂漫一生 提交于 2019-12-01 20:42:26
问题 How do I splat across objects without using ECMA6 features? Attempt function can(arg0, arg1) { return arg0 + arg1; } function foo(bar, haz) { this.bar = bar; this.haz = haz; } myArgs = [1,2]; With can I can just do: can.apply(this, myArgs); When trying with foo : new foo.apply(this, myArgs); I get this error (because I'm calling new ): TypeError: function apply() { [native code] } is not a constructor 回答1: Using Object.create function foo(bar, haz) { this.bar = bar; this.haz = haz; } x =

splat over JavaScript object (with new)?

亡梦爱人 提交于 2019-12-01 19:50:46
How do I splat across objects without using ECMA6 features ? Attempt function can(arg0, arg1) { return arg0 + arg1; } function foo(bar, haz) { this.bar = bar; this.haz = haz; } myArgs = [1,2]; With can I can just do: can.apply(this, myArgs); When trying with foo : new foo.apply(this, myArgs); I get this error (because I'm calling new ): TypeError: function apply() { [native code] } is not a constructor Using Object.create function foo(bar, haz) { this.bar = bar; this.haz = haz; } x = Object.create(foo.prototype); myArgs = [5,6]; foo.apply(x, myArgs); console.log(x.bar); Ven Using Object.create

How to define a method in ruby using splat and an optional hash at the same time? [duplicate]

浪子不回头ぞ 提交于 2019-12-01 03:23:11
This question already has an answer here: Optional argument after splat argument 6 answers I am able to define a method like this: def test(id, *ary, hash_params) # Do stuff here end But this makes the hash_params argument mandatory. These don't work either: def t(id, *ary, hash_params=nil) # SyntaxError: unexpected '=', expecting ')' def t(id, *ary, hash_params={}) # SyntaxError: unexpected '=', expecting ')' Is there a way to make it optional? You can't do that. You have to think about how Ruby would be able to determine what belongs to *ary and what belongs to the optional hash. Since Ruby

What does this mean in Ruby language?

此生再无相见时 提交于 2019-11-30 21:35:27
Run the following code, a = [1, 2, 3, 4, 5] head, *tail = a p head p tail You will get the result 1 [2, 3, 4, 5] Who can help me to explain the statement head,*tail = a , Thanks! head, *tail = a means to assign the first element of the array a to head , and assign the rest of the elements to tail . * , sometimes called the "splat operator," does a number of things with arrays. When it's on the left side of an assignment operator ( = ), as in your example, it just means "take everything left over." If you omitted the splat in that code, it would do this instead: head, tail = [1, 2, 3, 4, 5] p