BWeave Ramblings, maybe, when I get around to it.

Vim/Nvim: Open multiple files in splits

Open multiple files in Vim/Nvim in splits with the -o (top/bottom) and -O (side-by-side) CLI flags.

vim -o foo.rb foo_test.rb
nvim -O bar.rb bar_test.rb

Wanna get fancy? Here’s a handy way to work on Exercism.io Ruby exercises in Neovim using the built in terminal emulator along with the rerun gem to watch for changes and, you guessed it, rerun your tests.

nvim -O *.rb term://"rerun -x --dir . --pattern '*.rb' -- ruby -r minitest/pride *_test.rb"

Take it to the next level with aliases for multiple Exercism tracks. Add this to your .bashrc or .zshrc to work with Ruby and Python:

alias exrb='nvim -O *.rb term://"rerun -x -b --dir . --pattern \"*.rb\" -- ruby -r minitest/pride *_test.rb"'
alias expy='nvim -O *.py term://"rerun -x -b --dir . --pattern \"*.py\" -- pytest *_test.py"'

Python List Comprehensions

Python List Comprehensions are p rad. They go like this:

variable = [out_exp for out_exp in input_list if out_exp == 2]

Here’s a fun example taken from Exercism.io’s Python track:

number = 105
rain_drops = {3: "Pling", 5: "Plang", 7: "Plong"}
rain_sounds = [sound for value, sound in raindrops.items() if number % value == 0]
# rain_sounds: "PlingPlangPlong"

ActiveRecord upsert_all

ActiveRecord has an upsert_all. It “… updates or inserts (upserts) multiple records into the database in a single SQL INSERT statement” without instantiating ActiveRecord objects.

So the next time you need to update multiple records, like in my case so set the position attribute of multiple drag-n-drop-ed records, you can do something like this and only make two database queries!

ids_and_positions = {1: 2, 2: 3, 3: 1}
records = MyRecord.where(id: ids_and_positions.keys)
records_attributes = records.map do |record| # first query, thanks AR lazy loading!
  record.attributes.merge(position: ids_and_position[record.id])
end
MyRecord.upsert_all(records_attributes) # second query

It’s worth being aware that this is a “sharp knife” tool and bypasses validations.

Also, pay special attention to the nuances of the returning and unique_by args in the docs.

Pry has a whereami

Ever forget where you started when doing a bunch of debugging in Pry? I know I do! Quickly see where you started with the whereami command. Ezpz!

In with the new, maybe

It’s time for something new. The old site was meh.

🍻 Here’s to a fresh start, and hopefully, actually writing some posts.