#542 — March 4, 2021

Read on the Web

💡 We've had some fantastic Tip of the Week feedback, so we're carrying on :-) If you have any suggestions of things Jemma could cover, you can tweet her @jemmaissroff — she's also keeping a Twitter thread of quick Ruby tips if you want even more.
__
Peter Cooper, editor

Ruby Weekly

Upgrow: A Sustainable Architecture for Rails Apps? — Shopify draws on its experience as a Rails shop to create this guide with patterns (and anti-patterns) for creating large, long-lived Rails apps. Do you agree with their choices?

Shopify

Standard 1.0: A Ruby Style Guide, Linter and Formatter — As StandardJS is to JavaScript, Standard is to Ruby. It’s been around a while, but 1.0 is always a fun milestone, and I keep hearing about more people and teams using this to catch issues and have a standard formatting for codebases.

test double

Are You Listening to FounderQuest Yet? — It’s a weekly podcast about three Ruby developers bootstrapping a software business on their own terms...and murderbots, ninjas, garlic scapes, and (checks notes) something called Big Mouth Billy Bass. Don’t miss out.

FounderQuest sponsor

Five Ways to Make HTTP Requests in Ruby — A comparison of several approaches including net/http, httparty, HTTP (the gem), HTTPX, and Faraday. I’m going to admit right now the http gem is my go-to.

Valeriane Vernance

Active Record Queries in Views: When It's Bad, When It's Fine — Keeping concerns separate where you can is generally a best practice, but Jason doesn’t think it’s a cast iron rule. (Many JavaScript developers are mixing CSS in with JS nowadays, so it’s a free for all as far as I’m concerned… ;-))

Jason Swett

Quick Bits

💻 Jobs

Senior Software Engineering Consultant - [100% Remote]  — Co-founded by Justin Searls, Test Double is an engineering consultancy on a mission to improve the way the world builds software. Work on challenging projects with a collaborative, passionate team. 100% employee owned, and awesome perks.

TestDouble

Backend Engineer (SF or Vancouver BC) — Chime is the largest and fastest-growing U.S. player in the challenger-banking space. We’re hiring backend engineers. Join us 😎

Chime

Find Your Next Job 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

▶  Some Rails Best Practices in 20 Minutes — A well recorded screencast that covers a variety of ideas.

Jack Kinsella

Building Static Ruby Gems Containers using Docker & Amazon Linux — With the ability to deploy containers to Lambda, is there a way to create precompiled gems for some dependencies to ease and speed up deployment? (Spoiler: Yes)

Ken Collins

Testing Multiple Sessions in the Same Test with Capybara — You can simulate switching between users using Capybara’s using_session helper.

Matt Swanson

Build a Ruby App with CockroachDB & ActiveRecord — This tutorial shows how to build a Ruby app with CockroachDB & ActiveRecord. CRDB provides an ActiveRecord adapter as a RubyGem.

CockroachDB sponsor

The Ruby Unbundled Series: Creating Games with GosuGosu wraps OpenGL capabilities which makes it easy to make simple games like those presented here.

Darren Broemmer

Testing ActiveRecord Concerns — It’s a good idea to test your concerns outside of the models that use them, but there are some, um, concerns about how to do that.

Alex Barret

Build a Text-to-Speech Application with Hanami (and Vonage) — Bear in mind this uses the commercial Vonage Voice API (formerly Nexmo).

Ben Greenberg (Vonage)

Rails 6.1 Adds ActiveRecord::FinderMethods 'sole' and 'find_sole_by' — For when you truly want one and only one result.

Akanksha Jain

▶  Sharing Cookies with Subdomains in Rails — You might do this if you want to authenticate a user across all subdomains in your Rails app.

Go Rails

🛠 Code and Tools

Strings::Truncation: Truncate Strings with Fullwidth Characters and ANSI Codes — If you do a lot of truncating strings, a gem like this should be in your toolbelt.

Piotr Murach

WebMock: Stub and Set Expectations on HTTP Requests for Testing — Supports lots of HTTP libraries like Net::HTTP, Excon, the http gem, and Curb::Easy.

Bartosz Blimke

Founders/CTOs: Onboarding New Developers Taking Forever? We Can Help

Hint sponsor

Ahoy 3.2.0: Simple Analytics for Rails Apps — Track visits and events in your app. By default, data is stored in your normal database.

Andrew Kane

Invisible Captcha 2.0: Unobtrusive Spam Protection for Rails Forms — Your mileage may vary as ‘honeypot’ approaches can work well at first but are easily overcome by a determined abuser.

Marc Anguera Insa

gruf: A gRPC Ruby Framework — A wrapper around the Ruby gRPC library that supports gRPC 1.10.x and above.

BigCommerce

💡 Tip of the Week

Kernel#caller_locations gives us a helpful view of the execution stack

Have you ever needed to know the execution stack for a call? Perhaps you’re looking to provide a useful logging message with the entire stacktrace? Or you want to know where a specific method was called? Or you’re writing a gem and need to know where a method is being used?

In any of these cases, you’re in luck because from Ruby 2.0 onwards we have access to Kernel#caller_locations which gives us an array of Thread::Backtrace::Location objects which represent the current execution stack.

Here’s a simple little example with three methods, where the third method calls the second method which calls the first method which in turn tells us the caller_locations:

def method_one
  caller_locations
end

def method_two
  method_one
end

def method_three
  method_two
end

pp method_three

If we run this snippet (saved in a file called example.rb), we’ll get:

$ ruby example.rb
["example.rb:6:in `method_two'",
 "example.rb:10:in `method_three'",
 "example.rb:13:in `<main>'"]

We can see our stacktrace! Kernel#caller_locations can take two optional parameters which both work to limit the size of the array of Thread::Backtrace::Location objects we receive. The first (start) parameter tells how many stack entries to omit from the top of the stack. The second (length) parameter tells how many objects to include in the returned Array.

A common use of caller_locations is getting the first element to see the calling method. This can be done by passing in the arguments (1,1). If we modify our code slightly to use caller_locations(1,1), we’ll get:

$ ruby example.rb
["example.rb:6:in `method_two'"]

And we can see that method_two is calling method_one. Lastly, there are a few useful methods on Thread::Backtrace::Location objects. The most notable ones are #lineno, #label and #path which give us the line number, method name and file path respectively.

This week’s tip was written by Jemma Issroff.