Reputation: 2236
I used to have this method:
def send_action(action, &success_block)
end
Which I could call like this:
send_action('PAIR') do
pp 'test
end
Now I want to add an optional parameter:
def send_action(action, uuid = nil, &success_block)
end
But that doesn't seem to work (which I though). So I tried writing it with named parameters:
def send_action(action:, uuid: nil, &success_block)
end
But how can I combine named parameters with a block?
Upvotes: 1
Views: 987
Reputation: 164699
Both work with Ruby 2.4.4 and 2.6.4. Here's a demonstration with positional parameters.
def send_action(action, uuid = nil, &success_block)
p "#{action} #{uuid}"
success_block.call
end
send_action("foo") { p 99 }
"foo "
99
send_action("foo", "bar") { p 99 }
"foo bar"
99
And with named parameters.
def send_action(action:, uuid: nil, &success_block)
p "#{action} #{uuid}"
success_block.call
end
send_action(action: "foo") { p 99 }
"foo "
99
send_action(action: "foo", uuid: "bar") { p 99 }
"foo bar"
99
Upvotes: 2