Skip to content

Instantly share code, notes, and snippets.

@Rosa-Fox
Created February 14, 2014 14:25
Show Gist options
  • Save Rosa-Fox/9001829 to your computer and use it in GitHub Desktop.
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
#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
@Najaf
Copy link

Najaf commented Feb 14, 2014

OH! Are you thinking that tweet.split(" ") modifies the tweet in place?

Once you've run tweet.split(" "), tweet is still a string, it remains so unmodified. The statement tweet.split(" ") returns an array, it doesn't transform the contents of tweet into an array. At the moment you're not doing anything with the value, so it's discarded.

If you had done this:

words = tweet.split(" ")

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's pop, push, shift and unshift.

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 and flatten!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment