Skip to content

Instantly share code, notes, and snippets.

@sighmin
Created February 2, 2018 20:42
Show Gist options
  • Save sighmin/f516896b9c60939547d2715380ffce87 to your computer and use it in GitHub Desktop.
Save sighmin/f516896b9c60939547d2715380ffce87 to your computer and use it in GitHub Desktop.
extend self
# A friend of mine Stuart (https://github.com/stuartc) and I discussed this today.
# I think `extend self` adds the classes to the eigenclass of the module, or the eigenmodule? Whatever.
# My code used to look like
class BusinessBusiness
def initialize(foo, bar)
@foo = foo
@bar = bar
end
def call
do_some_things_with_foo_and_bar
end
end
biz = BusinessBusiness.new("foo", "bar")
budget = biz.call
# Now I prefer stateless modules
module BusinessBusiness
extend self
# pure
def something_descriptive(foo, bar)
foo + bar
end
end
budget = BusinessBusiness.something_descriptive("foo", "bar")
# You can also grab the method object to pass around
budget_maker = BusinessBusiness.method(:something_descriptive)
budget_maker.call("foo", "bar")
# or
budgets = OtherService.construct_budgets(budget_maker, ["data", "456", "pew pew"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment