Skip to content

Instantly share code, notes, and snippets.

@tomykaira
Created December 11, 2011 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomykaira/1458148 to your computer and use it in GitHub Desktop.
Save tomykaira/1458148 to your computer and use it in GitHub Desktop.
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