Class: Rubyflake
- Inherits:
-
Object
- Object
- Rubyflake
- Defined in:
- lib/rubyflake.rb
Constant Summary collapse
- EPOCH =
01/01/2011
1293858000- FLAKE_TIMESTAMP_LENGTH =
41- FLAKE_RANDOM_LENGTH =
0b1111111111111111111111111- FLAKE_TIMESTAMP_SHIFT =
23
Class Method Summary collapse
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(epoch = EPOCH) ⇒ Rubyflake
constructor
A new instance of Rubyflake.
Constructor Details
Class Method Details
Instance Method Details
#generate ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rubyflake.rb', line 14 def generate # Figure out how many milliseconds have occurred since epoch. milliseconds = ((Time.now().to_f - @epoch) * 1000).to_i # Generate 23 random bits. random_bits = Random.new.rand(0..Rubyflake::FLAKE_RANDOM_LENGTH) # Shift our timestamp over 23 bits to make room for the random bits, # and then add the two together. (milliseconds << Rubyflake::FLAKE_TIMESTAMP_SHIFT) + random_bits.to_i end |