ruby解决方案中的pythonchallenge.com 4
有没有比下面的ruby代码更好的/替代的/更可读的解决方案? 我能理解,但对我来说似乎不是很清楚ruby解决方案中的pythonchallenge.com 4,ruby,Ruby,有没有比下面的ruby代码更好的/替代的/更可读的解决方案? 我能理解,但对我来说似乎不是很清楚 #!/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}
#!/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
您的代码“in
block in”出现以下错误:“92118”的未定义方法“/”:字符串(NoMethodError)来自/Users/batman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:627:instart”来自/Users/batman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:490:in@拉斐尔·乌龟:好的,在next\u one=$1。添加了to\u i
。(我是从更高的值开始尝试的)。