Skip to content

Instantly share code, notes, and snippets.

@rosiehoyem
Last active December 24, 2015 02:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rosiehoyem/6728316 to your computer and use it in GitHub Desktop.
Save rosiehoyem/6728316 to your computer and use it in GitHub Desktop.
Homework, Day 4
=begin
Write a method that will take a tweet, search it for
words that you can substitute, and return a substituted string
tweet. For instance, the tweet "Hello to you, I'm at home"
would become "Hi 2 u, I'm @ home". The client has provided
the following acceptable substitutes. => e
Objectives
1. Write a method to shorten a string based on the allowed substitutes
2. Write a method that iterates over the list of tweets, shortens
them, and prints the results to the screen
=end
string = "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!"
def words_from_string(string)
string.downcase.scan(/[\w']+/)
end
#1
def shorten_string(string)
word_arr = []
word_arr << string.downcase.scan(/[\w']+/)
word_arr.each do |x|
x.replace "2" if x == "to, two, too"
x.replace "4" if x == "for, four"
x.replace "b" if x == "be"
x.replace "u" if x == "you"
x.replace "@" if x == "at"
x.replace "&" if x == "and"
end
short_string == word_arr.join(" ")
end
shorten_string(string)
puts
#2
def tweet_shortener
if string.length <= 140
puts string
else
shorten_string(string)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment