Tag: 逃离

麻烦用Ruby的’open-uri’打开utf-8 URI

我正在尝试使用ruby和open-uri从谷歌地图Web服务API获取丹麦的位置地址。 试图让Ærø,丹麦 : http : //maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false&region=dk在Chrome中运行时不支持open-uri: require ‘rubygems’ require “open-uri” require ‘json’ uri = “http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false&region=dk” response = open(uri) array = JSON.parse(response) pp array 在这里它产生 /usr/lib/ruby/1.8/uri/common.rb:436:in `split’: bad URI(is not URI?): http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false&region=dk (URI::InvalidURIError) 另一种方法是逃避角色: uri = “http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false&region=dk” uri_escaped = URI.escape(uri) response = open(uri_escaped) array = JSON.parse(response.read) pp array 但这会产生一个逃脱的结果(这不是追求:-) 任何人都知道什么可以解决这个问题(获得未转发的反馈或发送utf-8请求)? 这里的Ruby版本是1.8.7

如何在Ruby中捕获终端箭头键?

如何在终端中捕获箭头键,而无需用户在箭头键后按“Enter”键?

将字符串文字中的状态转义为`String#tr`的参数

关于在单引号字符串文字中反斜杠的转义状态作为String#tr参数,我有些神秘感。 你能解释下面三个例子之间的对比吗? 我特别不理解第二个。 为了避免复杂化,我在这里使用’d’ ,这不会改变双引号转义时的含义( “\d” = “d” )。 ‘\\’.tr(‘\\’, ‘x’) #=> “x” ‘\\’.tr(‘\\d’, ‘x’) #=> “\\” ‘\\’.tr(‘\\\d’, ‘x’) #=> “x”