有一些替代gsub只是替换一个字符串出现与另一个?
根据文档"如果替换是一个字符串,它将替换匹配的文本.它可能包含对格式\ d的模式捕获组的反向引用,其中d是一个组号,或者\ k,其中n是一个团队名字."
我不想要这个功能..
简化示例:
"abc".gsub("b", "\\7") -> "ac"
Run Code Online (Sandbox Code Playgroud)
我想"a\7c"..
问题是从用户那里询问替换字符串(实际上它是一个包含的路径"xxx\7.0\yyy"),如果它是一个简单的固定字符串我可以自己逃避它.
我知道这是有效的:"abc".gsub("b") { |x| "\\\7" }..但是像红宝石这样美丽的语言要学习到现在我相信应该有一个简单的替代方案,我在文档中忽略.
我试图找出一个Date对象是多少天前.
(Date.today - start_time).to_i
Run Code Online (Sandbox Code Playgroud)
我什么时候(Date.today - 30.days.ago.to_date).to_i得到29.思考?
def [](index)
case index
when 0, -2: @x
when 1, -1: @y
when :x, "x": @x
when :y, "y": @y
else nil
end
end
Run Code Online (Sandbox Code Playgroud)
array.rb:3: syntax error, unexpected ':', expecting keyword_then or ',' or ';' or '\n'
when 0, -2: @x
^
array.rb:5: syntax error, unexpected ':', expecting keyword_end
when :x, "x": @x
^
array.rb:6: syntax error, unexpected ':', expecting keyword_end
when :y, "y": @y
^
array.rb:8: warning: else without rescue is useless
array.rb:9: syntax error, unexpected keyword_end, expecting $end …Run Code Online (Sandbox Code Playgroud) 为什么nokogiri在服务器繁忙时等待几秒钟(3-5)并且我逐个请求页面,但是当这些请求处于循环中时,nokogiri不会等待并抛出超时消息.我正在使用超时阻止包裹请求,但nokogiri根本不等待那个时间.有关此的任何建议程序?
# this is a method from the eng class
def get_page(url,page_type)
begin
timeout(10) do
# Get a Nokogiri::HTML::Document for the page we’re interested in...
@@doc = Nokogiri::HTML(open(url))
end
rescue Timeout::Error
puts "Time out connection request"
raise
end
end
# this is a snippet from the main app calling eng class
# receives a hash with urls and goes throgh asking one by one
def retrieve_in_loop(links)
(0..links.length).each do |idx|
url = links[idx]
puts "Visiting link #{idx} of #{links.length}"
puts "link: …Run Code Online (Sandbox Code Playgroud) 我试图在我的ruby进程和Python进程之间进行一些通信; 我想使用UNIX套接字.
目标:ruby进程"fork和exec"的Python进程.在ruby进程中,创建一个UNIX套接字对,并将其传递给Python.
Ruby代码(p.rb):
require 'socket'
r_socket, p_socket = Socket.pair(:UNIX, :DGRAM, 0)
# I was hoping this file descriptor would be available in the child process
pid = Process.spawn('python', 'p.py', p_socket.fileno.to_s)
Process.waitpid(pid)
Run Code Online (Sandbox Code Playgroud)
Python代码(p.py):
import sys
import os
import socket
# get the file descriptor from command line
p_fd = int(sys.argv[1])
socket.fromfd(p_fd, socket.AF_UNIX, socket.SOCK_DGRAM)
# f_socket = os.fdopen(p_fd)
# os.write(p_fd, 'h')
Run Code Online (Sandbox Code Playgroud)
命令行:
ruby p.rb
Run Code Online (Sandbox Code Playgroud)
结果:
OSError: [Errno 9] Bad file descriptor
Run Code Online (Sandbox Code Playgroud)
我希望ruby进程将文件描述符传递给python进程,以便这两个可以使用这些套接字发送数据.
所以,我的问题:
1)是否可以像上面那样在ruby和python进程之间传递打开的文件描述符?
2)如果我们可以在两个进程之间传递文件描述符,那么我的代码中有什么问题.
我有一个启动子进程的Ruby脚本.我希望他们在整个过程被杀死时被杀死.
IO.popen('testacular start unit.conf.js', 'w')
Run Code Online (Sandbox Code Playgroud)
运行我的脚本:
user.name:/my/repo [git: my-branch] $ ruby my-script.rb
Run Code Online (Sandbox Code Playgroud)
来自testacular的输出:
user.name:/my/repo [git: my-branch] $ info: Testacular server started at http://localhost:8000/
info (launcher): Starting browser PhantomJS
info (PhantomJS 1.7): Connected on socket id uVAO41Q2niyLA8AqbZ8w
PhantomJS 1.7: Executed 44 of 44 SUCCESS (0.213 secs / 0.115 secs)
Run Code Online (Sandbox Code Playgroud)
点击Control-C来杀死进程.检查正在运行的进程:
user.name:/my/repo [git: my-branch] $ ps
PID TTY TIME CMD
# ...
39639 ttys019 0:01.28 node /usr/local/bin/testacular start unit.conf.js
39649 ttys019 0:00.09 node /usr/local/bin/phantomjs /var/folders/2p/dklw3xys2n3f4hqmx73zvz6w0000gp/T/testacular-61981618/capture.js
39650 ttys019 0:00.82 /usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs /var/folders/2p/dklw3xys2n3f4hqmx73zvz6w0000gp/T/testacular-61981618/capture.js
Run Code Online (Sandbox Code Playgroud)
我们可以看到testacular进程仍在运行.
手动杀死它,看看典型的testacular关闭输出: …
是active_support/core_ext/date_and_time/calculations为了什么?分别有/date、/time、/date_time和Date、Time和DateTime。但到底是/date_and_time为了什么?
我尝试使用 require 它而不是date_time,然后我得到了文件中定义的方法的 NoMethodError 。
我正在和Rubocop玩耍,有一些约定我比标准Rubocop遵循。考虑以下:
if some_variable and another
do_this
end
Run Code Online (Sandbox Code Playgroud)
Rubocop抱怨我应该使用&&而不是and这里。我检查了文件和默认设置Style/AndOr就是always。但是,它确实具有设置(conditionals),允许我执行以下操作:
model.save! and return
Run Code Online (Sandbox Code Playgroud)
...但是我只想为我的Rails项目完全关闭此标准。Rubocop确实让我对每个实例执行以下操作:
# rubocop:disable Style/AndOr
if check_this and check_that
# rubocop:enable Style/AndOr
do_something
end
Run Code Online (Sandbox Code Playgroud)
...但是那似乎真的很乏味,所以如果我可以Style/AndOr完全禁用它,我更喜欢它。这可能吗,如果可以,我将如何去做?
我有一个像这样的字符串
JINSAL0056( 1), JINSAL0057( 1), JINSAL0041( 1),
Run Code Online (Sandbox Code Playgroud)
我需要将JINSAL0056,JINSAL0057,JINSAL0041放在()中的字段和数字中.如果我朝着正确的方向前进,我已经写了几个代码让我知道.
s = "JINSAL0056( 1), JINSAL0057( 1), JINSAL0041( 1)"
ss = s.split(",")
sss = ss.split(" ( ")
Run Code Online (Sandbox Code Playgroud)
如何写分裂.请帮帮我吧
我试图用我的模型Tribune显示最后一个实例记录这个布局:
一些随机论坛
一些随机论坛
最后录制的论坛报
我正在使用gem will_paginate,它允许我每页显示10个看台.问题是布局正在运行但应用于每个页面.每10个看台,一个被确定为"最后".显然,我希望只有一个论坛被确定为最后一个.
这是我的代码:
<div class="wrapping">
<% @tribunes.each do |tribune| %>
<div class="container">
<div class="mouse-out-container"></div>
<div class="row">
<% if tribune == @tribunes.last
%>
<h1>Last</h1>
<div class="col-xs-12 col-md-12">
<div class="card">
<div class="card-category">Popular</div>
<div class="card-description">
<h2><%= tribune.title %></h2>
<p><%= tribune.content.split[0...25].join(' ') %>...</p>
</div>
<img class="card-user" src="https://kitt.lewagon.com/placeholder/users/tgenaitay">
<%= link_to "", tribune, :class => "card-link" %>
</div>
<% else %>
<div class="col-xs-12 col-md-12">
<div class="card">
<div class="card-category">Popular</div>
<div class="card-description">
<h2><%= tribune.title %></h2>
<p><%= tribune.content.split[0...25].join(' ') %>...</p>
</div>
<img class="card-user" src="https://kitt.lewagon.com/placeholder/users/tgenaitay"> …Run Code Online (Sandbox Code Playgroud)