Skip to content

Instantly share code, notes, and snippets.

@rondy
Last active March 2, 2021 11:14
Show Gist options
  • Save rondy/a9f167cca216dfb51d1dc1fac56755b1 to your computer and use it in GitHub Desktop.
Save rondy/a9f167cca216dfb51d1dc1fac56755b1 to your computer and use it in GitHub Desktop.

Functional objects on Ruby programming language

  • Start class names with a verb;
  • Public contract is a #call method;
    • Enables SRP;
    • Enables composition;
    • Enables polymorphism;
    • Enables to receive Proc/callables objects;
  • Receive stateful/impure functions through the object initializer;
    • This permits easy mocking/substitution.
  • Receive pure function inputs through the #call method;
  • This resembles curry-like functions;
  • The #call calling always returns a value, preferably 'result'-like objects;
    • This avoids 'primitive obssesion' anti-pattern;
    • Consider monadics operations;
  • Define stateful/impure functions as default implementation, in case nothing is received;
  • Stateful/impure functions are always returned as closures/lambda;
  • Object state is uded only for atateful/impure collaborators.
    extract_utm_campaign_value_from(url) # is pure
    get_page_title.call(url)             # is impure
  • Message passing is natural;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment