Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does double splat (**) argument mean in this code example and why use it?

So I've been over Traiblazer and Reform documentation and I often see this kind of code

class AlbumForm < Reform::Form
  collection :songs, populate_if_empty: :populate_songs! do
    property :name
  end

  def populate_songs!(fragment:, **)
    Song.find_by(name: fragment["name"]) or Song.new
  end
end

Notice the def populate_songs!(fragment:, **) definition?

I'm well aware of double splat named arguments (like **others) that capture all other keyword arguments. But I've never seen ** alone, without a name.

So my 2 questions are:

  1. what does ** mean in the block above?
  2. why use this syntax?
like image 514
Dorian Avatar asked Oct 25 '25 04:10

Dorian


1 Answers

what does ** mean in the block above?

It's a kwsplat, but it's not assigned a name. So this method will accept arbitrary set of keyword arguments and ignore all but :fragment.

why use this syntax?

To ignore arguments you're not interested in.


A little demo

class Person
  attr_reader :name, :age

  def initialize(name:, age:)
    @name = name
    @age = age
  end

  def description
    "name: #{name}, age: #{age}"
  end
end

class Rapper < Person
  def initialize(name:, **)
    name = "Lil #{name}" # amend one argument
    super # send name and the rest (however many there are) to super
  end
end

Person.new(name: 'John', age: 25).description # => "name: John, age: 25"
Rapper.new(name: 'John', age: 25).description # => "name: Lil John, age: 25"
like image 78
Sergio Tulentsev Avatar answered Oct 26 '25 19:10

Sergio Tulentsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!