Ruby开发入门


开发环境搭建

首先安装Ruby SDK,我安装的版本是2.0。以后安装IDE,这里用的是Jetbrain的RubyMine 5.4.3,注意是否支持对应版本的Ruby SDK。



一段神奇的注册码...

如今最新版本是4.0.2, 使用这个key仍然有效, 对于这个没什么好说的, 若有必要,请支持购买正版。html

No.1:mysql

name: rubymine
License Key:git

70414-12042010
00002VG0BeoZbwmNAMNCx5E882rBEM
Ysn1P!e"s830EDlHcWg8gmqYVkvZMo
Injf4yqlO1yy"82NiwNzyYInoT7AiX
github

No.2:sql

username:EMBRACE
license key:
89330-12042010
00001p4HOxG8it!A4uOcpk1E"boEjk
v!tn2JZJC8Jw4hVlEmviJ0ge461sTw
owxcaVPQvd1gQzGxOpt2rElKQ3"R7w
数据库


用在最新版5.4.3上居然也能够!太神了吧!



开始Ruby之旅

在RubyMine Settings->Ruby SDK and Gems中配置好Ruby SDK后(RubyMine能自动找到安装好的SDK的位置),
就能够写咱们最熟悉的HelloWorld的Ruby版了。打开RubyMine后,新建一个空工程,在里面添加一个Ruby Class文件,输入内容以下:

1
2
3
4
5
6
7
8
9
10
11
class  Hello
 
   def  say
     str =  'hello world!!!'
     5 .times { puts str }
   end
 
end
 
hello = Hello. new ()
hello.say

执行效果就是在控制台输出五遍hello world!!!。



Rails入门

想要建立一个Rails工程,首先要经过Gems安装Rails包。新的Ruby SDK中都已包含了Gems管理器,因此不用再安装了。
在Settings->Ruby SDK and Gems下,点击Install Gems。



实际上这与直接执行SDK中的Gems批处理是同样的:

1
gem install rails --version "= 4.0.0" --no-ri --no-rdoc

若是网速太慢,能够直接手动下载Gem后安装。Rails的下载连接是 http://rubygems.org/gems/rails

1
gem install rails-4.0.0.gem --verbose --no-ri --no-rdoc



常见安装问题

1.为何Gems安装这么慢?

若是发现安装过程很慢,就用--verbose参数查看究竟是卡在了哪里,通常多是网络问题。能够经过sources命令管理源地址,
只保留一个淘宝的镜像地址。

1
2
3
4
5
6
7
8
#列举全部源
gem sources
 
#添加一个源
gem sources -a "http://ruby.taobao.org"
 
#删除一个源
gem sources -r "https://rubygems.org"

其余经常使用的源有:

1
2
3
4
http: //rubygems .org/  
http: //gems .github.com  
http: //gems .rubyforge.org  
http: //ruby .taobao.org

2.找不到依赖项

安装rails 4时报错:Unable to resolve dependencies: rails requires actionmailer (= 4.0.0)

1
2
#更新RubyGem管理器
gem update --system



2013年11月2日 六

1.能够直接安装RailsInstaller,节省下载Gems包的时间。

2.但安装Mysql Gems包时碰到了错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
ERROR:  Error installing mysql2:
         ERROR: Failed to build gem native extension.
 
         D:/RailsInstaller/Ruby1.9.3/bin/ruby.exe extconf.rb
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
 
Provided configuration options:
         --with-opt-dir
         --without-opt-dir
         --with-opt-include
         --without-opt-include=${opt-dir}/include
         --with-opt-lib
         --without-opt-lib=${opt-dir}/lib
         --with-make-prog
         --without-make-prog
         --srcdir=.
         --curdir
         --ruby=D:/RailsInstaller/Ruby1.9.3/bin/ruby
         --with-mysql-dir
         --without-mysql-dir
         --with-mysql-include
         --without-mysql-include=${mysql-dir}/include
         --with-mysql-lib
         --without-mysql-lib=${mysql-dir}/lib
extconf.rb:37:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError
)
 
 
Gem files will remain installed in D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9
.1/gems/mysql2-0.3.13 for inspection.
Results logged to D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.
3.13/ext/mysql2/gem_make.out

报错的缘由是Gems mysql2 0.3.13找不到依赖的libmysql库和头文件。因此首先确认是否安装了libmysql(即MySQL Connector/C),

即使安装了libmysql,可能gem命令在安装mysql2时也会找不到libmysql的库和头文件,因此执行gem install时直接经过参数指定:

1
2
3
4
gem install mysql2 --platform=ruby -- 
'--with-mysql-lib="C:\Program Files (x86)\MySQL\mysql-connector-c-noinstall-6.0.2-win32\lib" 
--with-mysql-include="C:\Program Files (x86)\MySQL\mysql-connector-c-noinstall-6.0.2-win32\include" 
--with-mysql-dir="C:\Program Files (x86)\MySQL\mysql-connector-c-noinstall-6.0.2-win32"'

这样就能够成功mysql2安装了!但是运行Rails在加载mysql2.so时仍是会报Load Error错误,并且错误提示仍是乱码!
查了好多文章最后终于在一老外的博客找到了答案,还须要作最后一步: 将Connector C\lib下libmysql.dll拷贝到Ruby\bin下
如今启动Rails应用,终于能够链接MySQL数据库了!

3.若是不使用RailsInstaller,那么用gem install时常常会报下面错误:

1
2
3
4
5
6
7
D:\Ruby200\bin>gem install rails --platform=ruby
ERROR:  Error installing rails:
         The 'atomic' native gem requires installed build tools.
 
Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instructions

这是由于缺乏DevKit,而RailsInstaller已经集成了。首先安装

1
2
3
4
5
6
7
8
> cd <DEVKIT_INSTALL_DIR>
> ruby dk.rb init
#生成config.yml,这里会检查将要添加DevKit支持的Ruby列表,只支持经过RubyInstaller安装的Ruby
#若是这里列出的Ruby与你的要求不符,能够手动修改
> ruby dk.rb review  #检查要添加DevKit支持的Ruby列表是否有误,能够略过
> ruby dk.rb install
[INFO] Updating convenience notice gem override for 'C:/Ruby192'
[INFO] Installing 'C:/Ruby192/lib/ruby/site_ruby/devkit.rb'

如今能够执行gem install xxx --platform=ruby --no-ri --no-rdoc
相关文章
相关标签/搜索