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
8
votes
0 answers

ReactiveUI 6.5 - When ReactiveObject is inherited in class I get a build error Splat.dll could not be found

I'm using Visual Studio 13 Community. Created a simple WinForms project, created an empty class and inherited ReactiveObject. I've installed reactivui-winforms 6.5.0 using NuGet. I can see ver 1.0.0 was installed and Splat in references. The…
rreynolds
  • 81
  • 3
8
votes
4 answers

Hash Destructuring

You can destructure an array by using the splat operator. def foo(arg1, arg2, arg3) #...Do Stuff... end array = ['arg2', 'arg3'] foo('arg1', *array) But is there a way to destruct a hash for option type goodness? def foo(arg1, opts) #...Do…
Drew
  • 15,158
  • 17
  • 68
  • 77
7
votes
3 answers

Is there an easier alternative to mimicking the splat operator?

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…
Ian
  • 50,146
  • 13
  • 101
  • 111
6
votes
1 answer

Splatting a struct

I have a bunch of structs like this with increasing number of members, but consistent member naming: struct one { int a; }; struct two { int a; int b; }; struct three { int a; int b; int c; }; I also have a templated function which I want to have…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
2 answers

How to make PHP understand *either* an array or the splat operator (3 dots ...) for function parameters?

I have this code: function test(...$strings) { // ... } It allows me to call test() like this: test('a', 'series of', 'strings go here'); Which works. However, I often would like to do: test([ 'a', 'series of', 'strings go here'…
user11555230
6
votes
1 answer

Ruby: How can I kill "warning: `*' interpreted as argument prefix"?

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,…
Yasushi Shoji
  • 4,028
  • 1
  • 26
  • 47
6
votes
1 answer

Julia: using splat to pass along arguments

I am trying to write a function that calls several functions that accept named parameters. I'd like the A function to be able to named parameters splatted together in args and pass along the matching arguments to the functions it calls. function…
Rob Donnelly
  • 2,256
  • 2
  • 20
  • 29
6
votes
1 answer

Passing splat on nil as argument

All values for b below let me call a method with the *args syntax. def some_method(a) puts a end b = 1 some_method(*b) # => 1 b = false some_method(*b) # => false b = "whatever" some_method(*b) # => "whatever" With nil, I expected to get nil,…
Automatico
  • 12,420
  • 9
  • 82
  • 110
6
votes
2 answers

Ruby to_s conversion to binary (Splat operator in argument)

If I run the following code, the first two lines return what I expect. The third, however, returns a binary representation of 2. 2.to_s # => "2" 2.to_s * 2 # => "22" 2.to_s *2 # => "10" I know that passing in 2 when calling to_s will…
slapthelownote
  • 4,249
  • 1
  • 23
  • 27
5
votes
2 answers

What's the feature in Ruby that allows "p *1..10" to print out the numbers 1-10?

require 'pp' p *1..10 This prints out 1-10. Why is this so concise? And what else can you do with it?
Paul H.
  • 53
  • 3
5
votes
3 answers

Handling a method with an optional argument followed by a list of keyword arguments

I want to pass an optional argument (any datatype, even a Hash) followed by a list of keyword arguments (which can be empty) to a method. This is what I got: def my_method(subject = nil, **options) [subject, options] end The method output is as…
Taschetto
  • 321
  • 3
  • 14
5
votes
1 answer

Using splat operator with when

Case statement: case x when 1 "one" when 2 "two" when 3 "three" else "many" end is evaluated using the === operator. This operator is invoked on the value of the when expression with the value of the case expression as the argument. The…
X. Wang
  • 973
  • 1
  • 11
  • 21
5
votes
2 answers

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

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…
Severin
  • 8,508
  • 14
  • 68
  • 117
5
votes
3 answers

What does a * in front of a string literal do in ruby?

This code seems to create an array with a range from a to z but I don't understand what the * does. Can someone please explain? [*"a".."z"]
MakeM
  • 107
  • 5
5
votes
3 answers

Ruby rubocop: how to freeze an array constant generated with splat

I'm assigning an array constant like this: NUMS = *(2..9) Rubocop says C: Freeze mutable objects assigned to constants.NUMS = *(2..9)               ^^^^^ So I try NUMS = *(2..9).freeze Rubocop says C: Freeze mutable objects assigned to…
dirkdirk
  • 171
  • 2
  • 10
1 2
3
11 12