splat

Ruby, Source Code of Splat?

亡梦爱人 提交于 2019-12-13 12:01:46
问题 Someone asked about the splat operator yesterday, and I wanted to see the source code... would that be written in C or in Ruby? Where would it be found? 回答1: The splat operator is poorly documented in the core Ruby documentation as of Ruby 2.4. It's a core feature of the language, though, and the source code for the splat operator can be found in vm_eval.c under rb_yield_splat(VALUE values). The unit test for rb_yield_splat makes it clearer what is happening: it "yields with passed array's

ruby: adding numbers and printing true when sums are 21

徘徊边缘 提交于 2019-12-13 02:35:06
问题 It's a simple problem given on rubeque.com: write a method that takes any number of integers and adds them to return true if the sum is 21. False otherwise. It tests the input with: assert_equal twenty_one?(3, 4, 5, 6, 3), true assert_equal twenty_one?(3, 11, 10), false Here's what I have so far: def twenty_one?(*nums) nums.inject(&:+) end if twenty_one? == 21 puts true else false end But I get the error message: RuntimeError: The value '21' does not equal 'true'. I'm really confused on how

Ruby: How can I kill “warning: `*' interpreted as argument prefix”? [duplicate]

北城余情 提交于 2019-12-12 11:15:27
问题 This question already has answers here : Why does white-space affect ruby function calls? (2 answers) Closed 2 years ago . How can I remove the "warning: `*' interpreted as argument prefix" from the following code? hash = {"a" => 1, "b" => 2, "s" => 3,} if "string".start_with? *hash.keys then puts "ok" else puts "ng" end When I run the code above, I get: $ ruby -w /tmp/a.rb /tmp/a.rb:5: warning: `*' interpreted as argument prefix ok What is the best way to fix this warning? I've tried to put

Splatting a function with an object's property

牧云@^-^@ 提交于 2019-12-11 07:59:09
问题 In PowerShell you can pass multiple parameters to a function or cmdlet by wrapping them in a hashtable variable, then passing that variable prefixed with @ instead of $ . Is it possible to splat with a hashtable which is a property of another object (i.e. as a one liner)? e.g. below I first have to assign the property ( testInt , testString ) to another variable before I can splat it to Demo . I'd like some way to avoid this additional step, but haven't been able to find a neat solution...

Local or remote execution of powershell script with generic parameters

风流意气都作罢 提交于 2019-12-11 03:22:19
问题 In a development team, I would like to have the same test scripts to be executed locally by a developper or remotely by our test platform. Here is what I would like to use as premises for each script # Test local/remote execution by reading C:\ directory param( [switch] $verbose, [switch] $remote, [string] $ip, [string] $user, [string] $password #Add here script specific parameters ) Write-Host "Command invokation incoming parameter count : " $psboundparameters.count if ($remote) { $Params =

Is there an easier alternative to mimicking the splat operator?

我怕爱的太早我们不能终老 提交于 2019-12-07 02:21:28
问题 I've found it's available in Ruby, but I recognize it from what I've done in Python; the "splat" operator. Long story short, I'm wondering if there's a simpler way to accomplish what I currently am, mimicking what the "splat" operator does. I made a central method that the rest can call because I realized I have several very similar ones, and they were all doing the same except for a few minor things. Here's the method signature: private String callScript(String scriptLocation, String...

CoffeeScript: Expand array in function call

断了今生、忘了曾经 提交于 2019-12-06 23:49:32
问题 In Ruby I can call methods with array elements used as positional parameters like this method(fixed_arg1, fixed_arg2, *array_of_additional_args) Here the "*" operator expands the array in place. I'm trying to do the same in CoffeeScript, but haven't found a way. Specifically, I want to pass additional arguments in a call to a jQuery function $('#my-element').toggle(true, *config.toggleOptions) The syntax above does not work, obviously, and I'm looking for a way that does. 回答1: Try $('#my

proper name for python * operator?

强颜欢笑 提交于 2019-12-05 20:17:47
问题 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

Is there an easier alternative to mimicking the splat operator?

本小妞迷上赌 提交于 2019-12-05 04:47:31
I've found it's available in Ruby, but I recognize it from what I've done in Python; the "splat" operator. Long story short, I'm wondering if there's a simpler way to accomplish what I currently am, mimicking what the "splat" operator does. I made a central method that the rest can call because I realized I have several very similar ones, and they were all doing the same except for a few minor things. Here's the method signature: private String callScript(String scriptLocation, String... extraArgs) throws Exception { I want to require at least one argument (the scriptLocation ), and then allow

CoffeeScript: Expand array in function call

心不动则不痛 提交于 2019-12-05 03:33:22
In Ruby I can call methods with array elements used as positional parameters like this method(fixed_arg1, fixed_arg2, *array_of_additional_args) Here the "*" operator expands the array in place. I'm trying to do the same in CoffeeScript, but haven't found a way. Specifically, I want to pass additional arguments in a call to a jQuery function $('#my-element').toggle(true, *config.toggleOptions) The syntax above does not work, obviously, and I'm looking for a way that does. Try $('#my-element').toggle(true, config.toggleOptions...) You need to splat it. fun(1,2,3,4,5) fun = (first, second, rest.