Skip to content

Comment on Keyword Arguments: Ruby, Clojure, Common Lispparent

Comments

Here's an example of how I use the hash as keyword arguments. (Minus the documentation

    def save_record(record, options={}, &block)
      return nil if record.nil?
      options = {
        :add_params => {},
        :as => nil,
        :without_protection => false,
        :params => params[record.class.name.underscore.to_sym],
        :user => current_user,
      }.update options
The important part is: options = {}.update(options)

This pattern works very well for providing defaults and some documentation on what options are available. I still document the options before the function.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.