-
-
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 |
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 :)
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
According to the Ruby Style Guide, we should prefer
map
overcollect
.I read this SO entry and got scared about switching the multi-line map block from
{}
todo end
.I tested
hashtag_list.count(tag)
andhashtag_list.grep(tag).size
a few times withBenchmark.measure {}
in the console. The staggeringly minimal difference was in favor ofcount
.