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
268
votes
9 answers

proper name for python * operator?

What is the correct name for operator *, as in function(*args)? unpack, unzip, something else?
Anycorn
  • 50,217
  • 42
  • 167
  • 261
259
votes
4 answers

What does a double * (splat) operator do

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?
Roy Lee
  • 10,572
  • 13
  • 60
  • 84
193
votes
3 answers

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

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…
David Burrows
  • 5,217
  • 3
  • 30
  • 34
71
votes
7 answers

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

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…
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
52
votes
3 answers

How do I use Array#dig and Hash#dig introduced in Ruby 2.3?

Ruby 2.3 introduces a new method on Array and Hash called dig. The examples I've seen in blog posts about the new release are contrived and convoluted: # Hash#dig user = { user: { address: { street1: '123 Main street' } …
user513951
  • 12,445
  • 7
  • 65
  • 82
32
votes
1 answer

What does the * (star) mean in Ruby?

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…
Ernest
  • 8,701
  • 5
  • 40
  • 51
19
votes
2 answers

Where is it legal to use ruby splat operator?

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…
Dave Nolan
  • 3,009
  • 4
  • 30
  • 32
18
votes
2 answers

What does the * (asterisk) symbol do near a function argument and how to use that in others scenarios?

I am using Ruby on Rails 3 and I would like to know what means the presence of a * operator near a function argument and to understand its usages in others scenarios. Example scenario (this method was from the Ruby on Rails 3 framework): def…
user502052
  • 14,803
  • 30
  • 109
  • 188
18
votes
2 answers

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

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…
wim
  • 338,267
  • 99
  • 616
  • 750
16
votes
2 answers

Can scala splat be used for anything that isn't a varargs?

given e.g: scala> def pipes(strings:String*) = strings.toList.mkString("|") which I can call normally: scala> pipes("foo", "bar") res1: String = foo|bar or with a splat: scala> val args = List("a","b","c") scala> pipes(args:_*) res2: String =…
gfxmonk
  • 8,614
  • 5
  • 42
  • 53
15
votes
3 answers

Does Haskell have a splat operator like Python and Ruby?

In Python and Ruby (and others as well, I'm sure). you can prefix an enumerable with * ("splat") to use it as an argument list. For instance, in Python: >>> def foo(a,b): return a + b >>> foo(1,2) 3 >>> tup = (1,2) >>> foo(*tup) 3 Is there…
gfxmonk
  • 8,614
  • 5
  • 42
  • 53
15
votes
2 answers

How do I pass an array to a method that accepts an attribute with a splat operator?

If I have a method like: def sum *numbers numbers.inject{|sum, number| sum += number} end How would I be able to pass an array as numbers? ruby-1.9.2-p180 :044 > sum 1,2,3 #=> 6 ruby-1.9.2-p180 :045 > sum([1,2,3]) #=> [1, 2, 3] Note that I…
Jeremy Smith
  • 14,727
  • 19
  • 67
  • 114
14
votes
1 answer

What does *:: (asterisk double colon) do in Ruby?

I was poking through the Rails code today and stumbled upon this snippet: new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday)) What does the asterisk-double-colon (or splat-double-colon if you will) in *::Date do? Presumably it has…
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
13
votes
1 answer

Is there a splat operator (or equivalent) in Matlab?

If I have an array (of unknown length until runtime), is there a way to call a function with each element of the array as a separate parameter? Like so: foo = @(varargin) sum(cell2mat(varargin)); bar = [3,4,5]; foo(*bar) == foo(3,4,5) Context: I…
13
votes
3 answers

How to pass the 'argument-line' of one PowerShell function to another?

I'm trying to write some PowerShell functions that do some stuff and then transparently call through to existing built-in functions. I want to pass along all the arguments untouched. I don't want to have to know any details of the arguments. I…
jwfearn
  • 28,781
  • 28
  • 95
  • 122
1
2 3
11 12