Ruby入门--Linux/Windows下的安装、代码开发及Rails实战
http://www.linuxidc.com/Linux/2014-04/100242.htm
Ubuntu 13.04下Ruby的安装 http://www.linuxidc.com/Linux/2013-06/85734.htm
公司有项目组进行系统重构,采用了Ruby On Rails框架,我也抽出时间学习了一下,并对几个原来用Java开发的定时任务、消息监听进行了ruby改造,学习过程中主要参考两本书:《Programming Ruby中文版(第二版)》 (下载见http://www.linuxidc.com/Linux/2014-04/100254.htm )、《Ruby on Rails教程》(PDF下载见 http://www.linuxidc.com/Linux/2014-04/100253.htm ),开发工具:vim、RubyMine。
安装
Linux下:安装RVM和GEM后,之后所有的安装,都可以交给RVM和GEM了,安装RVM默认会装好GEM。
(1)curl -L https://get.rvm.io | bash -s stable
(2)source /etc/profile.d/rvm.sh ,这个脚本的目的是把rvm相关加入$PATH路径
安装完毕后界面如下(这个是从github下载代码,国内非常慢,多尝试几次):
安装RVM之后,安装ruby组件:
(1)安装ruby:rvm install ruby
这一步rvm会帮我们自动安装很多linux依赖包(如果linx本身没有的话),如果自动装不上,也可以根据屏幕提示收到安装,比如可能有提示进行如下安装:
yum install -y libyaml-devel autoconf gcc-c++ readline-devel zlib-devel openssl-devel automake libtool bison
网速不好,机器太烂(比如我的单CPU1G内存虚拟机),可能会花很多时间(比如2个小时),先有个心理准备
(2)安装rails:gem install rails
输入ruby -v ,显示ruby的版本,可以看到ruby已经装好。
我们也可以通过which ruby,查看ruby程序的位置。之所以直接输入ruby就能运行,是因为rvm已经把ruby加到$PATH环境变量中了。
我们输入echo $PATH,可以看到$PATH环境变量中已经包含ruby的路径了,如下:/usr/local/rvm/bin 。
HelloWorld
使用irb,写出第一行代码:puts "Hello, World!"
也可以编辑一个文件:test.rb,内容也只有一行:puts "Hello,World",然后再命令行运行:ruby test.rb,可以看到输出了正确的结果。
我们也可以这样设置test.rb,内容如下:
#!/usr/local/rvm/bin/ruby
puts "Hello,World"
然后再命令行给test.rb可执行的权限:chmod 777 test.rb,然后我们在命令行输入:./test.rb,可以看到,输出了正确的结果。
Mysql devel&client组件
(1)yum -y install mysql-devel
由于我的rhel6.2机器yum下面有错误的.repo文件,导致我一直连不上yum源服务器,执行了如下操作:
cd /etc/yum.repos.d/
rm -fr 无用的.repo文件
yum clean all
vim /etc/yum.repos.d/rhel-source.repo(这个文件的内容如下)
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://192.168.1.11/yum/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-RedHat-release
[rhel-source-beta]
name=Red Hat Enterprise Linux $releasever Beta - $basearch - Source
baseurl=ftp://ftp.redhat.com/pub/redhat/linux/beta/$releasever/en/os/SRPMS/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-beta,file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
(2)gem install mysql2
经过以上两步,就可以连接mysql数据库了,例子代码如下:
require 'mysql2'
class Test
def initialize
@db = Mysql2::Client.new(:host => "192.168.211.245",
:port => 3306,
:username => "mysql",
:password => "mysql",
:database => "yitian_b2c_db")
puts "a"
end
end
if __FILE__ == $0
test = Test.new
end
定时任务组件 rufus-scheduler
支持cron表达式,参考网址:http://www.linuxidc.com/Linux/2014-04/100244.htm
先安装:gem install rufus-scheduler
log4r
require 'logger' 就可以使用了,参考网址: http://www.linuxidc.com/Linux/2014-04/100249.htm
xml操作
例子代码如下:
buffer = "";
x = Builder::XmlMarkup.new(:target => buffer, :indent => 1)
x.instruct! :xml,:version => '1.0',:encoding =>'UTF-8'
x.comment! "书本信息"
x.library("shelf" => "Recent Acquisitions") {
x.section("name" => "ruby"){
x.book("isbn" => "0672310001"){
x.title "Programming Ruby"
x.author "Yukihiro"
x.description"Programming Ruby-
The Pramatic Programmer's Guide"
}
}
}
puts buffer
fh = File.new("xml.xml","w")
fh.puts buffer
fh.close
参考网址:
http://builder.rubyforge.org/
http://www.linuxidc.com/Linux/2014-04/100243.htm
http://www.linuxidc.com/Linux/2014-04/100248.htm
hessian
安装hessian:gem install hessian2
hessian代码示例:
require 'hessian2'
client = Hessian2::HessianClient.new(url)
client.xxx
程序完成及部署
源代码下载地址: http://pan.baidu.com/s/1dDejscx
历时3天,完成我的第一个ruby程序,是改写的一个定时xml生成程序,干的不错!
一点小经验积累,如下:
initialize里面,打开数据库连接,是不可取的。如果程序执行很长时间,下次来用这个连接时,可能已经关闭了,最好用时open,用完close,不知道ruby里面有没有连接池的概念,可以进一步提高性能;
测试环境实践发现,hessian协议性能貌似低于dubbo协议,大约dubbo 2-3倍于 hessian,这个程序是2.6倍,很简单的请求,平均dubbo一次是4-5毫秒,hessian一次是11-12毫秒;
生产环境实际运行,hessian协议性能貌似和dubbo协议基本一致,运行了约20万次,总计消耗时间7分钟和8分钟的区别。
RabbitMQ的Ruby客户端:bunny
安装:gem install bunny
开始Rails之旅
推荐PDF教程: http://www.linuxidc.com/Linux/2014-04/100253.htm 有这个可以说足够了。
sqllite
Linux机器可能没有安装Sqllite-devel,安装一下吧:yum install sqlite-devel
JavaScript服务器解释引擎
linux机器可能没有安装JavaScript的服务器解释引擎,安装一下吧:gem install execjs gem install therubyracer,貌似这两步之后也不行,我的机器是REHL6.2,需要安装node.js才行,步骤如下:
wget http://nodejs.org/dist/v0.10.5/node-v0.10.5.tar.gz
tar xfv node-v0.10.5.tar.gz
cd node-v0.10.5
./configure
make
make install
参考文章:http://www.linuxidc.com/Linux/2014-04/100252.htm
Git
Git安装:
Getting Started - Installing Git
Installing Git
Let’s get into using some Git. First things first—you have to install it. You can get it a number of ways; the two major ones are to install it from source or to install an existing package for your platform.
Installing from Source
If you can, it’s generally useful to install Git from source, because you’ll get the most recent version. Each version of Git tends to include useful UI enhancements, so getting the latest version is often the best route if you feel comfortable compiling software from source. It is also the case that many Linux distributions contain very old packages; so unless you’re on a very up-to-date distro or are using backports, installing from source may be the best bet.
To install Git, you need to have the following libraries that Git depends on: curl, zlib, openssl, expat, and libiconv. For example, if you’re on a system that has yum (such as Fedora) or apt-get (such as a Debian based system), you can use one of these commands to install all of the dependencies:
$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
$ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev
When you have all the necessary dependencies, you can go ahead and grab the latest snapshot from the Git web site:
http://git-scm.com/download
Then, compile and install:
$ tar -zxf git-1.7.2.2.tar.gz
$ cd git-1.7.2.2
$ make prefix=/usr/local all
$ sudo make prefix=/usr/local install
After this is done, you can also get Git via Git itself for updates:
$ git clone git://git.kernel.org/pub/scm/git/git.git
Installing on Linux
If you want to install Git on Linux via a binary installer, you can generally do so through the basic package-management tool that comes with your distribution. If you’re on Fedora, you can use yum:
$ yum install git-core
Or if you’re on a Debian-based distribution like Ubuntu, try apt-get:
$ apt-get install git
Installing on Mac
There are two easy ways to install Git on a Mac. The easiest is to use the graphical Git installer, which you can download from the Google Code page (see Figure 1-7):
http://code.google.com/p/git-osx-installer
Figure 1-7. Git OS X installer.
The other major way is to install Git via MacPorts (http://www.macports.org
). If you have MacPorts installed, install Git via
$ sudo port install git-core +svn +doc +bash_completion +gitweb
You don’t have to add all the extras, but you’ll probably want to include +svn in case you ever have to use Git with Subversion repositories (see Chapter 8).
Installing on Windows
Installing Git on Windows is very easy. The msysGit project has one of the easier installation procedures. Simply download the installer exe file from the GitHub page, and run it:
http://msysgit.github.com/
After it’s installed, you have both a command-line version (including an SSH client that will come in handy later) and the standard GUI.
Note on Windows usage: you should use Git with the provided msysGit shell (Unix style), it allows to use the complex lines of command given in this book. If you need, for some reason, to use the native Windows shell / command line console, you have to use double quotes instead of simple quotes (for parameters with spaces in them) and you must quote the parameters ending with the circumflex accent (^) if they are last on the line, as it is a continuation symbol in Windows.
Git配置与使用:与 推送到GitHub:教程见
注意:你必须先注册一个GitHub.Com账户,如果使用SSH方式进行推送,则需要创建SSH秘钥,需要在GitHub中建立Repository。
SSH推送到GitHub的命令如下:
git remote add origin git@github.com:<username>/first_app.git
$ git push -u origin master
<username>要替换成你在GitHub注册的用户名,master是你的分支名称,现在我们可以在GitHub中看到这个master分支了:
https://github.com/pumadong/first_app
关于Git,在Windows及Mac下面,都有GUI程序可用,在linux下面,貌似只有git命令可用了。
建立一个Raise项目并推送到GitHub
发布到云部署平台heroku
使用scoffold快速建立一个用户微博程序
演示从头到尾建立一个大型的示例程序
Windows下安装
rubyinstaller
安装包下载地址:http://pan.baidu.com/s/1dDejscx
官网下载:http://rubyinstaller.org/downloads
执行rubyinstaller-2.0.0-p353-x64.exe,进行安装,安装完毕后,cmd里面执行:ruby -v,显示版本号,说明ruby安装成功;
执行gem -v,显示版本号,说明RubyGems(Ruby程序包管理器)也已经被默认安装好了;
DevKit
安装DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe,解压到一个固定的位置,使用后不能变换位置,更多参考:
https://github.com/oneclick/rubyinstaller/wiki/Development-Kit
步骤大致如下:
1) 将下载 DevKit 解压到 D:DevKit 目录。
2) 打开 CMD 窗口,进入 D:DevKit 目录,输入ruby dk.rb init 。#生成config.yml,这里会检查将要添加DevKit支持的Ruby列表,只支持通过RubyInstaller安装的Ruby。
3) 输入 ruby dk.rb install #开始安装。
4) 输入 ruby dk.rb review #检查要添加DevKit支持的Ruby列表是否有误,可以略过。
5) 输入 gem install rdiscount --platform=ruby 。#这一步只是验证DevKit是否安装成功,如果能安装rdiscount成功说明安装DevKit成功,也可以不做。
Rails
安装完以上后,可以安装rails了:
从CMD提示窗口输入指令:gem install rails 开始安装rails。
如不想安装文档文件,可以输入:gem install rails --no-rdoc --no-ri
程序自动下载并安装rails, 耐心等待。
安装完成后,你可以在路径 D:Rubylib
ubygems1.9.1gems 看到些东西,都是rails的包文件,与ruby安装在同一目录下。
这时在CMD提示窗口输入指令: rails -v 显示rails的版本号。
参考网址:http://www.linuxidc.com/Linux/2014-04/100250htm
Mysql2
gem install mysql2 -- '--with-mysql-dir="E:Program FilesMySQLMySQL Server 5.5"'
拷贝到devkit/mingw/bin下,我之所以考到这里,是因为dlltool.exe也在这里,都放到path里方便。
然后运行:gendef.exe libmysql.dll。
这条命令会生成libmysql.def文件。
生成这个libmysql.def文件之后���就可以生成新的lib了。
dlltool -v –dllname libmysql.dll –def libmysql.def –output-lib libmysql.lib。
拷贝libmysql.lib到D:Rubyin目录下面,重新安装,
都OK了。