Questions tagged [splat]

The `*` operator as used in Ruby. So called because it looks like an insect that's splatted on a windscreen.

The * operator as used in Ruby. So called because it looks like an insect that's splatted on a windscreen.

166 questions
4
votes
2 answers

How to annotate type using the splat operator

How (is it possible) to annotate the type of arguments when using the splat operator? f(x, y) = x^2 + y^2 vec = [1.0, 2.0, 'a'] f(vec[1:2]...) How can I annotate that use of ... in the function call. Also notice that none of the macros to view code…
amrods
  • 2,029
  • 1
  • 17
  • 28
4
votes
2 answers

Use of underscores with splats in Ruby

I've followed Why's (Poignant) Guide to Ruby, through couple of other guides, to Ruby style guide to see how Rubyists think. But this is the first time I see trailing underscores. What are these things? Are they useful and if so, when do we use…
James Pond
  • 267
  • 1
  • 4
  • 18
4
votes
2 answers

Why does this use of the Ruby splat throw an error?

I'd like to allow the user to pass an option to a method that can be either a single object or an array. The below code works, assuming `opts[:variable_length_opt] is defined: def initialize(opts={}) @ivar = *opts[:variable_length_opt] end But…
Sean Mackesey
  • 10,701
  • 11
  • 40
  • 66
4
votes
4 answers

Pass arguments by reference to a block with the splat operator

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…
alf
  • 18,372
  • 10
  • 61
  • 92
3
votes
2 answers

Accepting an undefined number of arguments in Ruby/Inline C

I am trying to rewrite a highly recursive function using inline C with Ruby. The function accepts an undefined number of arguments, i.e. it would look like this in Ruby: def each_entity(*types) # Do something and recurse. end I am…
user2398029
  • 6,699
  • 8
  • 48
  • 80
3
votes
1 answer

ReactiveUi: How can I replace the default IViewLocator when using AutoFac?

High! I am trying to replace the default IViewLocator of ReactiveUi/Splat in WPF. I am using AutoFac as container. My goal is very simple: ReactiveUi/Splat should use my custom implementation of IViewLocator when resolving a view for view model. I…
Michael
  • 938
  • 1
  • 10
  • 34
3
votes
0 answers

PHP 8.0.8 / MYSQL ArgumentCountError: The number of elements in the type definition string must match the number of bind variables

I got this error-message: Fatal error: Uncaught ArgumentCountError: The number of elements in the type definition string must match the number of bind variables in /var/www/html/soma/doku_functions.php:600 Stack trace: #0…
Essu
  • 31
  • 3
3
votes
1 answer

How to convert [45,32,56] or "45,32,56" to 42,32,56?

I have a hash from which I want to get specific keys a={1=>32, 23=>23, 24=>232, 56=>123} keys=[23,56] To get values of this keys from a{}, I'm using function a.values_at 23,56 # => [23, 232]` Problem is how do I convert [23,56....] to 23,56
Lakhani Aliraza
  • 435
  • 6
  • 8
3
votes
1 answer

Splatting a function with an object's property

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…
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
3
votes
1 answer

why doesn't splat work directly with command in powershell?

Why does the following work: Get-WmiObject -class Win32_OperatingSystem And this works also: $params = @{class='Win32_OperatingSystem'} Get-WmiObject @params But this doesn't work: Get-WmiObject…
Walter
  • 39
  • 2
3
votes
3 answers

Creating a text based game in Python. How to check for user-input?

I am creating a text based game in Python and need help with splat parameters. I have a function that tests if input is valid, and also allows you to access your inventory. I have two parameters, one that gets the input prompt, and one that is…
Conyare
  • 105
  • 10
3
votes
2 answers

Find the 2nd element of array

I don't understand how this isn't working. The program is supposed to take instance method second in the class Array and return the 2nd object in the array class Array def second(*arr) arr.length <= 1 ? nil : arr[1] end end #Test…
user3466773
  • 176
  • 1
  • 2
  • 14
3
votes
3 answers

Passing a splat parameter from one method to another method

I would like to create methods called 'add' and 'subtract' using splat parameter as below: def add(*numbers) numbers.inject(0) { |sum, n| sum + n } end def subtract(*numbers) numbers[0] - add(numbers[1..-1]) end But it didn't work. What should…
3
votes
3 answers

Map splat arguments over method parameters

We create a method with splatted arguments and call Method#parameters on it: def splatter(x, *y, z); end params = method(:splatter).parameters # => [[:req, :x], [:rest, :y], [:req, :z]] I'm looking for a function f that will map a list of…
rickyrickyrice
  • 577
  • 3
  • 14
3
votes
5 answers

What's the splat doing here?

match, text, number = *"foobar 123".match(/([A-z]*) ([0-9]*)/) I know this is doing some kind of regular expression match but what role does the splat play here and is there a way to do this without the splat so it's less confusing?
uzo
  • 2,821
  • 2
  • 25
  • 26