Class: VoteFu::Algorithms::HackerNews
- Inherits:
-
Object
- Object
- VoteFu::Algorithms::HackerNews
- 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)
Class Method Summary collapse
-
.call(voteable, gravity: 1.8) ⇒ Float
Calculate the Hacker News score.
Instance Method Summary collapse
- #calculate ⇒ Object
-
#initialize(voteable, gravity:) ⇒ HackerNews
constructor
A new instance of HackerNews.
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
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
#calculate ⇒ Object
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 |