---
title: "A Rubyist's Walk Along the C-side: Implementing an Extension in C"
publication: "Ruby Weekly"
issue: 546
published_at: "2021-04-01"
canonical_url: "https://rubyweekly.com/issues/546"
---

Plus an interview with David Heinemeier Hansson. |        

#546 — April 1, 2021 

[Read on the Web](https://rubyweekly.com/issues/546)    

😄 Hi folks! There's a time and a place for April Fools' jokes — today is certainly the right time (someone might want to [tell Volkswagen that](https://www.aljazeera.com/economy/2021/3/31/bb-thejokeis-on-volkswagen-after-april-fools-name-change-debacle)) but this isn't the place, so fingers crossed there aren't any stories that catch me out this week... :-) Also, Jemma is a back with a tip of the week at the end of this issue, so be sure to enjoy that.
__
_Peter Cooper, editor_  

![](https://res.cloudinary.com/cpress/image/upload/e_grayscale,w_150,h_170,c_pad,g_south/xrupigpv8kyjqotfdgy8.jpg)    

Ruby Weekly 

[![\](https://res.cloudinary.com/cpress/image/upload/w_1280,e_sharpen:60/yxgak25lsbenwhx0g1ct.jpg)](https://blog.peterzhu.ca/ruby-c-ext-part-1/)   

[A Rubyist's Walk Along the C-side: Hello World!](https://blog.peterzhu.ca/ruby-c-ext-part-1/) — The first in a coming eight part series that takes an accessible tour through the often thorny world of implementing a Ruby extension in C. This introductory post gives you the quickest way to creating a barebones extension. 

Peter Zhu   

[Rails 5.2.5, 6.0.3.6 and 6.1.3.1 Released](https://weblog.rubyonrails.org/2021/3/26/marcel-upgrade-releases/) — Why are these releases significant? They fix the [Marcel/MimeMagic dependency issue](https://github.com/rails/rails/issues/41750) around licenses that burned super hot last week. 

George Claghorn   [![\](https://copm.s3.amazonaws.com/b6f634b9.png)](https://railslts.com) 

[Long Term Support for Ruby on Rails 4.2, 3.2 and 2.3 - with Support for Ruby 2.7](https://railslts.com) — Rails LTS provides extended support for old versions of Ruby on Rails. Drop-in gem replacements are available for Rails 4.2, Rails 3.2 and Rails 2.3. You can even run your old Rails app with modern Ruby versions. 

Rails LTS sponsor   

[An Interview with David Heinemeier Hansson](https://www.flagsmith.com/podcast/david-heinemeier-hansson-ruby-on-rails) — While it does cover Rails’ beginnings, there’s also thoughts on open source and DHH’s campaign against the big tech monopolies and the work on Basecamp’s latest project, the [Hey.com](https://hey.com/) email system. 

Ben Rometsch   

[Rails 7 to Allow Run Queries on a Background Thread Pool](https://blog.saeloun.com/2021/03/31/rails-adds-ability-to-query-async.html) — `load_async` will let ActiveRecord schedule queries to run asynchronously on a thread pool in a reasonably transparent way. This should open up some nice performance optimizations though potentially some bugs too. 

Swaathi Kakarla     

Quick Bits 

- The always popular _Euruko_ European Ruby conference is back (online [this year](https://euruko2021.org/), due to COVID) and if you want to speak, their [call for speakers is open](https://sessionize.com/euruko-2021/) until April 18.
- [JRuby 9.2.17.0](https://www.jruby.org/2021/03/29/jruby-9-2-17-0.html) has been released. It improves Sorbet support.
- [RubyMine 2021.1 beta 4](https://blog.jetbrains.com/ruby/2021/03/rubymine-2021-1-beta-4/) is out and includes JetBrains' new _Code with Me_ collaborative development feature — the final release is now just around the corner.
- [@RubyCards](https://twitter.com/RubyCards) is a Twitter account aiming to help you learn and remember Ruby concepts by way of flashcards.  

💻 Jobs  

[Senior Backend Engineer](https://www.runtastic.com/career/job/?job=389615&no-cache=1) — As part of adidas, we face the challenge of building a top-class backend infrastructure for our fitness [apps](https://www.runtastic.com/) every day. 

Runtastic   

[Senior Software Engineer (US - Remote-Friendly)](https://jobs.lever.co/snapdocs/ac0de4a3-b7e8-4117-bbc3-3fbac1418adb) — Snapdocs is going through a hyper-growth phase, looking for empathetic Senior Software Engineers as we scale our product and team. 

Snapdocs   

[Find Ruby Jobs with Hired](https://hired.com/join/?utm_source=newsletter&utm_medium=sponsor&utm_campaign=%28b2c%29%28l-all%29%28r-all%29%28cooper-rubyweekly%29&utm_content=find-your-next-job) — Take 5 minutes to build your free profile & start getting interviews for your next job. Companies on Hired are actively hiring right now. 

Hired  

📘 Articles & Tutorials  

[Using CockroachDB with Rails](https://blog.testdouble.com/posts/2021-03-26-using-cockroachdb-with-rails/) — CockroachDB is a scalable, distributed SQL database that boasts being, if set up correctly, almost impossible to take down. Want to use it with Rails? Here’s how. 

Ali Ibrahim   

[Building a Ruby Web App with and without Rails' Libraries](https://shopify.engineering/building-web-app-ruby-rails) — It’s actually an interesting challenge to try and build a Ruby webapp with as few libraries as possible. Sorry, no Sinatra allowed here.. what about going right with the `socket` library? 😆 This is a bit of a mixed bag, but you’ll probably pick up something. 

Maple Ong (Shopify)   

[Which Is Fastest? ERB vs. HAML vs. Slim](https://www.honeybadger.io/blog/ruby-template-performance-erb-haml-slim/) — Comparing the speed of different templating approaches took me back to the late 00’s a bit, but it’s nice to still see people running the numbers. They’re all pretty close nowadays. 

Diogo Souza   

[JetBrains RubyMine: A Cross-Platform Ruby & Rails IDE](https://blog.jetbrains.com/ruby/2021/03/rubymine-2021-1-beta-1/?utm_source=cooperpress&utm_medium=cpc&utm_campaign=rubymine&utm_content=010421#rbs_support_and_better_code_completion) — The 2021.1 release is just around the corner. Learn about the new features for Ruby 3 and RBS support. 

JetBrains sponsor   

[Lazy-Loading Content with Turbo Frames and Skeleton Loader](https://boringrails.com/tips/turboframe-lazy-load-skeleton) — A solid and concise example of using Turbo Frames to dynamically load content into part of a page. 

Matt Swanson   

[Anything I Want With Sequel and Postgres](https://janko.io/anything-i-want-with-sequel-and-postgres/) — Janko demonstrates how the Sequel gem supports advanced features of Postgres, allowing them to bulk import CSV files into a partitioned set of tables that required some obscure PG knowledge. 

Janko Marohnić   

[Timecop vs Rails TimeHelpers](https://engineering.freeagent.com/2021/03/25/timecop-vs-rails-timehelpers/) — Did you know that Rails has its own TimeHelpers a la Timecop? As Matt demonstrates, you probably won’t be replacing Timecop in your Rails app in the near future (pun intended). 

Matt Bearman   

[Why Write Rails View Tests](https://pragmaticpineapple.com/why-write-rails-view-tests/) — Writing integration tests to cover the branches in your views can be time consuming so view tests provide an alternative, says the author. 

Nikola Đuza   

[How I Code Without Service Objects](https://www.codewithjason.com/code-without-service-objects/) — If you feel like service objects have grown beyond their utility, then Jason’s post will sing to you while showing you how he uses plain-old OOP to get things done. 

Jason Swett   

[Verifying JWTs with JWKS in Ruby](https://blog.unathichonco.com/verifying-jwts-with-jwks-in-ruby) — Some best practices around fetching and caching the key set and mitigating other security concerns with JWTs. 

Unathin Chonco  

🛠 Code and Tools 

[![\](https://res.cloudinary.com/cpress/image/upload/w_1280,e_sharpen:60/dxuliukwxcpttxowlkz1.jpg)](https://github.com/rails/marcel)   

[Marcel 1.0: Find the MIME Type of Files from Their Contents and/or Filename](https://github.com/rails/marcel) — If this project sounds familiar, it’s because it was at the center of the MimeMagic/GPL story last week – it now uses a Apache-licensed dataset instead and has dropped the MimeMagic dependency. It’s used by Rails but you can use it just as well from any Ruby code of your own for detecting filetypes. 

Ruby on Rails Project   

[http-clients-benchmark: Benchmarks of Well-known Ruby HTTP Clients](https://gitlab.com/honeyryderchuck/http-clients-benchmark) — The creator of HTTPX made this repository to keep his “assumptions honest” around how his gem performs against its competition. 

Honeyryderchuck   

[Complete Peace of Mind Rails Hosting](https://www2.reinteractive.com/l/820993/2021-02-17/gt5dd) 

OpsCare by reinteractive sponsor   

[SimpleDiscussion 1.3: A Simple, Extensible Rails Forum](https://github.com/excid3/simple_discussion) — We first linked this a year ago but it’s just had its first update in a while. I like the feel of [the forums](https://gorails.com/forum) it creates. 

Chris Oliver   

[pwned: A Ruby Wrapper for the Pwned Passwords API](https://github.com/philnash/pwned) — Check if a password has been found in any of the huge data breaches. 

Phil Nash   

[devise-two-factor: Barebones Two-Factor (TOTP) Authentication with Devise](https://github.com/tinfoil/devise-two-factor) 

Tinfoil Security   

[Torch.rb 0.6.0: Deep Learning for Ruby](https://github.com/ankane/torch.rb) — A way to use [Torch](https://pytorch.org/), an open source machine learning framework (more commonly associated with Python.) 

Andrew Kane      

💡**Tip of the Week** 

Effective use of Array#bsearch 

Binary search is a commonly taught algorithm for finding an element of a sorted array in `O(log(n))` time where `n` is the size of the array. In the context of an array, it takes a sorted array, makes a comparison to a middle element, and then based on that comparison recurses in either the right or left 'half' of the array until it either finds an element where a comparison returns true, or has no more elements to look over. 

Ruby has an `Array#bsearch` method to return an element selected by a binary search. Its functionality can be a little tricky to grasp at first. Take a look at this example:  

["tip", "of", "the", "week"].sort.bsearch { _1 == "of" }
 => nil   

Initially, this result may be counterintuitive. Let's dig into why this happens, so we can effectively use `Array#bsearch` going forward! 

There are assumptions `Array#bsearch` makes about the array it's searching, and the condition on which it's searching: 

- The array is sorted.
- There are two types of comparisons we can make with the condition against elements of the array: 

It doesn't check these assumptions, however, and can't guarantee results when these assumptions aren't met. So if we look again at our example from above, we see it meets the first assumption: the array is sorted. But on the second assumption, it has boolean results which do not all evaluate to `false` before `true`. Let's take a closer look:  

sorted_array = ["tip", "of", "the", "week"].sort
 => ["of", "the", "tip", "week"]
 
 # This violates the second assumption because `false`
 # evaluations don't always precede `true` evaluations
 sorted_array.map { _1 == "of" }
 => [true, false, false, false]
 
 # There are a few options for how we could fix this:
 
 # If we reverse the array, it'll work
 sorted_array.reverse.map { _1 == "of" }
 => [false, false, false, true]
 
 # Tada!
 sorted_array.reverse.bsearch { _1 == "of" }
 => "of"
 
 # Or we can use the <=> operator which returns 1,0,-1
 # Note we need "of" <=> _1 to hold the ordering property we seek
 sorted_array.map { "of" <=> _1 }
 => [0, -1, -1, -1]
 
 # Tada!
 sorted_array.bsearch { "of" <=> _1 }
 => "of"   

And we can see that as long as both of the assumptions hold, we can rely on `Array#bsearch` to efficiently and effectively search an array. 

An added bonus, if we're just looking for the index of an element selected by binary search, we can use `Array#bsearch_index` with the same block as above:  

sorted_array.bsearch_index { "of" <=> _1 }
 => 0   

This week’s tip was written by [Jemma Issroff](https://twitter.com/jemmaissroff).          

Curated by [Peter Cooper](https://twitter.com/peterc), [Glenn Goodrich](https://twitter.com/ruprict) and [Cooperpress](https://cooperpress.com/). 

[Change your email address](https://rubyweekly.com/edit_subscription/*|UID|*) or[Stop getting this newsletter](https://rubyweekly.com/leave/*|UID|*) 

© Cooper Press Ltd · Fairfield Enterprise Centre, Louth, LN11 0LS, United Kingdom
