Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check whether the given text is in 140 character limit. URL is taken into account.
# -*- coding: utf-8 -*-
TWEET_MAX_LEN = 140
def tweetable?(text)
text = text.dup
len = 0
while text.sub!(/http:\/\/[^\s]*/, '')
len += 20
end
while text.sub!(/https:\/\/[^\s]*/, '')
len += 21
end
(len + text.length) <= TWEET_MAX_LEN
end
tweetable?('こんにちは') # => true
tweetable?('' * 140) # => true
tweetable?(''*119 + " http://mikutter.hachune.net/") # => true
tweetable?(''*120 + " http://mikutter.hachune.net/") # => false
tweetable?(''*118 + " https://mikutter.hachune.net/") # => true
tweetable?(''*119 + " https://mikutter.hachune.net/") # => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.