RubyGems


用Ruby一定会用到 RubyGems, RubyGems是Ruby下的包管理器,用过maven的人都知道包管理器的重要性。


安装


以下安装过程在cygwin上实现


首先访问 RubyGems 官网 https://rubygems.org/pages/download



下载RubyGems包


先下载 RubyGems的zip包


https://rubygems.org/pages/download




ruby占用内存过多 ruby有啥用_ruby



把包解压开运行 setup.rb



ruby占用内存过多 ruby有啥用_rvm_02




安装完成



ruby占用内存过多 ruby有啥用_ruby_03




切换使用淘宝源


由于众所周知的原因,我们必须使用淘宝源,否则装不了rubyGems上的东西


$ gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
$ gem sources -l
*** CURRENT SOURCES ***

https://ruby.taobao.org
# 请确保只有 ruby.taobao.org


这样就可以用gem install xxxx 来安装东西了,具体请查阅相关文档



使用Centos


从这里开始就必须要使用Centos了,虽然在cygwin下一样可以安装RubyGems但是里面有很多必须要用到的包,比如mysql的包需要linux的编译环境才能安装成功,如果用cygwin需要做很多的准备工作比较麻烦,不如直接使用centos


在centos上安装ruby和 gems


sudo yum install ruby
sudo yum install gcc g++ make automake autoconf curl-devel openssl-devel zlib-devel httpd-devel apr-devel apr-util-devel sqlite-devel
sudo yum install ruby-rdoc ruby-devel
yum install rubygems


用Ruby访问mysql


用rubyGems安装mysql的支持非常简单


gem install mysql

不过在安装前请确保你安装过mysql,如果没有请执行

yum install mysql
yum install mysql-devel

否则你无法从gem安装mysql


使用


先做一个最简单的例子来访问mysql并查询student表



#!/usr/bin/ruby

require "rubygems"
require 'mysql'


begin
    con = Mysql.new 'localhost', 'root', 'root','test'
    puts "mysql version: #{con.get_server_info}"
    rs = con.query 'select * from student;'
    #get rows    
    n_rows = rs.num_rows
    puts "There are #{n_rows} rows in the result set"

    rs.each_hash do |row|
        puts row['name'] + "  " + row['age']
    end

rescue Mysql::Error => e
    puts e.errno
    puts e.error

ensure
    con.close if con
end

解释

  • 在ruby 1.8 之前需要先require ‘rubygems’ 才能 require ‘mysql’
  • 要使用 each_hash 才能够通过 row[‘column’] 取出row里面的具体字段
  • 请把关闭数据库连接的语句放到 ensure 里面

升级Ruby到1.9.3

虽然用yum可以简单快捷的安装ruby,但是你所安装的ruby版本也受限于yum源,我用的只有1.8.7,rails都安装不上,所以需要升级到1.9.3。这就需要使用RVM

何为RVM?


RVM就是 Ruby Version Manager 从字面意思上看就是ruby版本管理器,实际上他会构建出多个ruby版本环境,非常好用


从现在开始抛弃yum安装的ruby 和 gem那套把。开始使用RVM


STEP1 升级yum


yum update

STEP2 安装必要的包


# yum install gcc-c++ patch readline readline-devel zlib zlib-devel 
# yum install libyaml-devel libffi-devel openssl-devel make 
# yum install bzip2 autoconf automake libtool bison iconv-devel

STEP3 安装RVM


# curl -L get.rvm.io | bash -s stable

你会看到类似的输出结果

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 20511  100 20511    0     0   1120      0  0:00:18  0:00:18 --:--:-- 19722
Downloading https://github.com/wayneeseguin/rvm/archive/stable.tar.gz
Creating group 'rvm'

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.

# Administrator,
#
#   Thank you for using RVM!
#   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.

In case of problems: http://rvm.io/help and https://twitter.com/rvm_io


STEP4 安装RVM环境


source /etc/profile.d/rvm.sh

STEP5 安装 1.9.3


rvm install 1.9.3

STEP6 新建新的gem配置


语法


rvm gemset create gemset_name    # gemset_name 是一个随便的名字
rvm ruby_version@gemset_name  # 指定ruby 版本和 gemset_name 名字

实际例子

rvm gemset create 9gem
rvm 1.9.3@9gem

至此就升级完毕了,gems也是RVM管理的gems不是yum安装的那个


终于学完了Ruby入门课程,正式成为一个Ruby小菜鸟了