Skip to content

Instantly share code, notes, and snippets.

View hugohernani's full-sized avatar

Hugo Silva hugohernani

View GitHub Profile
class NumberFormatter
def format(_number)
raise NotImplementedError, "subclass should specify its formatting details"
end
end
class NonPositiveFormatter < NumberFormatter
def format(_number)
"\n" # nothing to print
end
@hugohernani
hugohernani / anagrams.exs
Created February 18, 2021 03:56
Elixir code example on running agent and task to create a dictionary of anagrams (extracted from Thomas Dave book)
defmodule Dictionary do
@name __MODULE__
## API
def start_link,
do: Agent.start_link(fn -> %{} end, name: @name)
def add_words(words),
do: Agent.update(@name, &do_add_words(&1, words))
@hugohernani
hugohernani / ar_model_required_attrs.rb
Created March 11, 2016 17:47
Get Attributes which presence validator was added to a model_(instance|klass)
def required_attributes(obj)
target = obj.class == Class ? obj : obj.class
target.validators.select do |amv|
amv.class == ActiveRecord::Validations::PresenceValidator
end.map(&:attributes).flatten
end