Skip to content

Instantly share code, notes, and snippets.

@motine
Created March 1, 2022 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motine/28da503ba0075e9d64d3f3b1faab9014 to your computer and use it in GitHub Desktop.
Save motine/28da503ba0075e9d64d3f3b1faab9014 to your computer and use it in GitHub Desktop.

DSL Boilerplate

We want something like this:

class LakeSuperior
  include LakeDSL

  lake_name 'Lake Superior'
  fish 'trout'
  fish 'northern pike'
end

And we have to write boilerplate like this:

module LakeDSL
  # dump code
  def lake_name(name = nil)
    return @lake_name if name.nil?
    @lake_name = name
  end

  # super annoying
  def fish(name = nil)
    return @fishes if name.nil?
    @fishes ||= []
    @fishes << name
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment