This week in #dev (Aug 30, 2024)

thoughtbot
Edited by Matheus Richard

Welcome to another edition of This Week in #dev, a series of posts where we bring some of our most interesting Slack conversations to the public.

Discovering Gemfile’s .tool-versions Feature for Ruby Versioning

Silumesii Maboshe discovered that the Gemfile can utilize a .tool-versions file to specify the Ruby version:

# in Gemfile
source "https://rubygems.org"

ruby file: ".tool-versions"

Testing Block Execution in RSpec

Neil Carvalho discusses a common spec pattern for testing blocks, which involves setting a flag to true when a method is executed. He provides an example:

executed = false

object_under_test.method_under_test do
  executed = true
end

expect(executed).to eq true

He mentions RSpec’s yield_control as a more elegant alternative:

expect { |b| object_under_test.method_under_test(&b) }.to yield_control

Simplifying dropdown field creation in Rails

Matheus Richard talks about the common practice of creating a select/dropdown field from an array while generating labels using methods like humanize or titleize. He notes that while using numbered arguments can simplify the process, Rails’ index_by method offers an even more concise solution:

= simple_form_for model do |f|
  = f.input :options, collection: ['option_1', 'option_2'].map { |option| [option.titleize, option] }

can be improved to:

= f.input :options, collection: ['option_1', 'option_2'].index_by(&:titleize)

Thanks

This edition was brought to you by Matheus Richard, Neil Carvalho, and Silumesii Maboshe. Thanks to all contributors! 🎉