Today I Learned

hashrocket A Hashrocket project

RSpec's be_within Matcher

Today I learned RSpec has a be_within matcher.

It does what it says - it verifies if the actual is within a delta (<=) of the expected.

expect(3.0).to be_within(0.5).of(3.0) # ✅
expect(3.2).to be_within(0.5).of(3.0) # ✅
expect(3.5).to be_within(0.5).of(3.0) # ✅
expect(4.3).to be_within(0.5).of(3.0) # ❌ 
See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.