Skip to content

Instantly share code, notes, and snippets.

@rpearce
Last active May 10, 2022 02:24
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 rpearce/b87f28f5ee61f3dd86e2 to your computer and use it in GitHub Desktop.
Save rpearce/b87f28f5ee61f3dd86e2 to your computer and use it in GitHub Desktop.
Ruby Screencast - Tweet Parser

Tweet Parser

Accepting Tweet Input

Let's put our new knowledge of Ruby strings to work by accepting username and tweet input and returning a sentence including both.

In your tweets.rb file, write the following into tweets.rb:

print "What is your username? "
username = gets
print "Enter tweet: "
tweet = gets

Next, Let's use interpolation, or embedding Ruby code in to a string, to output to the terminal Robert tweeted, "Loving this TIY screencast!":

print "#{username} tweeted, \"#{tweet}\""

Note the the back slashes (\) in front of the quotation marks. If we did not have them in there, the computer would think that we were trying to end our current string of characters, but we don't want that! We want to output quotations marks as a part of the string, so we must escape the " characters we don't want Ruby to pay much attention to.

Once you have this entered, save the file, and then from your Terminal run

$ ruby tweets.rb

When we enter our username and a tweet, look at what happens to our sentence: it ends up on three different lines. Something else is going on here; with the inspect command, we can see the exact characters — hidden and visible — that make up a string. Let's use the puts and inspect commands on our username and tweet variables to see exactly what is contained in each string.

print "What is your username? "
username = gets
print "Enter tweet: "
tweet = gets

puts username.inspect
puts tweet.inspect

print "#{username} tweeted, \"#{tweet}\""

As you can see, there is an escaped character, \n, on the end of each string. Computers have to be told when they should move text to a new line, for example, and the way you tell a computer to move text to a new line is by including \n in to a string. But we still need to understand from where the trailing \n originates.

When you entered your username in to What is your username: , what action did you take next? You hit the Enter or Return key to move to the next step. When you hit that key, the program captures that as input, as well. What a pain! That's not what we want to happen. We need to alter our gets command so that we can remove the trailing \n characters.

print "What is your username? "
username = gets.chomp
print "Enter tweet: "
tweet = gets.chomp

puts username.inspect
puts tweet.inspect

print "#{username} tweeted, \"#{tweet}\""

chomp will remove any trailing "carriage return characters," such as \n or \r, from the end of a Ruby string1. The chomp command is my absolute favorite command name in Ruby, for I imagine an alligator _chomp_ing off the trailing \n character from my string.

When we save and run the tweets.rb file again from our Terminal, we see that the \n characters have been removed from our strings, and our outputted sentence is correctly on one line. [RUN THE FILE AGAIN]

Hooray! The \ns are gone, and our quotation (the last line) is on one line. Now that we have debugged, or removed the issues from, our Ruby program, we can remove the extra lines of code that were _inspect_ing our variables. We can also change our final print to the puts command so that our output is on its own line no matter what.

print "What is your username? "
username = gets.chomp
print "Enter tweet: "
tweet = gets.chomp
puts "#{username} tweeted, \"#{tweet}\""

So to recap, we accepted string inputs from the user, learned about escaped characters and learned how to clean up our outputted text for readability.

OTHER STUFFS

now print the tweet's length...we'll cover how to validate it later

Tweet length Maths:

  • set maximum char count variable to 140 -> assignment and +
  • subtract from length to get how many characters they have left over -> -
  • convert this to a "percentage-chars-used" -> ÷ and * and float + combining with a string to get %

Tweet Hash

  • author (first name, last name), content
  • change author's last name
  • replace the content

Tweets Array:

  • can hold any type of data (we'll do hashes)
  • first to last
  • last to first
  • selecting at an index
  • deleting at an index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment