Class: VoteFu::Algorithms::HackerNews

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

Overview

Hacker News ranking algorithm

This algorithm heavily penalizes older content. Items decay rapidly based on age, making it suitable for fast-moving content feeds where freshness is paramount.

Formula: Score = (P - 1) / (T + 2)^G Where:

P = points (plusminus score)
T = age in hours
G = gravity (default 1.8)

Examples:

Post.all.sort_by { |p| p.hacker_news_score }.reverse

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(voteable, gravity:) ⇒ HackerNews

Returns a new instance of HackerNews.



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

def initialize(voteable, gravity:)
  @voteable = voteable
  @gravity = gravity
end

Class Method Details

.call(voteable, gravity: 1.8) ⇒ Float

Calculate the Hacker News score

Parameters:

  • voteable (ActiveRecord::Base)

    The voteable object

  • gravity (Float) (defaults to: 1.8)

    Decay rate (higher = faster decay, default 1.8)

Returns:

  • (Float)

    The score (higher = better)



28
29
30
# File 'lib/vote_fu/algorithms/hacker_news.rb', line 28

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

Instance Method Details

#calculateObject



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

def calculate
  points = [@voteable.plusminus - 1, 0].max
  age_hours = hours_since_creation

  return 0.0 if age_hours.negative?

  points / ((age_hours + 2)**@gravity)
end