Blog
About
Hire Me

Kevin Curtin

Blog
About
Hire Me
Kevin Curtin
September 6, 2015

Setting Default Values With Struct

Kevin Curtin
September 6, 2015

Sometimes it’s useful to set a default value for a Struct. If either value is empty it will return the default value specified after the || operator.

require 'date'

DateRange = Struct.new(:start_date, :end_date) do
  def start_date
    self[:start_date] || Date.today - 30.days
  end

  def end_date
    self[:end_date] || Date.today
  end
end
Newer PostDebugging With Git Bisect
Older PostAccessing Data With 'Fetch'
Back to Top
kevin@kjcurtin.com