博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
有趣的Ruby-学习笔记7
阅读量:3555 次
发布时间:2019-05-20

本文共 3377 字,大约阅读时间需要 11 分钟。

RubyGems

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

安装

以下安装过程在cygwin上实现
首先访问 RubyGems 官网 https://rubygems.org/pages/download

下载RubyGems包

先下载 RubyGems的zip包
https://rubygems.org/pages/download

把包解压开运行 setup.rb

安装完成

切换使用淘宝源

由于众所周知的原因,我们必须使用淘宝源,否则装不了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 rubysudo yum install gcc g++ make automake autoconf curl-devel openssl-devel zlib-devel httpd-devel apr-devel apr-util-devel sqlite-develsudo yum install ruby-rdoc ruby-develyum install rubygems

用Ruby访问mysql

用rubyGems安装mysql的支持非常简单
gem install mysql
不过在安装前请确保你安装过mysql,如果没有请执行
yum install mysqlyum install mysql-devel
否则你无法从gem安装mysql

使用

先做一个最简单的例子来访问mysql并查询student表
#!/usr/bin/rubyrequire "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']    endrescue Mysql::Error => e    puts e.errno    puts e.errorensure    con.close if conend
解释
  • 在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  Speed100 20511  100 20511    0     0   1120      0  0:00:18  0:00:18 --:--:-- 19722Downloading https://github.com/wayneeseguin/rvm/archive/stable.tar.gzCreating 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 9gemrvm 1.9.3@9gem
至此就升级完毕了,gems也是RVM管理的gems不是yum安装的那个
终于学完了Ruby入门课程,正式成为一个Ruby小菜鸟了

转载地址:http://qcdrj.baihongyu.com/

你可能感兴趣的文章
【笔记】docker核心概念和使用 docker命令
查看>>
【学习笔记】spring cloud和微服务(一)介绍
查看>>
【笔记】python爬虫实战1 lxml模块 xPath语法 实例:爬取豆瓣网站
查看>>
【笔记】整合Druid数据源 Druid监控以及属性绑定配置 在浏览器输入端口进入监控页面查看
查看>>
【笔记】springboot+spring security登录流程实现
查看>>
【练习】2021下半年数据结构刷题笔记和总结 (一)(图和动态规划)
查看>>
【项目实战】vue+springboot项目使用富文本编辑器实现长文章发表和展示
查看>>
【项目】uniapp前端接收后端springboot数据并渲染到界面+使用swiper和uView
查看>>
【学习笔记】比较分别用prim和kruskal实现最小生成树和算法优化方案
查看>>
【记录】python多线程的使用 线程同步(LOCK和RLOCK) python与mysql数据库交互实现增加和查找 python的格式化输出
查看>>
【算法学习】动态规划之输出两个字符串最长公共子序列c++版
查看>>
【学习笔记】redis命令和使用(一)与key有关的命令
查看>>
【算法】深度优先搜索遍历的应用 设计算法以求解无向图G的连通分量的个数和无向图G的边数
查看>>
【算法】广度遍历算法的应用 求出距离顶点v0的最短路径长度为最长的一个顶点,图结构的bfs生成树及其双亲表示形式
查看>>
【项目】springboot中使用kaptcha生成验证码,登录时密码加盐处理
查看>>
【JAVA多线程学习笔记】(1)实现线程的方式 线程生命周期 操作线程的方法
查看>>
【java学习笔记-io流 文件读写和键盘读写】带缓存的输入/输出流和数据输入/输出流
查看>>
【Java作业】实验二 货物进销管理系统(运行成功完整代码
查看>>
【学习笔记】mongoDB初步(一)MongoDB的安装和增删改查基本语法,使用和命令
查看>>
【学习笔记】在windows下进行基于TCP的本地客户端和服务端socket通信
查看>>