Rosetta Code/Rank languages by popularity: Difference between revisions
Content deleted Content added
→{{header|Ruby}}: re-write to remove limits on number of categories. |
|||
Line 455: | Line 455: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Now that there are more than 500 categories, the URL given in the task description is insufficient. I use the RC API to grab the categories, and then count the members of each category. |
|||
Uses the <code>RosettaCode</code> module from [[Count programming examples#Ruby]] |
|||
<lang ruby>require ' |
<lang ruby>require 'rosettacode' |
||
module RosettaCode |
|||
entries = [] |
|||
def RosettaCode.rc_all_categories |
|||
query = { |
|||
"action" => "query", |
|||
"list" => "allcategories", |
|||
"format" => "xml", |
|||
"aclimit" => 500, |
|||
} |
|||
categories = [] |
|||
while true |
|||
url = rc_url "api.php", query |
|||
doc = REXML::Document.new open(url) |
|||
REXML::XPath.each(doc, "//c") do |category| |
|||
open("http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=500") do |f| |
|||
categories << category.text |
|||
for line in f |
|||
end |
|||
match = line.match(%r{>([^<>]*)</a> \((\d+) members?\)}) |
|||
entries << match[2] + ' - ' + match[1] if match |
|||
continue = REXML::XPath.first(doc, "//query-continue") |
|||
break if continue.nil? |
|||
cm = REXML::XPath.first(continue, "allcategories") |
|||
query["acfrom"] = cm.attribute("acfrom").value |
|||
end |
|||
categories |
|||
end |
|||
end |
|||
$stdout.sync=true |
|||
categories = Hash.new(0) |
|||
RosettaCode.rc_all_categories.each do |category| |
|||
sleep 1 # be kind to the server |
|||
RosettaCode.rc_tasks(category) do |task| |
|||
categories[category] += 1 |
|||
end |
end |
||
end |
end |
||
puts "There are #{categories.length} categories" |
|||
⚫ | |||
puts "the top 10:" |
|||
⚫ | |||
category, count = pair |
|||
puts "#{i+1}. #{count} - #{category}" |
|||
end</lang> |
end</lang> |
||
<pre>There are 571 categories |
|||
the top 10: |
|||
1. 313 - Tcl |
|||
2. 312 - Programming Tasks |
|||
3. 270 - Python |
|||
4. 236 - Ada |
|||
5. 232 - Ruby |
|||
6. 229 - C |
|||
7. 222 - Perl |
|||
8. 209 - OCaml |
|||
9. 205 - Java |
|||
10. 200 - Haskell</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |