Skip to content

Instantly share code, notes, and snippets.

@ivanbrennan
Created September 27, 2013 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ivanbrennan/6723925 to your computer and use it in GitHub Desktop.
Save ivanbrennan/6723925 to your computer and use it in GitHub Desktop.
Tweet shortening!
subs = {"to" => "2", "two" => "2", "too" => "2",
"for" => "4", "four" => "4", "be" => "b",
"you" => "u", "at" => "@", "and" => "&"}
def string_shortener(str, subs)
punks = [" ", ".", "?", "!", ","]
current_word = ""
shortened_str = ""
return str if str.length <= 140
str.each_char do |char|
if !punks.include?(char)
current_word += char
else
shortened_word = (subs[current_word] || current_word)
shortened_str += (shortened_word+char)
current_word = ""
end
end
return shortened_str[0,140] if shortened_str.length > 140
shortened_str
end
tweet1 = "Hey guys, can anyone teach me how to be cool? I really want to be the best at everything, you know what I mean? Tweeting is super fun you guys!!!!"
tweet2 = "OMG you guys, you won't believe how sweet my kitten is. My kitten is like super cuddly and too cute to be believed right?"
tweet3 = "I'm running out of example tweets for you guys, which is weird, because I'm a writer and this is just writing and I tweet all day. For real, you guys. For real."
tweet4 = "GUISEEEEE this is so fun! I'm tweeting for you guys and this tweet is SOOOO long it's gonna be way more than you would think twitter can handle, so shorten it up you know what I mean? I just can never tell how long to keep typing!"
tweets = [tweet1, tweet2, tweet3, tweet4]
def print_short_tweets(tweets, subs)
tweets.each do |tweet|
puts string_shortener(tweet, subs)
end
end
print_short_tweets(tweets, subs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment