How to add optional keyword to Ruby2.0 keyword argument?


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

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

Safe!! Clean!!

Isn't it ?

Published at 2016.06.06