Short Ruby Newsletter

Share this post

๐Ÿ”– Issue #10 - 12-18 September 2022

newsletter.shortruby.com

๐Ÿ”– Issue #10 - 12-18 September 2022

The one about RSpec

Lucian Ghinda
Sep 19, 2022
3
Share

Hello,

Iโ€™m @lucianghinda, the curator of this newsletter. If you want a short excerpt of this newsletter containing only some of the images with code, I created one here. But I invite you to read the entire newsletter as it has excellent content ๐Ÿ˜Š.

If you like it and want to help me reach a wider audience, I would kindly ask you to leave some feedback/short written testimonial that I can display on the About page.

Leave a short testimonial

This edition was created with support from @adrianthedev from Avo for Ruby on Rails (a friendly full-featured Rails admin panel) and from @jcsrb, who sent me recommendations to include in the newsletter.

If you have any feedback or ideas about this newsletter, please reach out on Twitter or via email at hello@shortruby.com


๐Ÿ‘‰ Thiago Massaย sharedย about how to do a proper Ruby memoization in Ruby

Memoization in Ruby can be very tricky. ๐Ÿค”  Yesterday I've shared the wrong โŒ example with the Ruby idiom for memoization.  @mhenrixon  pointed out that if the response is nil, it won't memoize and subsequent calls will make the HTTP request again.  Did you know that?๐Ÿ‘‡
Source: @th1agofm onย Twitter

๐Ÿ‘‰ Shino Kouda shared an example of how to use enum in Rails with integer type and this generated a good discussion about possible issues generated by this approach and how to overpass them:

Source: @ShinoKouda on Twitter

๐Ÿ‘‰ Josh Cheek shared that when using Postgres, one can define a Postgres Enum type. Here is the gist he created that works by adding the enum type with raw SQL. If you want to do this with Rails 7 it added support for this enum type:

Source: @shortrubynews

If you want to add a new enum, you will need to write raw SQL with ALTER TYPE. See Rails 7 guides for enumerated types

About using integers for enums, here is a good discussion explaining the pros and cons:

Agreed. + enums as integers is not a good way to go. Once the system might change requirements to delete existing status or split it into two. Data migration is hard in this case. Also, it's hard for analysing data directly in DB as you need to keep in mind this match. Enums as integers in the database is a trade off. It can save a lot of memory and give you better performance in large tables. But itโ€™s usually not necessary. LiteralEnums can be integers or strings. But theyโ€™re strings by default.
Source: @ka8725 and @joeldrapper on Twitter

Joel Drapper then proposed using a gem he wrote:

Example of usage for gem literal_enums-rails
Source: @joeldrapper on Twitter
Source: @joeldrapper on Twitter

๐Ÿ‘‰ Kirill Shevchenkoย sharedย  an example of encoding and decoding JSON with gRPC:

Source: @kirill_shevch onย Twitter

๐Ÿ‘‰ Stefan Vermaasย sharedย how to display flash messages with Turbo in Rails:

Played around today with some ideas to display flash messages automatically for Turbo requests in Rails. I believe this it's a decent way to show flash messages as it requires minimal effort to use them. Just use `set_flash_message` and you're good to go.
Source: @stefanvermaas onย Twitter

๐Ÿ‘‰ Nate Berkopecย sharedย how to profile an entire Rails boot sequence:

Is there actually a good way to profile the _entirety_ of a Rails boot sequence? I threw my profiler around the Rackup file (config-ru) today and it only caught like 25% of actual wall clock time I was seeing w/rails runner.
Source: @nateberkopec onย Twitter

There is a good discussion there about various ways to achieve that. While replying to the thread, Jean Boussier did a PR to stackprof gem to make this happen quickly with the following command:

๐Ÿ‘‰ Shino Koudaย sharedย code sample about parsing a JSON and instantiating objects using a feature of JSON.parse:

Nice way to parse JSON in Ruby, using a poorly documented object_class option (found on SO). Far easier to dig then.
Source: @ShinoKouda onย Twitter

Just note that using OpenStruct will affect your performance. Here is what the official Ruby documentation says about OpenStruct:

๐Ÿ‘‰ Tim Rileyย sharedย a thread about making Hanami faster:

As we near the @hanamirb 2.0 release, weโ€™ve been running some benchmarks to make sure the house is in order. We learnt that hanami-controller wasnโ€™t as memory efficient as weโ€™d hoped! A big part of it seemed to be with dry-configurable.
Source: @timriley onย Twitter

๐Ÿ‘‰ Kirill Shevchenkoย sharedย a code sample to show how to open IRB console within the current scope:

IRB supports binding.irb feature, which allows you to open the IRB console within the current scope. This can be useful when you want to quickly play around with some snippets without even installing debug gems. #ruby
Source: @kirill_shevch onย Twitter

๐Ÿ‘‰ Benito Sernaย sharedย a sample code about polymorphic rendering with ActiveModel and ActionView:

You can build powerful polymorphic rendering with ActiveModel and ActionView. #ruby #rails #rubyonrails In this example we have an inputs array with different kind of input objects, but as all of them are ActiveModels all of them now to how to be rendered.
Source: @bhserna onย Twitter
Source: @bhserna onย Twitter

๐Ÿ‘‰ Justin Searlsย sharedย his opinion about why he recommends people to avoid RSpec.

Top of mind things I think are problematic about Spec (literally just typing as I think of stuff): 1. Its implementation is inscrutably complex, so if you have an issue, inspecting the source is not productive solution 2. Its DSL defeats most attempts at automated introspection (Ctags, lang server, etc.) which means navigating the code is very difficult compared to tests of classes/methods 3. The DSL also allows for combinatorial explosions in runtime performance cost via incredibly terse and tidy APIs, most notably via shared_example_groups 4. The DSL provides numerous ways to structure tests and everyone has a different opinion (subject is great, subject is evil, let is good/bad, nesting is good/bad), leading to terrific inconsistency on projects as individuals express their own favorite way on the specs they write unless you dictate a style in a really authoritarian way 5. The rspec-rails tie-ins are often years late to be updated to reflect changes in Rails itself, and when a failure occurs in that gem's indirection, it is very very difficult to workaround as a user. It adds another point of failure for little marginal value 6. Generating specs by looping over data structures is incredibly easy and convenient and often leads to an entire category of bugs (often pointed out by Jim Weirich in his talks) where you might oop over a data structure to generate 100 specs and unwittingly realize that the final test case was actually generated and run 100 times 7. The above is caused by the fact that It's two-pass system: first the specs are loaded and a tree of example groups is constructed dynamically, then a second pass in which the tests are executed. Because it's a DSL and not class/method, it's very easy for developers to fail to grok that there are two runtime contexts and they will often be confused when describe or it or expect behaves differently based on which method the current block is being passed to 8. People who advocate RSpec for the real Dan North BDD purpose it was created will very often tout its utility as a feature behavior elucidation tool, but in practice 99.9% of specs are just symmetric unit and integration tests of code. This creates a lot of tension, as the thing that RSpec is advertised as ("decouple your specs from the implementation class, describe the feature's outcomes instead!") often exacerbates the problem of unclear indirection (when I want to update class X, which spec do I need to drive that change through?). It's just another opportunity for ideological conflict to result in inconsistency and confusion in the test suite   I have a lot more but didn't want to spend more than 10 minutes dunking on Spec today
Source: @searls onย Twitter

This generated a lot of responses. You should read the entire thread and also click the quoted tweets. Here is just a summary, but please read those two links to make up your own conclusion:

๐Ÿ‘‰ Kirill Shevchenkoย sharedย a code sample showing curry for procs

Ruby has a curry method for procs. This allows partially applied procs to be returned when they are called with fewer arguments than required. #ruby
Source: @kirill_shevch onย Twitter

๐Ÿ‘‰ Thiago Massaย sharedย a code sample with .() shorthand syntax for .call:

Source: @th1agofm onย Twitter

Joel Drapper also added the double dot notation works, and it does the same:

Source: @joeldrapper on Twitter

๐Ÿ‘‰ Nate Hopkinsย sharedย a script to work with libraries that are incompatible with Turbo Drive as they assume that the document head is loaded and parsed on every page visit:

Here's a hack/fix that you can use to force such libs to work with Turbo Drive. It's inefficient from a compute perspective but allows you to use Turbo Drive with incompatible libs. Also for anyone unaware, Turbo Drive provides UX similar to SPAs with minimal developer effort.
Source: @hopsoft onย Twitter

๐Ÿ‘‰ Dave Paola shared that they launched a new project - a development agency with the mission to be the place where Junior Ruby on Rails developers start their careers:

Source: @dpaola2 on Twitter

๐Ÿ‘‰ Meghan Gutshall asked a question about how to explain a Ruby class:

Source: @meg_gutshall on Twitter

Here are some replies, but you can find more in the thread:


If you read so far and you like the content, maybe you take into consideration sharing this and subscribe:


Related (but not Ruby specific)

Celso de Sรกย asked about what it means to be a mid-level Ruby developer. Here are some of the answers to this question:

My definition of a mid-level developer is (short version): An engineer who can be trusted to work on a project without much guidance. And who generally delivers good results in expected time frames | (source: @spickerman)  Difference to a senior developer: Senior need to be able to plan and lead complex projects. Additionally, they need to be able to manage stakeholders, do hiring interviews, and mentor and train other developers | (source: @spickerman)  Ultimately, it doesn't matter. We need to understand what the customer wants, 1st. & understand how we ensure everything is customer driven. That should be a junior requirement imo The fullstack being customer <-> code :) Then we need to learn to collaborate with other devs | (source: @alterisian)

I think you should also take a look at โ€œgeekery skillsetโ€ proposed by GeePaw Hill:

I propose that there are four (rough and approximate) evolutions in geekery skillset:  1) Thinking in syntax. 2) Thinking in code. 3) Thinking in <paradigm>. 4) Thinking in change.
Source: @GeePaw on Twitter

Joe Masilotti ๐Ÿ“—ย sharedย a nice trick to skip running Github Actions:

TIL that you can skip running your GitHub Actions by adding [skip ci] to a commit message. [skipci] (no space), however, does NOT skip CI.
Source: @joemasilotti onย Twitter

Read the documentation for this here.

Tobias Petry.Sqlย sharedย a database tip about making the index invisible:

Database Tip Deleting an unused index is still a risky operation: If you are wrong (and it is still needed) the database will become slow. Before deleting it, you can make the index invisible first and make it visible again instantly if you still need it.
Source: @tobias_petry onย Twitter

Brandon Weaverย sharedย about seniority and self-marketing:

I'll need to write this one up later, but had an interesting conversation and idea today around self-marketing.  At very senior levels it's absolutely mandatory to do your job well, but what does that look like?  Well it turns out that part is far more complicated.
Source: @keystonelemur onย Twitter
Source: @keystonelemur on Twitter

You can read the full thread here or the article Brandon wrote expanding this on this blog: Beyond Senior โ€“ The Peacock, the Hermit, and the Counsel


Articles and Videos

Courses or Books or Communities

Brandon Weaverย sharedย launched a new community focused on learning Ruby. The discord invitation is this one, and the main website is rubylearning.dev. This week we already started working as a group on the book Rebuilding Rails by Noah Gibbs.

Kasper Timm Hansenย sharedย an update about his course called Product Focused Ruby.

Something to read

Soutaro Matsumotoย sharedย their presentation slidesย from RubyKaigi about how to use Ruby types. See the slides here.

Newsletters

Greg Molnarย sharedย a new issue of This Week in Rails written by Petrik de Heus

Andrew Mason shared a new issue of The Ruby Radar. Read 68: Hack the Planet!

David Colby shared a new issue of Hotwiring Rails. Read Hotwiring Rails - September 2022

Awesome Ruby Newsletter released their 330 newsletter. Read 330 Awesome Ruby Newsletter

RubyWeekly published its 620 newsletter. Read Ruby Weekly #620

Articles

Eric Berryย sharedย that they wrote about how to use fswatch to build a Rails watcher/runner

Joe Masilotti ๐Ÿ“—ย sharedย a list of 10 of his favorite online communities for Ruby and Rails communities.

Joรซl Quennevilleย sharedย about Working Iteratively

Something to watch ๐ŸŽฅ or listen ๐ŸŽง

Videos

๐ŸŽฅ Drifting Rubyย sharedย a new episode of the video newsletter for This week in rails. Watch โ†’ This Week in Rails Sept 17th, 2022

๐ŸŽฅ Joel Drapper shared a session where he started converting a large ERB to Phlex and then discovered and fixed a bug. Watch โ†’ Just trying out

๐ŸŽฅ The Rubber Duck Dev Show shared a new episode with Brittany Martin and Josh Gaastra about Engineering Managers and Product Managers. Watch โ†’ EM vs PM! with Brittany and Josh

๐ŸŽฅ Avo shared a video doing a walkthrough of the new features they released. Watch โ†’ Avo Admin 2.15 for Ruby on Rails

Audio & Podcasts

๐ŸŽง Jason Sweet launched a new episode of the Code with Jason podcast where he invited Adrian Marin to talk about Avo and how it is to make tools for developers. Listen โ†’ 161 - Adrian Marin, Founder of Avo Admin

๐ŸŽง Joรซl Quennevilleย sharedย a new episode of The Bike Shed with Sara Jackson where they talk about The History of Computing. Listen โ†’ 354: The History of Computing

๐ŸŽง Ruby For Allย sharedย a new episode about Bridgetown. Listen โ†’ Episode 8: What is Bridgetown. If you like this you should consider joining the Jared White Discord community The Spicy Web

๐ŸŽง Remote Rubyย sharedย a new episode where they invited Joel Drapper to talk about Phlex: Listen โ†’ Phlexing with Joel Drapper

Gems, Libraries, and Updates

๐Ÿงฐ Avo shared they released a new version with a lot of features. See the changelog

๐Ÿงฐ Postmodernย sharedย they added ruby 3.2.0 preview 2 to ruby install. See the changelog here. Thus now, the following command should work:

Added ruby-3.2.0-preview2 to ruby-versions, so ruby-install should now be able to safely download and install it. #ruby      $ ruby-install --latest ruby-3.2.0-preview2
Source: @postmodern_mode3 on Twitter

๐Ÿงฐ JRuby Dev Teamย sharedย they released version 9.3.8.0 which includes โ€œa CVE fix for the Psych YAML library, support for lightweight fibers using Java 19 virtual threads, and a bunch of compatibility and bug fixesโ€. See changelog

๐Ÿงฐ Marco Rothย sharedย that Turbo 7.2 rc1 was released. See the changelog

๐Ÿงฐ Postmodernย sharedย a gem named sord that will transform YARDoc into o Sorbet definitions.

๐Ÿงฐ Mike Perhamย sharedย they released Sidekiq Pro 5.5.4 โ€œfixes a batch race condition which could cause the batch pending count to read -1โ€. See changelog

๐Ÿงฐ Rubygems Newsย shared an update for a library called Spy which is a new way of mocking:

Source: https://github.com/ryanong/spy

๐Ÿงฐ Nate Berkopecย sharedย a new library fixture_factory that helps to work with fixtures like factories. Here is a code sample:

Source: https://github.com/Shopify/fixture_factory

๐Ÿงฐ Andrew Mason shared a new library they discovered Capybara Test Helpers. Here is what it looks like:

Source: https://github.com/ElMassimo/capybara-compose

Please consider sharing this on social media or with your colleagues:

Share

If you have any feedback or ideas about this newsletter, please reach out on Twitter or via email at hello@shortruby.com

3
Share
Previous
Next
Comments
Top
New
Community

No posts

Ready for more?

ยฉ 2023 Lucian Ghinda
Privacy โˆ™ Terms โˆ™ Collection notice
Start WritingGet the app
Substackย is the home for great writing