Last active
December 24, 2015 01:29
-
-
Save ahimmelstoss/6723569 to your computer and use it in GitHub Desktop.
shortening tweets by turning them into arrays and iterating over them with a hash of substitutions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'awesome_print' | |
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!" | |
tweet5 = "to two too four for be you at and" | |
$substitutions_hash = { #global variable to work in method | |
"to" => "2", "two" => "2", "too" => "2", "for" => "4", "four" => "4", "be" => "b", | |
"you" => "u", "at" => "@", "and" => "&" | |
} | |
def tweet_substitute(tweet) | |
tweet_words = tweet.split(" ") | |
tweet_words.each_index do |index| | |
$substitutions_hash.each do |word, substitution| | |
if tweet_words[index] == word | |
tweet_words[index] = substitution | |
end | |
end | |
end | |
tweet = tweet_words.join(" ") | |
if tweet.size >= 140 | |
return tweet.slice(0, 140) | |
else | |
return tweet | |
end | |
end | |
ap tweet_substitute(tweet4) | |
#I made this one before realizing making the tweet into an array would be better | |
# def tweet_substitute2(tweet) | |
# tweet.gsub! " too ", " 2 " | |
# tweet.gsub! " two ", " 2 " | |
# tweet.gsub! " to ", " 2 " | |
# tweet.gsub! " four ", " 4 " | |
# tweet.gsub! " for ", " 4 " | |
# tweet.gsub! " be ", " b " | |
# tweet.gsub! " you ", " u " | |
# tweet.gsub! " at ", " @ " | |
# tweet.gsub! " and ", " & " | |
# if tweet.size >= 140 | |
# return tweet.slice(0, 140) | |
# end | |
# return tweet | |
# end | |
# # ap tweet_substitute2(tweet4) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment