开发环境搭建

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



一段神奇的注册码...

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

No.1:

name: rubymine

License Key:

70414-12042010

00002VG0BeoZbwmNAMNCx5E882rBEM

Ysn1P!e"s830EDlHcWg8gmqYVkvZMo

Injf4yqlO1yy"82NiwNzyYInoT7AiX

No.2:

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

Ruby开发入门的更多相关文章

  1. 《Ruby语言入门教程v1.0》学习笔记-01

    <Ruby语言入门教程v1.0> 编著:张开川 邮箱:kaichuan_zhang@126.com 想要学习ruby是因为公司的自动化测试使用到了ruby语言,但是公司关于ruby只给了一 ...

  2. scala程序开发入门

    scala程序开发入门,快速步入scala的门槛: 1.Scala的特性: A.纯粹面向对象(没有基本类型,只有对象类型).Scala的安装与JDK相同,只需要解压之后配置环境变量即可:B.Scala ...

  3. 安装ruby开发环境

    如何快速正确的安装 Ruby, Rails 运行环境 对于新入门的开发者,如何安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 ...

  4. [转帖]H5 手机 App 开发入门:技术篇

    H5 手机 App 开发入门:技术篇   http://www.ruanyifeng.com/blog/2019/12/mobile-app-technology-stack.html 阮一峰老师的文 ...

  5. Ruby小白入门笔记之<个人记录档>

    书写缘由 快两年的JAVA开发,因为来到一家新公司,产品需要用Ruby开发,故此才有了这从头开始,一入编程深似海啊...... 因为入门时是JAVA,所以理念跟规范早已形成,故此感觉突然采用Ruby编 ...

  6. openresty 前端开发入门五之Mysql篇

    openresty 前端开发入门五之Mysql篇 这章主要演示怎么通过lua连接mysql,并根据用户输入的name从mysql获取数据,并返回给用户 操作mysql主要用到了lua-resty-my ...

  7. java WEB开发入门

    WEB开发入门 1 进入web JAVASE:标准- standard   JAVA桌面程序 GUI    SOCKET JAVAEE:企业-浏览器控制  web 2 软件结构 C/S :client ...

  8. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen深入理解

    原文链接:Hello, Android Multiscreen_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android Multiscreen深入理解. 本 ...

  9. [译]:Xamarin.Android开发入门——Hello,Android深入理解

    返回索引目录 原文链接:Hello, Android_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android深入理解 本部分介绍利用Xamarin开发And ...

随机推荐

  1. hihocoder 1249(2015ACM/ICPC北京)

    题意: 给你一块正方形的土地,里面有矩形的草地,要求把土地分成两份,满足以下两个条件 1.两边的绿洲,左边>=右边,差值尽可能的小 2.在满足1的情况下分给左边的土地尽快能的多 而且绿洲不会出现 ...

  2. ubuntu16.04安装eclipse后启动栏图标为问号

    ubuntu创建eclipse快捷方式图标. cd /usr/share/applications sudo touch eclipse.desktop sudo gedit eclipse.desk ...

  3. mysql的连接处理过程

      在mysqld_main函数中经过一系列的初始化后,mysql开始监听客户端的连接 mysqld_socket_acceptor->connection_event_loop(); 查看my ...

  4. python基础(1)

    1.python中三元表达式 类比于C.C++.Java中都有的三目运算符,python中使用下面语句实现三元表达式 true_part if condition else false_part. c ...

  5. Ajax来实现下拉框省市区三级联动效果(服务端基于express)

    //服务端JS代码: //提供服务端的处理 const express = require('express'); const fs = require('fs'); const app = expr ...

  6. 位运算n & (n-1)的妙用

    本文转自:http://blog.csdn.net/zheng0518/article/details/8882394 按位与的知识 n&(n-1)作用:将n的二进制表示中的最低位为1的改为0 ...

  7. Python小代码_9_求水仙花数

    for i in range(100, 1000): ge = i % 10 shi = i // 10 % 10 bai = i // 100 if ge ** 3 + shi ** 3 + bai ...

  8. WPF ViewModel与多个View绑定后如何解决的问题

    当重复创建View并绑定同一个ViewModel后,ViewModel中的字段更新,在新的View中的没有反应或者在View中找不到相应的视觉树(如ListBox的ListBoxItem) 初始的解决 ...

  9. Laravel-admin 使用Layer相册功能

    使用Laravel-admin后台,Laravel-admin已经集成了很多前端组件,但是在手册中也没有发现能够展示相册的插件,而本人比较喜欢Layer弹窗的插件所以想使用Layer来进行效果展示 通 ...

  10. 解决Error: ENOENT: no such file or directory, scandir 'D:\IdeaWork\code-front-jet\node_modules\.npminstall\node-sass\3.7.0\node-sass\vendor'

    在使用npm安装node-sass的时候,可能会出现如下的报错: Error: ENOENT: no such file or directory, scandir 'D:\IdeaWork\code ...