Class: VoteFu::Algorithms::RedditHot

Inherits:
Object
  • Object
show all
Defined in:
lib/vote_fu/algorithms/reddit_hot.rb

Overview

Reddit's "Hot" ranking algorithm

This algorithm balances popularity (vote score) with recency. Items with high scores rise quickly, but decay over time, allowing fresh content to surface.

The formula uses logarithmic scaling for votes, so the first 10 votes have the same impact as the next 100, then 1000, etc. This prevents runaway popular items from dominating forever.

Examples:

Post.all.sort_by(&:hot_score).reverse

See Also:

Constant Summary collapse

EPOCH =

Reddit's epoch (December 8, 2005)

Time.utc(2005, 12, 8, 7, 46, 43).to_i

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(voteable) ⇒ RedditHot

Returns a new instance of RedditHot.



33
34
35
# File 'lib/vote_fu/algorithms/reddit_hot.rb', line 33

def initialize(voteable)
  @voteable = voteable
end

Class Method Details

.call(voteable, gravity: 1.8) ⇒ Float

Calculate the hot score

Parameters:

  • The voteable object

  • (defaults to: 1.8)

    Not used in Reddit's algorithm but kept for API consistency

Returns:

  • The hot score (higher = hotter)



29
30
31
# File 'lib/vote_fu/algorithms/reddit_hot.rb', line 29

def self.call(voteable, gravity: 1.8)
  new(voteable).calculate
end

Instance Method Details

#calculateObject



37
38
39
40
41
42
43
44
45
# File 'lib/vote_fu/algorithms/reddit_hot.rb', line 37

def calculate
  score = @voteable.plusminus
  order = Math.log10([score.abs, 1].max)
  sign = score <=> 0
  seconds = epoch_seconds

  # The score decays over time (45000 seconds ≈ 12.5 hours)
  (sign * order + seconds / 45_000.0).round(7)
end