#​576 — October 28, 2021

Read on the Web

🧶 Some weeks are busy weeks, some weeks are quiet weeks.. this is one of the latter in the Ruby world :-) Luckily Jemma is back with a new Tip of the Week though!
__
Peter Cooper, your editor

Ruby Weekly

Chartkick 4.1: Attractive JavaScript Charts in One Line of Ruby — A long standing library (almost 9 years old!) that saves you from fighting with hooking up whatever the latest JavaScript charting libraries are, if you don't want to. GitHub repo.

Andrew Kane

Publishing Gems Securely with a YubiKey — Package security has continued to pop up as a big issue in several dev communities recently. So adding 2FA to your Rubygems account is the right thing to do, but if you publish a lot, entering OTPs can be a pain, as Aaron tenderly explains.

Aaron Patterson

Another Bug Bites the Dust — Easy-to-use error and performance monitoring tool. Supports 15+ Ruby frameworks (Rails, Sidekiq, Resque, and more). No-brainer pricing, all features are available on all plans. New trial users get a box of Dutch cookies on request.

AppSignal sponsor

Explaining Ruby Fibers — While this post does cover the basics, much of the focus is on now 3.0’s FiberScheduler along with gems like Polyphony are going to make using Fibers much more common.

Sharon Rosner

To Learn a New Language, Read Its Standard Library — The author of Ruby Under a Microscope is learning Crystal (a statically typed, compiled language but that’s heavily inspired by Ruby) using some very simple yet instructive algorithms.

Pat Shaughnessy

QUICK BITS:

Jobs

Principal Ruby Platform Engineer (Remote - EST Timezone +/- 3 hours) — Interested in Fintech? Join our core team building the AI that will level the playing field for self-directed investors.
Propelor

Senior Software Engineer (Ruby) — Join our highly collaborative team, and build products that empower educators and support children’s learning and development.
Himama

Find Ruby Jobs Through Hired — Create a profile on Hired to connect with hiring managers at growing startups and Fortune 500 companies. It's free for job-seekers.
Hired

📕 Articles & Tutorials

A Lesser Known Capability of Ruby's JSON.parse — Since JSON has no concept of Ruby symbols, hashes are returned with string keys which may be annoying to you. Did you know JSON.parse has an option to symbolize keys?

Szymon Fiedler

Testing Your JSON API in Ruby with dry-rb — Testing JSON responses, especially complex ones, can be tedious and brittle, but using dry-schema and dry-validation makes it much easier. You may want to use this for all your APIs.

Paul Götze

Supporting Fractions in Ruby — A look at using Ruby’s Rational class to perform calculations with fractions but.. as you might’ve guessed, there are gotchas.

Ben Pickles

JavaScript Test Code Coverage in Rails Apps — Measuring Ruby test code coverage is one thing, but what about for the JavaScript contained within your Rails apps?

Ariel Juodziukynas

Building, Testing, and Deploying Google Cloud Functions with RubyGoogle Cloud Functions is Google’s answer to AWS Lambda – essentially a serverless platform running functions in the cloud without having to maintain a server.

Subomi Oluwalana

Shortcut Puts the Agile in Agile and the “Can” in Kanban

Shortcut (formerly Clubhouse.io) sponsor

▶  Discussing Encryption and Security in Ruby and Rails with Basecamp's Jorge Manrubia — Jorge Manrubia is a security developer at BaseCamp. He discusses the encryption features added in Rails and explains where they fit into the ecosystem for Hey.com. If you’d prefer to read about his work, A Story of Rails Encryption is worth checking out.

Ruby Rogues Podcast podcast

Git Bisect: Travel Through Time and Bugs — Learning git bisect can save tons of time if you don’t know when a bug was introduced to your source code. Binary search, FTW!

Remi Mercier

Being RESTful About Your Routes — A basic intro to REST concepts and how Rails applies them to routes.

STEFAN VERMAAS

An Introduction to Rails Transactions
Paweł Dąbrowski

🛠 Code & Tools

Gammo: A Pure Ruby HTML5 Compliant Parser — Gammo conforms to the WHATWG HTML specification for turning HTML into DOM trees you can work with from your code. Unlike Nokogiri it’s pure Ruby too so may be easier to move about, although in my own experiment it wasn’t particularly fast.

namusyaka

Caffeinate: A Rails Engine for Drip Campaigns/Scheduled Email Sequences — Provides a DSL to create scheduled email sequences which can be used by ActionMailer without any additional configuration.

Josh Brody

Video for Ruby: Build Video in Just Two API Calls — Mux Video is an API-first platform that makes it easy to build beautiful live video that streams everywhere.

Mux sponsor

Opal 1.3: A Ruby to JavaScript Compilerv1.3.0 adds support for more Ruby features including retry, modern exceptions features, Kernel#binding, REPL improvements, experimental support for JavaScript’s async/await, and more.

Adam Beynon and Contributors

Counter Culture 3.0: 'Turbo-Charged' Counter Caches for Rails Apps — Boasts improvements over standard Rails counter caches. (I’ve always liked the name of this project.)

Magnus von Koeller

Maxitest 4.1: Minitest, Plus All The Features You Always Wanted.. — Like what? Well, there’s a list..

Michael Grosser

Sidekiq Statistic 2.0: See Statistics About Your Sidekiq Workers
Anton Davydov

Fiddle 1.1: The Official libffi Wrapper for Ruby
Matz and the Ruby Team

💡 Tip of the Week



The -p and -n options when running Ruby

In a recent Tip of the Week, we learned about the -e and -c flags we can use when running Ruby from the comand line. Ruby has many more flags we can use as well! (ruby --help shows us the full list of flags.) Of note today, the -n and -p flags both wrap scripts in loops.

-n adds a while gets ... end loop around our script. gets reads user input and stores it in a $_. So adding a while gets loop around our script allows us to continually read in user input. Without the -n flag, if we were testing snippets of code which respond to user input, we might keep re-running them on different manual test cases. This while loop with the -n flag can be specifically useful for testing snippets of code without needing to re-run the snippets.

The syntax to use with the -n flag if using a Ruby script is ruby -n file.rb, but for a one liner you could continue to use -e as we explored last week. For example:

$ ruby -ne 'puts "You said #{$_}"'
Hello there!
You said Hello there!
something else
You said something else

(Note that the Hello there! and something else are user input, while the interleaving statements are printed output. You could also pipe in other data to be processed by this loop, such as with ls | ruby -ne 'puts "You said #{$_}"' which would then run the code for each file returned by ls.)

As always, we can exit this while loop using control-D.

Taking the -n flag one step further, the -p flag will wrap a ruby file in a while gets loop and also print the user input itself. So using the same small example as above, we can do the following:

$ ruby -pe 'puts "You said #{$_}"'
Hello there!
You said Hello there!
Hello there!
something else
You said something else
something else

We input Hello there! and something else, Ruby executes our script, and then also prints our input again. This can be more handy, perhaps, when you want to make quick transformations over incoming data and then have it output again without using something like puts yourself.

For example:

$ echo "hello world" | ruby -pe '$_.upcase!'
HELLO WORLD

Try this with the piped ls idea from above to see what happens!

This week’s tip was written by Jemma Issroff.