Skip to content

Instantly share code, notes, and snippets.

@davekaro
Last active December 30, 2015 17:49
Show Gist options
  • Save davekaro/7863843 to your computer and use it in GitHub Desktop.
Save davekaro/7863843 to your computer and use it in GitHub Desktop.
timeline = client.user_timeline(@user_name, { count: 200, include_entities: true })
hashtag_list = timeline.map { |tweet| tweet.hashtags.collect(&:text) }
@hashtags = hashtag_list.uniq.map do |tag|
{ text: tag,
weight: hashtag_list.grep(tag).size,
link: { href: "https://twitter.com/search?q=%23#{tag}", target: '_blank', title: "#{tag}" } }
end
@hashtags = @hashtags.to_json
@davekaro
Copy link
Author

davekaro commented Dec 8, 2013

Re: map vs collect. Whatever works. I've heard many different preferences. I like to use collect when I'm just "collecting" one attribute of the objects in the array. I like to use map when I'm "mapping" the array to a different array, via some kind of transform.

Re: multi-line map block. I wasn't aware of the difference in associativity of the { character and the do keyword. However, you should check out http://devblog.avdi.org/2011/07/26/the-procedurefunction-block-convention-in-ruby/. I still follow the old way of line length. I think your block is functional, so sticking with curly braces would follow the new convention. I don't think you'll have the associativity issue here since you are doing assignment, which doesn't take two parameters.

Re: count vs grep(tag).size. I had never seed grep used on an arary. It's neat. But I think the count method is much easier to read. Bonus if it's slightly faster :)

@davekaro
Copy link
Author

davekaro commented Dec 8, 2013

Oh and https://github.com/JamesChevalier/hashtagged/blob/master/app.rb#L18 should be

hashtag_list = timeline.flat_map { |tweet| tweet.hashtags.map(&:text) } instead of
hashtag_list = timeline.map { |tweet| tweet.hashtags.map(&:text) }.flatten

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