Ruby
I recently have a requirement that need optional keyword in method call like this:
method( admin: 'admin' )
method( admin: 'admin', optional_k: 'external message here' )
It's easy to use
def method(option)
admin = option.delete(:admin)
optional = option
end
But it's not safe, and not clean for users.
There's another nice solution with Ruby 2.0 keyword argument but a little magic:
def method(admin: , **option)
puts admin
puts option
end
Safe!! Clean!!
Isn't it ?
Published at 2016.06.06
© Creative Commons - ShareAlike - NonCommercial - Attribution