嗨,我正在努力完成一个周末的大学项目,但遇到了一个障碍。
该项目的基本思想是使用rails和jQuery移动在谷歌地图上显示地理标记的推文。我没有使用twitter api,而是选择了datasift流api。
为了让它正常工作,我需要向datasift api传递一个查询字符串,如下所示:
'twitter.geo exists AND interaction.geo geo_radius "53.33754457198889,-6.267493000000059:10"'其中地理位置"53.33754457198889,-6.267493000000059“是中心,"10”是要捕获地理标记推文的半径(以英里为单位)。
我想在字符串中传递变量,例如
'twitter.geo exists AND interaction.geo geo_radius "#{@users_location}:#{radius}"'但是单引号意味着我不能使用这个字符串,我已经尝试过用不同的方式连接这个字符串,没有使用joy,所以如果任何人有一些建议或解决方案,我将不胜感激
发布于 2012-05-03 20:26:44
您还可以使用%( )来引用字符串,如%(twitter.geo exists AND interaction.geo geo_radius "#{@users_location}:#{radius}")。
有关您可能使用的其他一些替代方案,请参阅this。
发布于 2012-05-03 20:28:12
您不能将变量传递到单引号字符串中,但有几种方法可以获取您想要的内容:
# use a double quouted string and escape the quotes inside the string
"twitter.geo exists AND interaction.geo geo_radius \"#{@users_location}:#{radius}\""
# use single quotes and concat several strings
'twitter.geo exists AND interaction.geo geo_radius "' + @users_location + ':' + radius + '"'
# just use single quotes in a double quoted string
"twitter.geo exists AND interaction.geo geo_radius '#{@users_location}:#{radius}'"
# as the other answers suggest use %()
%(twitter.geo exists AND interaction.geo geo_radius "#{@users_location}:#{radius}")发布于 2012-05-03 20:27:31
我更喜欢%() (另一个答案),但您也可以在双引号字符串中使用\"转义引号。所有这些信息都可以在几乎任何Ruby string教程/文档中找到。
您还可以使用此处的字符串:
s = <<FOO
twitter.geo exists AND interaction.geo geo_radius "#{@users_location}:#{radius}"
FOOhttps://stackoverflow.com/questions/10431291
复制相似问题