zoukankan      html  css  js  c++  java
  • Ruby开发入门


    开发环境搭建

    首先安装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)MySQLmysql-connector-c-noinstall-6.0.2-win32lib" 
    --with-mysql-include="C:Program Files (x86)MySQLmysql-connector-c-noinstall-6.0.2-win32include" 
    --with-mysql-dir="C:Program Files (x86)MySQLmysql-connector-c-noinstall-6.0.2-win32"'

    这样就可以成功mysql2安装了!可是运行Rails在加载mysql2.so时还是会报Load Error错误,而且错误提示还是乱码!
    查了好多文章最后终于在一老外的博客找到了答案,还需要做最后一步:将Connector Clib下libmysql.dll拷贝到Rubyin下
    现在启动Rails应用,终于可以连接MySQL数据库了!

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

    1
    2
    3
    4
    5
    6
    7
    D:Ruby200in>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


  • 相关阅读:
    concurrent模块
    gevent模块
    Python中的线程
    2019.10.22 用TCP实现服务端并发接收
    Python中的进程
    进程和线程
    网络编程常用模块及方法
    [UOJ #222][NOI2016]区间(线段树)
    [BZOJ 4873][SHOI&SXOI2017]寿司餐厅(最大权闭合子图)
    [BZOJ 3751][NOIP2014]解方程(哈希)
  • 原文地址:https://www.cnblogs.com/xiaomaohai/p/6157705.html
Copyright © 2011-2022 走看看