我使用的是LESS样式语言.
考虑以下CSS:
.side-bg
{
background:url(../img/layout/side-bg.jpg) top no-repeat;
}
Run Code Online (Sandbox Code Playgroud)
现在我的所有图像都在../img/我希望能够将变量设置为图像路径的文件夹中,并像这样使用它:
@image-path: ../img;
.side-bg
{
background:url(@image-path/layout/side-bg.jpg) top no-repeat;
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.它不是一个大问题,如果图像文件夹发生变化,我总是可以使用查找和替换.我刚刚开始学习LESS,并想知道这样的事情是否可行.
我需要检查两个数组是否包含任何顺序的相同数据.使用虚构的compare方法,我想做:
arr1 = [1,2,3,5,4]
arr2 = [3,4,2,1,5]
arr3 = [3,4,2,1,5,5]
arr1.compare(arr2) #true
arr1.compare(arr3) #false
Run Code Online (Sandbox Code Playgroud)
我用过arr1.sort == arr2.sort,似乎有用,但是有更好的方法吗?
我正在使用PostgreSQL,并希望阻止某些必需的CHARACTER VARYING(VARCHAR)字段允许空字符串输入.
这些字段还需要包含唯一值,因此我已经使用了一个唯一约束; 但是,这不会阻止原始(唯一)空值.
基本示例,其中用户名必须是唯一的而不是空的
| id | username | password |
+----+----------+----------+
| 1 | User1 | pw1 | #Allowed
| 2 | User2 | pw1 | #Allowed
| 3 | User2 | pw2 | #Already prevented by constraint
| 4 | '' | pw2 | #Currently allowed, but needs to be prevented
Run Code Online (Sandbox Code Playgroud) 我担心在Web服务器上发布文档(例如XML)的经验不多,所以如果我对HTTP的理解不足,我会道歉.
我在127.0.0.1端口上的ruby应用程序中设置了一个基本的Mongrel Web服务器2000.(服务器).
我在同一台计算机上运行一个单独的Ruby应用程序.(客户端).
我需要客户端将XML文档POST到服务器.
我曾尝试使用Net :: HTTP来做到这一点,但我找不到一个明确的例子,告诉我应该做些什么.我有一个去,但遇到了错误.我已经打破了请求,使其尽可能基本:
http = Net::HTTP.new("127.0.0.1", 2000)
http.post('file', 'query=foo') #xc.rb line 6
Run Code Online (Sandbox Code Playgroud)
但它会导致以下错误
C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock': An existing connection was forcibly closed by the remote host. (Errno::ECONNRESET)
from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:141:in `rbuf_fill'
from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2551:in `read_new'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1319:in `block in transport_request'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1316:in `catch'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1293:in `request'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1286:in `block in request'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:745:in `start'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1284:in `request'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1307:in `send_entity'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1096:in …Run Code Online (Sandbox Code Playgroud) 我使用Nokogiri和Ruby来解释XML文件的内容.我想得到一个数组(或类似的)所有元素,<where>在我的例子中是直接的孩子.但是,我得到了各种文本节点(例如"\n\t\t\t"),这是我不想要的.有什么方法可以删除或忽略它们吗?
@body = "
<xml>
<request>
<where>
<username compare='e'>Admin</username>
<rank compare='gt'>5</rank>
</where>
</request>
</xml>" #in my code, the XML contains tab-indentation, rather than spaces. It is edited here for display purposes.
@noko = Nokogiri::XML(@body)
xml_request = @noko.xpath("//xml/request")
where = xml_request.xpath("where")
c = where.children
p c
Run Code Online (Sandbox Code Playgroud)
上面的Ruby脚本输出:
[#<Nokogiri::XML::Text:0x100344c "\n\t\t\t">, #<Nokogiri::XML::Element:0x1003350 name="username" attributes=[#<Nokogiri::XML::Attr:0x10032fc name="compare" value="e">] children=[#<Nokogiri::XML::Text:0x1007580 "Admin">]>, #<Nokogiri::XML::Text:0x100734c "\n\t\t\t">, #<Nokogiri::XML::Element:0x100722c name="rank" attributes=[#<Nokogiri::XML::Attr:0x10071d8 name="compare" value="gt">] children=[#<Nokogiri::XML::Text:0x1006cec "5">]>, #<Nokogiri::XML::Text:0x10068a8 "\n\t\t">]
我想以某种方式获得以下对象:
[#<Nokogiri::XML::Element:0x1003350 name="username" attributes=[#<Nokogiri::XML::Attr:0x10032fc name="compare" value="e">] children=[#<Nokogiri::XML::Text:0x1007580 "Admin">]>, #Nokogiri::XML::Element:0x100722c name="rank" …
我正在使用Ruby,需要在尝试创建新文件之前检测目录是否可写.
我尝试了以下代码,根据@path是否是目录,它正确返回true/false.但是,如果没有对@path文件夹的写入权限,它仍会返回true.
if File.directory?(@path) && File.writable?(@path)
#is a writeable directory
true
else
#is not a writeable directory
false
end
Run Code Online (Sandbox Code Playgroud)
我查看了File和Dir类的帮助,看不到任何允许我检查目录写权限的方法.有办法吗?
我只需要在Windows上使用Ruby 1.9.3.
我正在寻找一种方便和实用的方法来将编码值添加到Ruby中的URL查询字符串.目前,我有:
require 'open-uri'
u = URI::HTTP.new("http", nil, "mydomain.example", nil, nil, "/tv", nil, "show=" + URI::encode("Rosie & Jim"), nil)
p u.to_s # => "http://mydomain.example/tv?show=Rosie%20&%20Jim"
Run Code Online (Sandbox Code Playgroud)
这不是我正在寻找的,因为我需要得到
"http://mydomain.example/tv?show=Rosie%20%26%20Jim",所以这个show=值不会被截断.
Open :: URI有另一种方法可以做到这一点吗?如果没有,可以使用任何其他标准的Ruby或gem吗?
ruby ×5
arrays ×1
client ×1
comparison ×1
constraints ×1
css ×1
directory ×1
file ×1
http ×1
less ×1
net-http ×1
nokogiri ×1
open-uri ×1
permissions ×1
post ×1
postgresql ×1
ruby-1.9.3 ×1
uri ×1
urlencode ×1
varchar ×1
writable ×1
xml ×1
xpath ×1