Lesson 21 / 25
RuboCop, Types and Performance
Keep Ruby code consistent and fast with RuboCop, optional typing and YJIT.
Quality tools for a dynamic language
RuboCop is the standard linter and formatter, enforcing the community Ruby Style Guide and catching likely bugs; configure it in .rubocop.yml, autocorrect with rubocop -a, and add plugins such as rubocop-rails and rubocop-rspec. Standard is a zero-configuration alternative built on RuboCop. Because Ruby is dynamically typed, teams add optional type checking for large codebases: RBS is Ruby's official type signature language (in .rbs files), checked by Steep, and Sorbet (from Stripe) uses inline sig annotations with fast static and run-time checks. For performance, enable YJIT (ruby --yjit or RUBY_YJIT_ENABLE=1), which speeds up real Rails applications noticeably, and profile before optimising with stackprof, rack-mini-profiler and memory_profiler. Common wins in Ruby apps are not language-level: eliminating N+1 database queries, caching, background jobs and avoiding unnecessary object allocations in hot paths.
A RuboCop config and a Sorbet signature
Consistent style plus gradual typing.
# .rubocop.yml
# require:
# - rubocop-rspec
# AllCops:
# TargetRubyVersion: 3.4
# NewCops: enable
# Style/StringLiterals:
# EnforcedStyle: double_quotes
# Metrics/MethodLength:
# Max: 15
# Sorbet: gradual static typing with inline signatures
# (the '# typed: true' sigil goes on the first line of the file)
require "sorbet-runtime"
class ShippingCalculator
extend T::Sig
sig { params(total_paise: Integer, express: T::Boolean).returns(Integer) }
def fee_paise(total_paise, express:)
return 0 if total_paise >= 100_000
express ? 9_900 : 4_900
end
end
# RBS alternative in sig/shipping_calculator.rbs:
# class ShippingCalculator
# def fee_paise: (Integer total_paise, express: bool) -> Integer
# end
# run with YJIT: RUBY_YJIT_ENABLE=1 bundle exec pumaFix the database before the language
Most slow Ruby web requests are slow because of database queries or external calls. Check query counts and response times first; YJIT and micro-optimisations come after.
Quick check: What is YJIT?
- A testing framework
- A package manager
- A just-in-time compiler built into CRuby that speeds up Ruby code
- A linter
Answer
A just-in-time compiler built into CRuby that speeds up Ruby code — YJIT compiles hot Ruby code to machine code at run time.