向Wolfram Alpha提问

首先,我们需要用到open-uri

 

require 'open-uri'

 

然后,写一个方法。

 

  def ask(question)
    url = "http://www.wolframalpha.com/input/?i=" + URI.escape(question)
    buffer = open(url, "UserAgent" => "Ruby-Wolfram reader").read
    buffer[/Result.+title="([^"]+)/, 1].gsub('...', '')
  end

 

开始提问吧:

 

    time = ask("what's the time in America?")
    puts "The current time in America is #{time}"
    #The current time in America is 12:11:01 am EST  |  Sunday, December 27, 2009

    time = ask("what's the time in China?")
    puts "The current time in China is #{time}"
    #The current time in China is 1:11:05 pm CT  |  Sunday, December 27, 2009

    population = ask("what's the population of China?")
    puts "The population of China is #{population}"
    #The population of China is 1.31 billion people  (2007 estimate)

    temperature = ask("what's the current temperature in Suzhou?")
    puts "The temperature in Suzhou is #{temperature}"
    #The temperature in Suzhou is 2 deg C  (wind chill:  -2 deg C)

    date = ask("what's the date tomorrow?")
    puts "Tomorrow is #{date}"
    #Tomorrow is Monday, December 28, 2009

 

你可能感兴趣的:(C++,c,C#,Ruby)