https://github.com/sg552/happy_book_rails,一个创业者在培养rails团队时的教程积累,
http://siwei.me/,申思维个人博额,有较多的web原创教程
https://github.com/fouber/blog,一个UC开发者博客,有较多的前端开发的原创教程
http://guides.rubyonrails.org/ rails指南
作者归档:xinlu
转:Ruby 里的 %Q, %q, %W, %w, %x, %r, %s, %i
%Q
用于替代双引号的字符串. 当你需要在字符串里放入很多引号时候, 可以直接用下面方法而不需要在引号前逐个添加反斜杠 (\")
>> %Q(Joe said: "Frank said: "#{what_frank_said}"")
=> "Joe said: "Frank said: "Hello!"""
(...)也可用其他非数字字母的符号或成对的符号代替, 诸如[...], !...!, +...+,{...}, <...>等.
以下写法全部与上面等效:
>> %Q!Joe said: "Frank said: "#{what_frank_said}""!
>> %Q[Joe said: "Frank said: "#{what_frank_said}""]
>> %Q+Joe said: "Frank said: "#{what_frank_said}""+
除此之外还可省略Q写作:
>> %/Joe said: "Frank said: "#{what_frank_said}""/
=> "Joe said: "Frank said: "Hello!"""
%q
与%Q类似, 但是表示的是单引号字符串
>> %q(Joe said: 'Frank said: '#{what_frank_said} ' ')
=> "Joe said: 'Frank said: '\#{what_frank_said} ' '"
%W
语法近似于%Q, 用于表示其中元素被双引号括起的数组.
>> %W(#{foo} Bar Bar\ with\ space)
=> ["Foo", "Bar", "Bar with space"]
%w
用于表示其中元素被单引号括起的数组. 比较奇怪的是\(斜杠空格)会被转化成(空格), 但是其他的内容不会.
>> %w(a b c\ d \#e #{1}f)
=> ["a", "b", "c d", "\\#e", "\#{1}f"]
%x
使用`方法执行一段shell脚本并返回标准输出内容.
>> %x(echo foo:#{foo})
=> "foo:Foo\n"
%r
语法近似于%Q, 用于正则表达式.
>> %r(/home/#{foo})
=> "/\\/home\\/Foo/"
%s
用于表示symbol, 但是不会对其中表达式等内容进行转化
>> %s(foo)
=> :foo
>> %s(foo bar)
=> :"foo bar"
>> %s(#{foo} bar)
=> :"\#{foo} bar"
%i
Ruby 2.0 之后引入的语法, 用于生成一个symbol数组
2.0.0p247 :014 > %i(a b c)
=> [:a, :b, :c]
nginx+mina+puma的ruby部署
Ruby on Rails作为一款十分优秀的web开发框架,在当前web领域中慢慢占据了越来越重要,秉承rails快速开发的特点,很多快速部署rails的方案也越来越多。这篇文章中所选的方案是我个人认为十分优秀的部署方案。这套部署方案的结构是,nginx作为反向代理服务器负责负载均衡,mina作为自动化部署工具,puma作为rails的web服务器
Ruby的rails调试
“C:\Program Files\JetBrains\RubyMine 2017.1.5\bin\runnerw.exe” C:\RailsInstaller\Ruby2.3.0\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/ruby-debug-ide-0.6.1.beta4/bin/rdebug-ide –disable-int-handler –evaluation-timeout 10 –rubymine-protocol-extensions –port 63982 –host 0.0.0.0 –dispatcher-port 63983 — D:/RubymineProjects/WebApi/bin/rails server -b 127.0.0.1 -p 3000 -e development
Fast Debugger (ruby-debug-ide 0.6.1.beta4, debase 0.2.2.beta9, file filtering is supported) listens on 0.0.0.0:63982
——————–
根据上述的提示,Ruby的调试,需要依赖ruby-debug-ide和debase这两个组件。
按版本安装它,操作如下:
gem install ruby-debug -v 0.6.1.beta4
gem install debase -v 0.2.2.beta9
Install Ruby on Rails with Rbenv on CentOS 7
1.First, you need to install dependencies for rbenv and Ruby:
sudo yum install -y git-core zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl sqlite-devel
2.Install rbenv and ruby-build, be sure that you are in your non-root sudo user’s home directory:
cd ~
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
3.You need to determine the version of Ruby that you need. You can list available Ruby versions for installation with the following command:
rbenv install -l
4.Here, I will install the latest stable version, Ruby 2.2.3:
rbenv install -v 2.2.3
rbenv rehash
5.If you want to use another version, just install the version as above:
rbenv install -v 2.2.0
rbenv rehash
6.You can check all the versions you have installed with:
rbenv versions
The version with * is the active version
7.So, at the very least, you need to set your favorite version as global version for daily use:
rbenv global 2.2.3
8.Verify your choice with:
ruby -v
9.Also, you need to install the bundler gem to manage your application dependencies:
gem install bundler
10.Install the latest version of Rails:
gem install rails
rbenv rehash
gem install rails -v 版本号
gem uninstall rails -v 版本号
11.Check if Rails is installed properly:
rails -v
PHP函数在线测试
在线执行不同版本的同一份php代码,可以方便进行不同版本的函数测试。
http://sandbox.onlinephpfunctions.com/
PHP的伪注解:@var
/* @var $apiService MapiService */请注意:@var前是需要空格隔开的。
$apiService = Factory::create ( 'MapiService' );
$orderInfo = $apiService->getOrderInfo($orderSn);
elasticsearch数据持久化
elasticSearch数据持久化,默认情况不启用数据保存,故数据一般会几分钟就消失,按以下步骤保存索引数据。
#不同的集群名字不能相同。
cluster.name: es_vm_test
node.name: vmmaster
network.host: 0.0.0.0
http.port: 9200
#数据索引保存
path.data: /home/abc/elk-5.5.1/elkdata/data
path.logs: /home/abc/elk-5.5.1/elkdata/log
#关闭登录验证
xpack.security.enabled: false
ELK中文文档教程
ELK中文文档教程
https://kibana.logstash.es/content/
logstash抓取nginx日志
以下是基于elk+lnmp开源进行测试验证。
也可以参考官网的实现方法:https://kibana.logstash.es/content/logstash/plugins/codec/json.html
https://kibana.logstash.es/content/logstash/plugins/codec/multiline.html
在官网文档中,有较多应用场景:
https://kibana.logstash.es/content/
https://kibana.logstash.es/content/logstash/examples/
1.抓取nginx日志
input {
file {
# path => ["/home/wwwlogs/h5.vim.vim.com.log", "/home/wwwlogs/h5.vim.vim.com2.log"]
path => "/home/wwwlogs/h5.vim.vim.com.log"
exclude => "*.zip"
type => "java"
add_field => [ "domain", "h5.vim.vim.com" ]
codec => multiline {
pattern => "^\s+"
what => previous
}
}
file {
# path => ["/home/wwwlogs/h5.api.vim.vim.com.log", "/home/wwwlogs/h5.api.vim.vim.com2.log"]
path => "/home/wwwlogs/h5.api.vim.vim.com.log"
exclude => ["*.zip", "*.gz"]
type => "java"
add_field => [ "domain", "h5.api.vim.vim.com" ]
codec => multiline {
pattern => "^\s+"
what => previous
}
}
}
filter {
}
output {
stdout {
codec => rubydebug
}
elasticsearch {
hosts => ["0.0.0.0:9200"]
index => "logstash-%{domain}-%{+YYYY.MM.dd}"
}
}
2.定期清理索引
#!/bin/bash
# --------------------------------------------------------------
# This script is to delete ES indices older than specified days.
# Version: 1.0
# --------------------------------------------------------------
function usage() {
echo "Usage: `basename $0` -s ES_SERVER -d KEEP_DAYS [-w INTERVAL]"
}
PREFIX='logstash-'
WAITTIME=2
NOW=`date +%s.%3N`
LOGPATH=/apps/logs/elasticsearch
while getopts d:s:w: opt
do
case $opt in
s) SERVER="$OPTARG";;
d) KEEPDAYS="$OPTARG";;
w) WAITTIME="$OPTARG";;
*) usage;;
esac
done
if [ -z "$SERVER" -o -z "$KEEPDAYS" ]; then
usage
fi
if [ ! -d $LOGPATH ]; then
mkdir -p $LOGPATH
fi
INDICES=`curl -s $SERVER/_cat/indices?h=index | grep -P '^logstash-.*\d{4}.\d{2}.\d{2}' | sort`
for index in $INDICES
do
date=`echo $index | awk -F '-' '{print $NF}' | sed 's/\./-/g' | xargs -I{} date -d {} +%s.%3N`
delta=`echo "($NOW-$date)/86400" | bc`
if [ $delta -gt $KEEPDAYS ]; then
echo "deleting $index" | tee -a $LOGPATH/es_delete_indices.log
curl -s -XDELETE $SERVER/$index | tee -a $LOGPATH/es_delete_indices.log
echo | tee -a $LOGPATH/es_delete_indices.log
sleep $WAITTIME
fi
done