Skip to content

Instantly share code, notes, and snippets.

@gxespino
Last active January 9, 2018 22:15
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 gxespino/214021298caa458c3b842acc53dce984 to your computer and use it in GitHub Desktop.
Save gxespino/214021298caa458c3b842acc53dce984 to your computer and use it in GitHub Desktop.

Functional Ruby - Notes

  • immutable (don't modify inputs)
  • prefer stateless objects

Prevent access to initial state

class UserQuery
  def initialize(query)
    @query = query
  end
  
  def with(query)
    self.class.new(query)
  end

  def call
    query.execute
  end
end

Consider: use initializer to ONLY inject dependencies pass ALL inputs to methods

  • less coupling
  • instantiate objects once, use them numerous times
  • data (from db or external API) becomes first class
    • make sure this is well defined so that you can compose

Object composition:

input = { name: "Person" }

persist_person.call(
  validate.call(
    coerce.call(input)
  )
)

Lazy function calling

Partially implement function based on arguments passed in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment