Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby解决方案中的pythonchallenge.com 4_Ruby - Fatal编程技术网

ruby解决方案中的pythonchallenge.com 4

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}

有没有比下面的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}").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:in
start”来自/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
。(我是从更高的值开始尝试的)。