Skip to content

Instantly share code, notes, and snippets.

@shoyan
Created March 22, 2016 09:38
Show Gist options
  • Save shoyan/95fe2a5199f7525a1c16 to your computer and use it in GitHub Desktop.
Save shoyan/95fe2a5199f7525a1c16 to your computer and use it in GitHub Desktop.
単語数の出現回数をカウントする
text =<<TEXT
The example above works as intended because the five instances of the (anonymous) inner function refer to five different instances of variable j. Note that it does not work as intended if you replace let by var or if you remove the variable j and simply use the variable i in the inner function.
TEXT
result = {}
result.default = 0
text.split(" ").map { |a| a.gsub(/\.$/, '').gsub(/\(|\)/, '').downcase }.sort.each { |b| result[b.to_sym]+=1 }
p result.sort {|(k1, v1), (k2, v2)| v2 <=> v1 }
@shoyan
Copy link
Author

shoyan commented Mar 22, 2016

結果

[[:the, 6], [:variable, 3], [:if, 2], [:as, 2], [:of, 2], [:you, 2], [:intended, 2], [:instances, 2], [:inner, 2], [:five, 2], [:function, 2], [:j, 2], [:i, 1], [:in, 1], [:example, 1], [:does, 1], [:different, 1], [:it, 1], [:by, 1], [:let, 1], [:not, 1], [:note, 1], [:because, 1], [:or, 1], [:refer, 1], [:remove, 1], [:replace, 1], [:simply, 1], [:that, 1], [:anonymous, 1], [:to, 1], [:use, 1], [:var, 1], [:and, 1], [:work, 1], [:works, 1], [:above, 1]]

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