Questions tagged [double-splat]

A syntax for keyword arguments in Python and Ruby, e.g. **kwargs

In method definitions, **kwargs is used to take arbitrary keyword arguments. In method calls, it attempts to convert the object (usually a dictionary / hash) to keyword arguments.

Further references:

12 questions
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
13
votes
3 answers

Keyword arguments unpacking (splat) in Ruby

What is happening below seems a little strange to me. def f(a, b) puts "#{a} :: #{b}" end f(*[1, 2], **{}) # prints "1 :: 2" hash = {} f(*[1, 2], **hash) ArgumentError: wrong number of arguments (3 for 2) f(*[1, 2], **Hash.new) ArgumentError:…
Vlad M
  • 477
  • 3
  • 10
10
votes
2 answers

Double-splat operator destructively modifies hash – is this a Ruby bug?

I noticed what I find to be a very surprising behavior with the ** (double-splat) operator in Ruby 2.1.1. When key-value pairs are used before a **hash, the hash remains unmodified; however, when key-value pairs are only used after the **hash, the…
user513951
  • 12,445
  • 7
  • 65
  • 82
9
votes
1 answer

What is the point of using Ruby's double-splat (`**`) in method calls?

With a single splat, we can expand an array into multiple arguments, which is pretty different from passing the array directly: def foo(a, b = nil, c = nil) a end args = [1, 2, 3] foo(args) # Evaluates to foo([1, 2, 3]) => [1, 2, 3] foo(*args)…
Renato Zannon
  • 28,805
  • 6
  • 38
  • 42
5
votes
2 answers

Parameter destructuring (equivalent of python's double-splat)

In python I can pass a dict whose keys match parameters' names with the ** (double-splat) operator: def foo(a, b): print (a - b) args = {'b': 7, 'a': 10} foo(**args) # prints 3 How to do the same in ES6? This doesn't work: function foo(a,…
georg
  • 211,518
  • 52
  • 313
  • 390
4
votes
2 answers

Double splat on `nil`

My understanding is that a single splat on a non-array object calls to_a and then dissociates the elements apart. And since nil.to_a is defined to be [], the following conversion happens: [:foo, *nil, :bar] # => [:foo, *nil.to_a, :bar] # => [:foo,…
sawa
  • 165,429
  • 45
  • 277
  • 381
3
votes
2 answers

Make Ruby object respond to double splat operator **

I have a library that has an #execute method like this def execute(query, **args) # ... end I have a class that generates the data for args (which has a lot of logic depending on user abilities) class Abilities def to_h { user: user } # and…
23tux
  • 14,104
  • 15
  • 88
  • 187
3
votes
1 answer

Ruby double splat parameter is too greedy with Hash argument

In Ruby 2.4.1, I have a method like this: def example(*args, **kwargs) p args p kwargs end I can pass in positional arguments that are not Hash just fine: irb(main):001:0> example("Greetings") ["Greetings"] {} And if I want to use named…
Deltik
  • 1,129
  • 7
  • 32
2
votes
1 answer

Expanding empty hash in variable with double splat in Ruby

I got this strange behavior trying to expand the hash variable using double splat. Do not know why this is happening. My ruby version ruby 2.2.6p396 (2016-11-15 revision 56800) Scenario class MyClass def my_method;…
rafaels88
  • 839
  • 1
  • 6
  • 19
1
vote
1 answer

What does the double-splat do in a method call?

While preparing for the Ruby Association Certified Ruby Programmer Exam, I was solving the prep test and came upon this scenario: def add(x:, y:, **params) z = x + y params[:round] ? z.round : z end p add(x: 3, y: 4) #=> 7 // no surprise…
Amin Shah Gilani
  • 8,675
  • 5
  • 37
  • 79
1
vote
0 answers

Chaining double-splats in Ruby

Is the following behavior a bug or a feature? class A def initialize(**options) @options = options end def foo(**options) bar(**@options, **options) end def bar(**options) end def print puts @options.inspect end end a…
Ivan
  • 153
  • 1
  • 6
0
votes
2 answers

two level splatter TCL

If I have a procedure or a command in TCL, with variable number of arguments, one can use, if a list's elements are as an input, the "splatter" operator, for example: set a [list "ko" ] set m [ list "ok" "bang" ] lappend a {*}$m But, what if I want…
user1134991
  • 3,003
  • 2
  • 25
  • 35