What's the meaning of the semicolon after the id-parameter? Please see my comment within the snippet.
That isn't a Ruby-symbol. Isn't it? Otherwise the semicolon would be on the left-side. The ID is an integer. A bit confused currently ...
field :vendor, VendorType, null: false,
description: "A single car-vendor." do
argument :id, ID, required: true
end
def vendor(id:) # Why is there a semicolon after "id"?
Vendor.find(id)
end
The syntax is defining a keyword argument that the caller has to explicitly state:
def v(id:)
p id
end
v(1) # error
v(id: 1)
1
=> 1
v(something: 1)
ArgumentError (missing keyword: id)
It has its uses, primarily for interaction in the console and ensuring the user knows what they are doing before they call the method, or ensuring new developers of the codebase understand how to call the method.
If the required argument is missing, Ruby throws a nice ArgumentError which is concise and reduces the cognitive overhead for future developers to engage with a codebase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With