A prominent feature on medium.com is the concept of displaying how long it will take to read an article. Here's a screenshot of what it looks like:
This feature is nice because it gives the viewer an easy-to-understand representation of how big an article is. I've gone ahead and adapted that feature on my site as well. Let's look at what it takes to calculate this with code directly taken from this website.
Imagine you have a Post
model with a body
attribute that represents the contents of the post. Here's a simple way to calculate 'word count':
def word_count
body.split.size
end
This will split body
on every space (' '
) and return the size of the array. Pretty simple! The next part is calculating how long it will take to read the article. Wikipedia suggests that adults read at about 250-300 words per minute, but we'll use 200 to account for the fact that people read slower on a monitor (also in the Wikipedia article). Here's what the method looks like for 'reading time':
def reading_time
(word_count / 200.0).ceil
end
It's that simple. Now you too can display 'reading time' on your blog to give your readers an easier gauge of how big an article is.
And this post should have taken you barely more than a minute to read.