A little bit about numbers and
caching in Ruby
Kathryn Blackley, @keblackley
24 July 2013
CodeFellows, Seattle, WA
Scenario: you want to put a number into your Rails app.
Do you want…
A float? 6.2321, 113592.2, 0.0000012
An integer? 4, -18, 343338
A rational? (2/3), (-1/4), (29/8)
What’s the difference behind the scenes?
Written in binary (base 2)
0 = 0
2 = 10
1 = 1
3 = 11
4 = 100
Each switch is a called bit.
Ruby integers: fixnum and bignum
Fixnum – limited by length of word in your operating
system: 32 bit or 64 bit.
Bignum – an object is allocated so there’s one bit per
digit required (in binary).
Ruby automatically transitions
However your computer does numbers:
“double-precision floating-point format”
Uses powers of 2 and a significand to represent
Problems with limited precision possible.
Q: Where’s this all happening in your computer?
A: In the CPU!
Q: So, where’s that?
CPU: central processing unit
RAM: random access memory
Back to Ruby and Rails:
Caching in your rails app is dependent on the environment:
Default is OFF in development and testing
Default is ON in production
Can modify in config/environments
Why cache in your app?
Optimize your app scalability
Good to know looking ahead to when your app is extremely popular!