More in Ruby
Random picks
Read a file into a string in Go
Systems
os.ReadFile replaced ioutil.ReadFile in 1.16 and is the one-liner for small files.
Walk a directory tree lazily
Python
pathlib keeps the paths as objects rather than strings, so joining and suffix checks stay readable.
Prepared statement with PDO
PHP
Placeholders rather than interpolation. This is the whole of SQL injection defence in PHP.
Undo the last commit but keep the changes
Git
Moves HEAD back one commit and leaves everything staged, which is what you usually want after committing too early.
Check a malloc result in C
Systems
The allocation that nobody checks is the one that fails in production.
Find the commit that introduced a bug
Git
Binary search across history. Mark a known good and a known bad commit and git narrows it down for you.
Nullable reference types in C#
Systems
Turning the feature on makes the compiler point at every place a null could slip through.
A dataclass with validation
Python
__post_init__ runs after the generated constructor, which is where cheap invariants belong.