Created
February 14, 2014 14:25
-
-
Save Rosa-Fox/9001829 to your computer and use it in GitHub Desktop.
Aims to match words from the last 10 tweets to words in the CSV and print
This file contains hidden or 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
#Gets the last 10 tweets from a given user from the twitter API | |
screen_name = String.new ARGV[0] | |
#for each tweet | |
tweet = client.user_timeline(screen_name, count: 10).each do |tweet| | |
#Look through each row in the CSV file to see if any words in the tweet match the words in the 2nd element of the csv array(the word) | |
csv = CSV.foreach("csvratings.csv", {:headers=>true}).each do |row| | |
#For each line if the word matches words in the tweet then create a new array and put the valence mean value (3rd array element) in it. | |
#Splits the tweet string into an array to be matched with each element of csv | |
tweet.split(" ") | |
if row[1].match(tweet) | |
valence = [] | |
valance << row[2] | |
puts valence | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OH! Are you thinking that
tweet.split(" ")
modifies thetweet
in place?Once you've run
tweet.split(" ")
,tweet
is still a string, it remains so unmodified. The statementtweet.split(" ")
returns an array, it doesn't transform the contents oftweet
into an array. At the moment you're not doing anything with the value, so it's discarded.If you had done this:
Then
words
would contain the array of words in the tweet.There are methods in Ruby that effect objects in place. Notably on
Array
there'spop
,push
,shift
andunshift
.Other than those, by convention we usually have an exclamation point (which we sometimes call a bang, same as in the 'shebang line') at the end of the method name to indicate that the object is being modified in place. A method that modifies something in place is sometimes said to be "destructive" (it doesn't actually destroy anything, that's just what we call it).
If you look at the method listing for Array you'll notice there are destructive and non-destructive versions of methods, e.g.
flatten
andflatten!
.