问题描述
比下面的ruby代码,是否有一个“更好/替代/更具可读性”的解决方案来http://www.pythonchallenge.com/pc/def/linkedlist.php?
我能理解,但对我来说似乎不太清楚...
#!/usr/bin/env ruby -w
# encoding: utf-8
require \'net/http\'
Net::HTTP.start \'www.pythonchallenge.com\' do |http|
nothing = 12345
nothing = case http.get(\"/pc/def/linkedlist.php?nothing=#{nothing}\").body
when /(\\d+)$/ then $1.to_i
when /by two/ then nothing / 2
when /\\.html/ then puts $`
end while nothing
end
解决方法
没关系,但让我们尝试使其更具可读性:
#!/usr/bin/env ruby -w
# encoding: utf-8
require \'net/http\'
Net::HTTP.start \'www.pythonchallenge.com\' do |http|
next_one = 12345
while next_one
response = http.get(\"/pc/def/linkedlist.php?nothing=#{next_one}\").body
case response
when /Divide by two and keep going./ then
next_one = next_one / 2
when /and the next nothing is (\\d+)/ then
next_one = $1.to_i
else
puts \"Solution url: www.pythonchallenge.com/pc/def/linkedlist.php?nothing=#{next_one}\"
next_one = false
end
end
end