Rosetta Code/Rank languages by popularity: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: re-write to remove limits on number of categories.)
(→‎{{header|Ruby}}: use the Programming_Languages category)
Line 460: Line 460:
<lang ruby>require 'rosettacode'
<lang ruby>require 'rosettacode'


langs = Hash.new(0)
module RosettaCode
RosettaCode.rc_tasks("Programming_Languages") do |lang|
def RosettaCode.rc_all_categories
sleep 1 # don't kill the server
query = {
lang = (lang.split(":"))[-1]
"action" => "query",
RosettaCode.rc_tasks(lang) do |task|
"list" => "allcategories",
langs[lang] += 1
"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|
categories << category.text
end

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 "There are #{langs.length} languages"
puts "the top 10:"
puts "the top 10:"
categories.sort_by {|key,val| val}.reverse.each_with_index do |pair, i|
langs.sort_by {|key,val| val}.reverse.each_with_index do |pair, i|
category, count = pair
lang, count = pair
puts "#{i+1}. #{count} - #{category}"
puts "#{i+1}. #{count} - #{lang}"
end</lang>
end</lang>
<pre>There are 571 categories
<pre>There are 139 languages
the top 10:
the top 10:
1. 313 - Tcl
1. 313 - Tcl
2. 312 - Programming Tasks
2. 270 - Python
3. 270 - Python
3. 236 - Ada
4. 236 - Ada
4. 232 - Ruby
5. 232 - Ruby
5. 229 - C
6. 229 - C
6. 222 - Perl
7. 222 - Perl
7. 209 - OCaml
8. 209 - OCaml
8. 206 - Java
9. 205 - Java
9. 200 - Haskell
10. 200 - Haskell</pre>
10. 198 - ALGOL 68</pre>


=={{header|Tcl}}==
=={{header|Tcl}}==