Primitive Obsession

Instead of making a class, defining a primitive field is much simpler. However, if you keep adding fields more and more, eventually, it becomes huge. One way to solve this problem is to replace data value with object.

# dollars/cents

[5, 30]
# OR
{
  dollars: 5.
  cents: 30
}
# Dave with age 30

['Dave', 30]
# OR
{
  name: 'Dave'.
  age: 30
}

Replace with an object

class Money
  attr_reader :dollars, :cents

  def from_cents(cents)
    new(cents/100, cents%100)
  end

  def initialize(dollars, cents)
    @dollars, @cents = dollars, cents
  end
end

p Money.new.from_cents(530) #<Money:... @dollars=5, @cents=30>

Taeyang Lee

Taeyang Lee
I really enjoy taking on tasks which are out of my comfort zone and using them as a great way to learn the necessary tools to complete it.

Monads

Published on December 17, 2018

Functors

Published on December 16, 2018