Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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