“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it...”

Rob Lacey
Senior Software Engineer, Copenhagen, Denmark

It's all about self

I like to be discreet when it codes to snippets of code. I dislike this variable assignment only to use it once on the next line

today = Date.today
today = [today.year, today.year, "%02d" % today.month, "%02d" % today.day]

We need something like a map because the syntax is cleaner, but we don’t really need an array to achieve this surely?

[Date.today].map { |d| d.year, d.year, "%02d" % d.month, "%02d" % d.day }.first

How about this. It allows a block that yields self but we need to force it to return the contents of the block. This is hacky.

Date.today.tap { |d| break [d.year, "%02d" % d.month, "%02d" % d.day] }

So in comes then which is an alias of you guess it yield_self. Nice.

Date.today.then { |d| [d.year, "%02d" % d.month, "%02d" % d.day] }

OK, this is a silly example because this no one would ever do this. You’d do this…

Date.today.strftime("%Y%m%d")