分类目录归档:Ruby

ruby输出文本的几种方法

模板使用指南
http://guides.rubyonrails.org/layouts_and_rendering.html

使用模板
render template: "products/show"
输出文件内容
render file: "/u/apps/warehouse_app/current/app/views/products/show"
输出HTML
render html: "Not Found".html_safe
输出文本
render plain: "OK"
输出JSON
render json: @product

学习ruby的好书

https://github.com/sg552/happy_book_rails,一个创业者在培养rails团队时的教程积累,
http://siwei.me/,申思维个人博额,有较多的web原创教程
https://github.com/fouber/blog,一个UC开发者博客,有较多的前端开发的原创教程
http://guides.rubyonrails.org/ rails指南

转: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