Skip to content

Instantly share code, notes, and snippets.

@keating
keating / fork研究.rb
Created September 23, 2012 03:49
使用fork启动一个进程
pid = fork { `nohup ruby GpsReader.rb 1 &` }
# 这种写法能得到一个子进程的PID,但不是shell命令所运行进程的PID,若shell命令启动一个服务(一直运行),那么此子进程也不会死掉(与block是个while true,子进程便不会死掉一样)
pid = fork { exec "`nohup ruby GpsReader.rb 1 &`" }
# 这种写法能得到一个子进程的PID,同样不是shell命令进程的PID,子进程马上就结束了(ps S -C ruby或ps S -p your_PID),是exec方法在起作用(Replaces the current process by running the given external _command_),所以,在ruby代码运行一个shell命令时,使用exec要比直接`少一个进程
Process.detach(pid)
puts pid
while true
puts "i am father..."
sleep 2
@keating
keating / IE9下colspan不起作用的解决办法.html
Created September 25, 2012 01:49
IE9下colspan不起作用的解决办法:在最前面加一个tr,其中添加colspan数量的td
<style type="text/css">
td {
border: 1px solid #ccc;
height: 24px;
padding: 2px;
vertical-align: top;
}
.ie9-bug-tr td {
height: 0px;
@keating
keating / 用数组存储.rb
Created October 12, 2012 15:35
动态规划研究
# 返回城市距离
def dis e1, e2
if (e1 == "A" and e2 == "E") || (e1 == "E" and e2 == "B") || (e1 == "B" and e2 == "F")
return 5
end
10
end
@boxes = []
@keating
keating / gps_reader.rb
Created December 20, 2012 01:47
Soap with Ruby gem Savon
def fetch_gps
$client ||= Savon.client("http://...ip.../GPSInfo/Service.asmx?WSDL")
data_hash, data_qty = {}, 0
@equipments.each do |equ_no|
begin
response = $client.request "getGPSInfoByID" do
$client.http.headers["Content-Type"] = "text/xml; charset=utf-8"
$client.http.headers["SOAPAction"] = "http://tempuri.org/getGPSInfoByID"
soap.body = {:id => equ_no}
@keating
keating / tracker_remind.rb
Last active December 10, 2015 00:28
use Mechanize
agent = Mechanize.new
page = agent.get('http://....../login')
form = page.forms[0]
form["user_session[login]"] = "......"
form["user_session[password]"] = "......"
form.submit
#puts page.uri
page = agent.get('http://...../synchrons')
#puts page.uri
#puts page
@keating
keating / ActiveRecord_connection.rb
Created December 27, 2012 02:44
use ActiveRecord with in ruby script
require "active_record"
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "localhost",
:username => "map",
:password => "map",
:database => "lspf"
)
@keating
keating / customize validation.rb
Last active December 10, 2015 12:28
You can use the 't' method with `I18n.t` in models. And the first parameter of the 'add' method here should be empty string for not be reduplicate. A new method is not necessary, see the second file.
validate :start_date_validate
def start_date_validate
if start_date && end_date
if start_date >= end_date
errors.add("", I18n.t("education_information.start_end_not_validate"))
end
end
end
@keating
keating / jruby-test.txt
Last active December 10, 2015 19:38
jruby 性能测试
===development/webrick================================================================
内存:
启动(点击首页) 查询提单 打开两个浏览器轮流查询 2分钟不操作
CRuby 1.8% 2.5% 3.5% 3.5%
JRuby 10.9% 11.0% 11.9% 11.9%
CPU在查询提单时最大占用率(大约)
CRuby 30%
JRuby 40%
@keating
keating / cpu.rb
Created January 12, 2013 09:31
测试JRuby性能代码 Codes for test JRuby performance || Comparing ruby1.9 and JRuby performance
2.times.map {
Thread.new { while true do end }
}.each(&:join)
@keating
keating / 测试.rb
Last active December 11, 2015 09:28
#1
# 测试登录,omniauth-identity
it "will show save search button in home" do
OmniAuth.config.test_mode = false
user ||= FactoryGirl.create(:identity_first)
visit login_path
within("form") do
fill_in 'auth_key', with: user.email