tags: Ruby

Matchers That Can Take Other Matchers

In RSpec, some matchers can take another matcher as an argument. It is sometimes useful for writing an expectation for a collection. Let's say we want to test that an array contains a string that includes a particular text. We can do that by combining two matchers: include and match as follows. expect(an_array).to include(match(/Lorem/)) # Of course we also could do: # expect(an_array.grep(/Lorem/)).not_to be_empty # or # expect(an_array).to be_any {|item| /Lorem/ === item } I think that in this case, the expectation with include and match is much more concise than the commented-out alternative ways above.

Read more →

Implement and Test in One File with bundler/inline and rspec/autorun

Sometimes I want to create a tiny one-off script that intended to run on a sandbox environment like a Docker container and needs only one file. Most of the time, I also need test frameworks to do some refactoring. It can be done with bundler/inline and rspec/autorun. require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'rspec' end def doit 'return a string' end require 'rspec/autorun' RSpec.describe '#doit' do example do expect(doit).to be_a(String) end end Usually, Bundler reads the dependencies from Gemfile.

Read more →

RSpec's #to Method Takes the 2nd Argument As Its failing-message

Today I learned that RSpec's #to method takes the 2nd argument as its custom failing-message. expect(actual_value).to eq(expected_value), message Let's say we are going to test a JSON object. The following example expects a part of the JSON object response_json['eye_colour'] to be "blue". require 'net/https' require 'uri' require 'json' RSpec.describe do example do response = Net::HTTP.get_response(URI('https://swapi.dev/api/people/1/')) response_json = JSON.parse(response.body) expect(response_json['eye_colour']).to eq('blue') end end Unfortunately, this test fails and displays messages like this:

Read more →

`sole` Method Is Going to Be Introduced into ActiveRecord and Enumerable

Today I learned that Rails 6.1.3 does not have methods that raise exceptions when more than one record was found. However, in future versions, we could use ActiveRecord::FinderMethods#sole for that purpose. It seems Enumerable#sole is also going to be introduced. Sometimes I need to make sure there is one and only one record that matches a condition without using a unique-index, for example, when I cannot add that constraint to the database.

Read more →

Format with to_s Methods ActiveSupport Provides

Today I learned/remembered that to format a number with a delimiter, we can use ActiveSupport::NumericWithFormat#to_s(:delimited). require 'active_support' require 'active_support/core_ext' 123456789.to_s(:delimited) # => "123,456,789" Not only that, but this method also provides other formats and takes options to tweak its behavior. 123456789.to_s(:delimited, delimiter: '-') # => "123-456-789" 123456789.to_s(:currency, precision: 3) # => "$123,456,789.000" 123456789.to_s(:human_size) # => "118 MB" 123456789.to_s(:human) # => "123 Million" Originally, when I need to format a number to a delimited number, I would try to use number_to_delimited provided by ActiveSupport::NumberHelper.

Read more →

Creating and Uploading Huge Archive Files Swimmingly with Ruby

When we have to process a large number of files, we should be aware of the resources the process consumes. Here's the scenario I faced in my work recently: we have to create one archive file from lots of files that are stored on an S3 bucket. Created archive files must be placed on another S3 bucket. The number of files of an archive could be 700,000. The size of each file is up to 10MiB.

Read more →

A Quick Way to Try Stripe Subscriptions With Docker

Stripe provides an easy way to implement subscription services. This document explains how Stripe's subscription works and the things you can. They also provide an example to implement fixed-price subscriptions, like Netflix. You can try the example repository on GitHub and see the whole lifecycle of a subscription. Actually, you don't have to even set up any environment for the example app if you have Docker on your computer. The only dependency is Docker.

Read more →

Using Docker for RubyGems projects

Sometimes I want to create a new gem or modify existing gems to extend/fix them. However, I do not want to install tons of additional development tools on my host machine for that. Recently, I created a shell script which creates an instant development environment on Docker for that. #!/bin/sh -e exec docker run --rm -it \ --workdir=/work \ -v $(pwd):/work \ -v=$HOME/.gitconfig:/root/.gitconfig \ -v=$HOME/.ssh:/root/.ssh \ -v=$HOME/.

Read more →

Creating an Active Job Adapter

Active Job adapter interface I've created an Active Job adapter to run my background jobs on Google Cloud Run via Google Cloud Tasks. Although it is the first time for me to create an adapter for Active Job, this is working very well so far. https://github.com/esminc/activejob-google_cloud_tasks-http Creating an Active Job adapter itself is not so hard. You can see that by exploring the repository above. In fact, Active Job adapters are required to implement only two methods: enqueue and enqueue_at.

Read more →

Sending FCM Push Notifications with Andpush Gem

Recently, I had migrated from GCM (Google Cloud Messaging) to FCM (Firebase Cloud Messaging) because GCM was ending its life. At first, I found fcm gem which supports FCM. I changed my application code to use the gem and I deployed the new version since it was working nicely on the staging. However, as time goes, I realized that FCM notification sometimes stops working when the application runs on multiple threads.

Read more →

Testing Rails Application via Browsers with System Tests

I'm developing a small application which is built on Ruby on Rails, React and Relay. Since the project is based on my personal hobby and just a prototype, I preferred to write application codes rather than to write tests at first. However, as the application grows larger, I had to test the application manually every time when I make changes to make sure it doesn't break other features. The lack of tests made development speed slower.

Read more →

Gentaro "hibariya" Terada

Otaka-no-mori, Chiba, Japan
Email me

Likes Ruby, Internet, and Programming.