---
title: 'This week in #dev (Aug 30, 2024)'
teaser: 'Testing block execution in RSpec, simplifying dropdown field creation in
  Rails, and more.

  '
tags: this week in dev,tip,ruby,rails,testing
author: thoughtbot
published_on: 2024-09-06
---

Welcome to another edition of [This Week in #dev](https://thoughtbot.com/blog/tags/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][silumesii] discovered that the Gemfile can utilize a
`.tool-versions` file to specify the Ruby version:

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

ruby file: ".tool-versions"
```

[silumesii]: https://thoughtbot.com/blog/authors/silumesii-maboshe

## Testing Block Execution in RSpec

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

```ruby
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:

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

[neilson]: https://thoughtbot.com/blog/authors/neil-carvalho
[RSpec’s `yield_control`]: https://www.rubydoc.info/github/rspec/rspec-expectations/RSpec%2FMatchers:yield_control

## Simplifying dropdown field creation in Rails

[Matheus Richard][matheus] 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:

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

can be improved to:

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

[matheus]: https://thoughtbot.com/blog/authors/matheus-richard
[Rails' `index_by`]: https://api.rubyonrails.org/classes/Enumerable.html#method-i-index_by

## Thanks

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