Short Ruby Newsletter

Share this post

๐Ÿ‘‹ Issue #11 - 19-25 September 2022

newsletter.shortruby.com

๐Ÿ‘‹ Issue #11 - 19-25 September 2022

The one with with Phlex components

Lucian Ghinda
Sep 26, 2022
6
2
Share this post

๐Ÿ‘‹ Issue #11 - 19-25 September 2022

newsletter.shortruby.com

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 ๐Ÿ˜Š.

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.


๐Ÿ‘‰ Schwadย sharedย how quick it is to prototype ideas with Rails 7:

've been doing some work on a Rails 7 Bootstrap 5 application this month and I am _loving_ life. The loop from idea-to-feature is so short. If I need something fancy I can literally just reach over and grab hotwire. If I need a component I just go to the bootstrap docs. โค๏ธ
Source: @schwad_rb onย Twitter

๐Ÿ‘‰ Brad Gesslerย sharedย a snippet of code that will help you not leak instance variables in partials in Rails:

Source: @bradgessler onย Twitter

You can check a working example on the public project Brad shared on Github.

๐Ÿ‘‰ Emmanuel Hayfordย explainedย what is the meaning of the i in the shorthand syntax for creating an array of symbol %i[]:

You use the `%i[]` syntax all the time to create an array of symbols, but what's the meaning of the 'i'? Where does it come from? The 'i' means "intern".
Source: @siaw23 onย Twitter

๐Ÿ‘‰ Thiago Massaย sharedย a code sample about doing a direct grep in tasks:

mall Ruby on Rails tip. ๐Ÿ’ก Ever ran rails --tasks | grep my_task? This isn't really needed โŒ and there's an easier way to achieve the same result โœ…
Source: @th1agofm onย Twitter

๐Ÿ‘‰ Kevin Newtonย sharedย shared a proposal for implementing NilClass in Rubyย and it seems to be faster when done this way:

Here is how it will look like:

class NilClass   #   #  call-seq:   #     nil.to_i -> 0   #   #  Always returns zero.   #   #     nil.to_i   #=> 0   #   def to_i     return 0   end    #   #  call-seq:   #     nil.to_f    -> 0.0   #   #  Always returns zero.   #   #     nil.to_f   #=> 0.0   #   def to_f     return 0.0   end end
Code source from https://github.com/ruby/ruby/pull/3366

Read more about this including the benchmark run in the Ruby master ticket.

๐Ÿ‘‰ Kirill Shevchenkoย sharedย code sample about a common useful pattern when using Service Objects to encapsulate initializer in a call on the class:

if you are using the callable Service Object pattern, you can encapsulate the "new" method with private_class_method #ruby
Source: @kirill_shevch onย Twitter

In the same thread two gems that are helping to do this were shared: serviz and operators-service

๐Ÿ‘‰ Vinicius Stockย sharedย thread with VSCode tips for Ruby:

  • โ€œCMD + SHIFT + O: fuzzy search symbols on a fileโ€ - source

  • โ€œCMD + T: fuzzy search symbols everywhereโ€ - source

  • โ€œF2 for renaming local variables, instance variables, methods or constantsโ€ - source

๐Ÿ‘‰ Martin Spickermannย sharedย about bang method a quote from Matz:

A quote from Matz - Ruby creator - saying: "The bang (!) does not mean โ€œdestructiveโ€ nor lack of it mean non destructive either. The bang sign means โ€œthe bang version is more dangerous than its non bang counterpart; handle with careโ€. Since Ruby has a lot of โ€œdestructiveโ€ methods, if bang signs follow your opinion, every Ruby program would be full of bangs, thus ugly."
Source: @spickermann onย Twitter

I tried to find the original source for this and it appears to be this forum discussion.

๐Ÿ‘‰ Emmanuel Hayfordย sharedย a code sample showing how super_methodย works:

In Ruby, there's `super`, quite common... but there's also `super_method`, that returns a `Method` of superclass which would be called when `super` is used or `nil` if there is no method on superclass.
Source: @siaw23 onย Twitter

๐Ÿ‘‰ Kirill Shevchenkoย sharedย about using retrying in Ruby by using retriable gem:

In modern application architecture, when working with external / internal API services, the use graceful retries with exponential backoff is a must. retriable is a great #ruby gem that helps with this.
Source: @kirill_shevch onย Twitter

๐Ÿ‘‰ Marco Rothย sharedย a small and very pre alpha library to transform ERB into Phlex. Source code can be found here and you can play online with it at phlexing.fun:

Source: @marcoroth_ onย Twitter

๐Ÿ‘‰ Shino Koudaย sharedย using `app` and `helper` methods in Rails console:

To give access to some functionalities that are only available in various contexts, like named route helpers and view helper methods, the Rails console provides "app" and "helper" instances.
Source: @ShinoKouda onย Twitter

๐Ÿ‘‰ Brad Gesslerย sharedย a code sample showing the nested layouts techniqueย inspired from middemanapp:

Composing Rails layouts with partials or `content_for` has always felt clunky to me, so I stole the โ€œNested Layoutsโ€ technique from https://t.co/MconqgZDBG and got it working in Rails. Hereโ€™s what it looks like
Source: @bradgessler onย Twitter
# code from https://twitter.com/bradgessler/status/1572309688699715584  module Layouthelper   # Render a block within a layout. This is a useful, and prefered way, to handle   # nesting layouts, within Sitepress.   def render_layout (layout, **kwargs, &block)     render html: capture(&block), layout: "layouts/#{layout}", **kwargs   end end <!-- This can be used in views like this:  code from https://twitter.com/bradgessler/status/1572309684421554176 -->  <!-- /apps/layouts/body.erb.html --> <!DOCTYPE html> <html>   <head> ... </head>   <body>     <%= yield %>   </body> </html> <!-- /apps/layouts/application.erb.html --> <%= render_layout "body" do %>   <header>     <h1>The International Website of Pancakes   </header>   <%= yield %>   <footer>     Brad was hungry when he created this demo app in 2022   </footer> <% end %>
Source: @bradgessler onย Twitter

Check the playground created by Brad on Github for a more detailed explanation of how this works.

๐Ÿ‘‰ Greg Navisย sharedย a validator with a proc that returns allowed values:

The inclusion validator in Rails accepts a Proc that receives the validated model and returns allowed values. Inspired by @manume's bug report against active_record_doctor. Thank you for contributing!
Source: @gregnavis onย Twitter

๐Ÿ‘‰ Eric Berryย sharedย a code sample about how to chain operations in Ruby:

Source: @coderberry on Twitter

Joel Drapper shared an alternative for chaining using conditional object yielders:

Source for de code @joeldrapper on Twitter

๐Ÿ‘‰ Moses Gathukuย sharedย a code sample about using conditionals to set classes:

Rails &gt; 6.1 tag builder uses token_list to conditionally set classes. Check this example and read more on Boring Rails
Source: @Gathukumose onย Twitter

ย ๐Ÿ‘‰ Kirill Shevchenkoย sharedย how to use JSON.parse and parse into a custom array and hash classes:

The JSON.parse method returns a Hash for any object and an Array for any json array. However, you can parse JSON into custom structures by passing them to arguments object_class and array_class.
Source: @kirill_shevch onย Twitter

When using OpenStruct please be aware that it comes with a performance penalty.

ย ๐Ÿ‘‰ Nate Berkopecย sharedย advice about the number of processes per contain and preforking webservers:

Gonna keep beating this drum but 1 process per container does not apply to preforking webservers. Fork away, baby!
Source: @nateberkopec onย Twitter

And Andrey Novikov added an explanation:

Source: @Envek on Twitter

ย ๐Ÿ‘‰ Brad Gesslerย sharedย a thread about using your component systems/libraries over for example simple_form or any building gems:

Oh and if youโ€™re wondering what the key insight is here โ€ฆusing rails building gems like simple_form to use guides.rubyonrails.org/form_helpers.hโ€ฆ results in a lot of unnecessary complexity.  When you donโ€™t try using those Rails form helpers, you get much better results.
Source: @bradgessler on Twitter

He later on shared some experiments he did with Phlex that will land later in the sitepress.cc.

Here is an example of a Phlex component that will rend a markdown:

Source: @bradgessler on Twitter

You can find the code in his view-playground on Github. Joel suggested in the thread that the result of Rouge to the syntax highlighting is cached, thus having a slight performance improvement when rendering.

ย ๐Ÿ‘‰ Mike Perhamย askedย if anyone is actively working on removing Global VM Lock/GVL (also named Global Interpreter Lock/GIL) in Ruby:

Is anyone actively working on GVL removal in Ruby, to get a true multi-core CRuby? I would be interested in sponsoring this effort.  Python is making progress...
Source: @getajobmike onย Twitter

He described the primary use case of this change as regarding Rails app that canโ€™t scale without managing multiple processes.

Here are two replies from Yukihiro Matz and Jean Boussier (please read the entire conversation for more context)

The last time we tried, removing GVL made Ruby significantly slower. If the Python community succeeds in making it faster, we will follow.
Source: @yukihiro_matz on Twitter and @by_root on Twitter
In addition, Ractor is a Ruby counterpart of Python's sub-interpreter. So in a sense, we have already removed GVL.
Source: @yukihiro_matz on Twitter

ย ๐Ÿ‘‰ CJ Avillaย sharedย a Github repo where he played with Phlex and Sinatra:

I'm OOO today and had a chance to play around with @joeldrapper's Phlex + Sinatra Fun stuff! Reminds me a bit of the DSL we used at Stripe for the old ERB docs (before we moved to Markdoc.dev)
Source: @cjav_dev onย Twitter

You might want to check this Github Repo created by Ren Guoy where the deploys some Phlex.fun components on AWS Lamdba using Serverless.

๐Ÿ‘‰ David Colby shared that among the biggest changes in Turbo 7.2.0 is the ability to respond to a GET request with Turbo Stream:

Source: @davidcolbyatx on Twitter


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


Related (but not Ruby specific)

๐Ÿค” Brandon Weaverย sharedย about growth and learningย 

A fact, often forgotten, of the tech industry: you don't _need_ to read everything, listen to podcasts, attend conferences, or any of that to be good at your job. You're allowed to just exist.
Source: @keystonelemur onย Twitter

And it got some interesting replies:

Source: @ElleArmageddon on Twitter

๐Ÿค” Nate Berkopecย askedย a question about DX and automating dev workflows:

Source: @nateberkopec onย Twitter

A summary of the answers that were written as reply to the question:

Source: Twitter replies

๐Ÿค” Shopify Engineeringย askedย about:

Source: @ShopifyEng onย Twitter

Here are some answers (including them here does not mean I agree with all of them, but I think they are interesting to think about).

Read the entire conversation, it had some interesting ideas; and I always like to challenge my beliefs/principles and think about what I miss or what I have to learn.

๐Ÿค” Simon Willisonย sharedย asked about how to explore a CSV file with 100_000 rowsย 

If someone gives you a CSV file with 100,000 rows in it, what tools do you use to start exploring and understanding that data?
Source: @simonw onย Twitter

He shared his solution to this question, but there were a lot of ideas in the conversation.

Source: @simonw on Twitter

๐Ÿค” Brandon Weaverย wrote about how to promote as a senior:

Ask early, ask often, and keep a cadence on conversations about gaps between you and that next level.  Managers won't remember every detail so write it down and keep a track sheet of evidence. It's a collaborative process, and the higher level the more you need to contribute.
Source: @keystonelemur onย Twitter

๐Ÿค” Jerome Hardawayย sharedย that engineering managementย is as simple as going to all the meetings so the team does not have to:

Engineering management is basically going to all the meetings so the rest of the team doesnโ€™t have to.
Source: @JeromeHardaway onย Twitter

๐Ÿค” Collinย sharedย an alias for resolving merge conflicts with Vim:

Here's a nifty little alias for resolving merge conflicts with Vim ๐Ÿ˜Š:
Source: @collin_jilbert onย Twitter

๐Ÿค” Thomas Steiner shared a draft PR about implementing importmaps in WebKit:

๐Ÿ‘€ Oh, a draft PR that adds import maps (https://t.co/GxMpSNQjY8) to @WebKit: https://t.co/GxMpSNQjY8. Nice!
Source: @tomayac on Twitter


Articles and Videos

Courses or Books or Communities

๐ŸŽค Paweล‚ Dฤ…browskiย sharedย the deck he used when delivering the โ€œSidekiq on the surface and under the hoodโ€ presentation at @wrocloverb this year. See it here

๐Ÿ“’ Hanami Masteryย sharedย they are opening access for Hanami Mastery PRO - a way to support their work and also get learning benefits in exchange (early access to courses and books, community access, priority support, and more). Join here.

๐ŸŽค Ruby Conf Miniย sharedย in a series of tweets on this main thread the speakers at RubyConf Mini. Read the thread to learn more about the speakers and the topics they will address.

Something to read

Newsletters

๐Ÿ“– Aurelie Verrotย shared they launched 35 edition of Women on Rails. Read the english version here. This edition has the content localized in French, Italian and ย Spanish

๐Ÿ“– Greg Molnarย sharedย they released the latest This week in Rails

๐Ÿ“– Andrew Mason shared a new number of The Ruby Radar: #69 Turbo Brisket

Articles

๐Ÿ“– Sam Rubyย sharedย a technology preview of what he calls โ€œTerraforming Railsโ€. It is worth reading the RFC for this where Sam explains what he tries to achieve and maybe also see the video demo of the current state of this tech where he deploys to fly.io a newly created Rails 7 app. I think (just a personal opinion) that this is very close to a concept that we could name ActiveDeployment.

๐Ÿ“– Colin Loretzย sharedย an interactive tutorial he found about practicing PostgreSQL skills: Enhance your Postgres skills

๐Ÿ“– Kevin Murphyย sharedย an article they wrote about organising RSpec test doubles. Read Symmetric Spies: RSpec Test Organization

๐Ÿ“– David Colby wrote an article showing infinite scroll with Rails, Turbo Stream and Stimulus by using Turbo 7.2.0. He also shared another article he wrote about Building a modal form with Turbo Stream GET requests.

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

Videos

๐ŸŽฅ Aaron Patterson announced he would do a live stream with Vinicius Stock. Subscribe here to be notified when it will start.

๐ŸŽฅ Hanami Masteryย sharedย their first episode of Hanamy Mastery PRO about working with ROM repositories. See Leverage ROM repositories

๐ŸŽฅ Wroclove.Rbย sharedย all videos from 2022 edition. See wroc_love.rb 2022

๐ŸŽฅ Adrian Marin shared his presentation at Wroclove.rb about engine automation. See How To Package A Rails Engine Generation To Automation

๐ŸŽฅ Yaroslav Shmarov (Supe Rails Yt)ย sharedย their video presentation from WrocLove.rb about using hotwire and viewcomponent in production. See 18 months of using hotwire and viewcomponent in production

๐ŸŽฅ Drifting Rubyย sharedย the video newsletter for This Week in Rails. See This Week in Rails Sept 23rd, 2022

๐ŸŽฅ The Rubber Duck Dev Show released a new episode with Andrew Atkinson. Listen Scaling All the Things! with Andrew Atkinson

Audio & Podcasts

๐ŸŽง Jason Swettย shared the latest episode of Code with Jason where he talks with Alex Evanczuk about maintaining large Rails apps. Listen 162 - Large Rails Appsย 

๐ŸŽง Ruby For Allย sharedย a new episode about Continuous Integration. Listen Episode 9 - Getting Started with Continous Integration. If you want to find reasons to listen to this podcast, you should check this discussion, where many people gave lovely feedback about this podcast.

๐ŸŽง Andrew Masonย sharedย a new episode of Remote Ruby where they discuss Storybook, Tailwind, components, JIT and Purge CSS. Listen You Gotta Risk It For The Brisket

๐ŸŽง Joel Quenneville shared a new episode for The Bike Shed where he talks with Geoff Harcourt about test infrastructure and faster CI. Listen 355: Test Performance

Gems, Libraries, and Updates

๐Ÿงฐ Koichi Itoย sharedย the latest release of Haml 6, which is faster 1.7x than Haml 5. Read changelog

๐Ÿงฐ Tim Rileyย sharedย Hanami 2.0.0.beta3, which now supports reloading inside the console. Read changelog

๐Ÿงฐ DHHย sharedย that Turbo 7.2.0 was released and it has a lot of new features and bug fixes. Check the release changelog

๐Ÿงฐ Nate Hopkinsย sharedย that he is working on two librariesย  TurboReady adding more power to Turbo Streams, and TurboReflex, adding more power to Turbo Frames.

Here are some code samples from TurboReady:

Source: TurboReady

And here are some examples from TurboReflex:

Source: TurboReflex

๐Ÿงฐ Adamย sharedย a new library that allows writing web, mobile, and desktop apps powered by Flutter by writing Ruby code. So far, the core works but supporting the UI is still WIP. See the project here.

๐Ÿงฐ Joel Drapper shared they released Phlex.fun version 0.2.2. Read the changelog.

๐Ÿงฐ Kasper Timm Hansen announced a new version of active_record-associated_object gem. It now supports code to remove Active Job boilerplate:

Source: https://github.com/kaspth/active_record-associated_object/pull/2

๐Ÿงฐ Sinatra released version 3.0.0. Read the changelog.


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

Share

2
Share this post

๐Ÿ‘‹ Issue #11 - 19-25 September 2022

newsletter.shortruby.com
Previous
Next
2 Comments
Vova Rozhkov
Sep 26, 2022ยทedited Sep 26, 2022

Wow man this is huge. I can apply right now only a few advices/approaches/gems enumerated here, but your work on collecting all this stuff is outstanding!

I think the best one I've got is "if !condition is more readable than unless", this bothered me for a long time, but now I'm gonna change my rubocop rules.

Expand full comment
Reply
1 reply by Lucian Ghinda
1 more commentโ€ฆ
TopNewCommunity

No posts

Ready for more?

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