博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安装并运行Ruby
阅读量:6071 次
发布时间:2019-06-20

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

hot3.png

Ways of Installing Ruby

We have several tools on each major platform to install Ruby:

  • On Linux/UNIX, you can use the package management system of your distribution or third-party tools (rbenv and RVM).
  • On OS X machines, you can use third-party tools (rbenv and RVM).
  • On Windows machines, you can use RubyInstaller.

See the  page for details on using package management systems or third-party tools.

Of course, you can also install Ruby from source on all major platforms.

RVM (“Ruby Version Manager”)

 allows you to install and manage multiple installations of Ruby on your system. It can also manage different gemsets. It is available for OS X, Linux, or other UNIX-like operating systems. 

[root@localhost ~]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3gpg: requesting key D39DC0E3 from hkp server keys.gnupg.netgpg: key D39DC0E3: public key "Michal Papis (RVM signing) 
" importedgpg: no ultimately trusted keys foundgpg: Total number processed: 1gpg: imported: 1 (RSA: 1)[root@localhost ~]# \curl -sSL https://get.rvm.io | bash -s stableDownloading https://github.com/rvm/rvm/archive/1.27.0.tar.gzDownloading https://github.com/rvm/rvm/releases/download/1.27.0/1.27.0.tar.gz.ascgpg: Signature made Tue 29 Mar 2016 09:49:47 PM CST using RSA key ID BF04FF17gpg: Good signature from "Michal Papis (RVM signing)
"gpg: WARNING: This key is not certified with a trusted signature!gpg: There is no indication that the signature belongs to the owner.Primary key fingerprint: 409B 6B17 96C2 7546 2A17 0311 3804 BB82 D39D C0E3 Subkey fingerprint: 62C9 E5F4 DA30 0D94 AC36 166B E206 C29F BF04 FF17GPG verified '/usr/local/rvm/archives/rvm-1.27.0.tgz'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.# Hi,## 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: https://rvm.io/help and https://twitter.com/rvm_io[root@localhost ~]# source /etc/profile.d/rvm.sh[root@localhost ~]# rvm list known# MRI Rubies[ruby-]1.8.6[-p420][ruby-]1.8.7[-head] # security released on head[ruby-]1.9.1[-p431][ruby-]1.9.2[-p330][ruby-]1.9.3[-p551][ruby-]2.0.0[-p648][ruby-]2.1[.8][ruby-]2.2[.4][ruby-]2.3[.0][ruby-]2.2-headruby-head# for forks use: rvm install ruby-head-
--url https://github.com/github/ruby.git --branch 2.2# JRubyjruby-1.6[.8]jruby-1.7[.23]jruby[-9.0.5.0]jruby-head# Rubiniusrbx-1[.4.3]rbx-2.3[.0]rbx-2.4[.1]rbx[-2.5.8]rbx-head# Opalopal# Minimalistic ruby implementation - ISO 30170:2012mruby[-head]# Ruby Enterprise Editionree-1.8.6ree[-1.8.7][-2012.02]# GoRubygoruby# Topaztopaz# MagLevmaglev[-head]maglev-1.0.0# Mac OS X Snow Leopard Or Newermacruby-0.10macruby-0.11macruby[-0.12]macruby-nightlymacruby-head[root@localhost ~]# man rvm# IronRubyironruby[-1.1.3]ironruby-headRVM(1) [FIXME: manual] RVM(1)NAME rvm - The Ruby Version ManagerSYNOPSIS rvm [FLAGS] [OPTIONS] ACTION [IMPLEMENTATION[,IMPLEMENTATION[,...]]DESCRIPTION RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems.FLAGS --head with update, updates rvm to git head version. --rubygems with update, updates rubygems for selected ruby --default with ruby select, sets a default ruby for new shells. --debug Toggle debug mode on for very verbose output. --trace Toggle trace mode on to see EVERYTHING rvm is doing.[root@localhost ~]# rvm --help= rvm* https://rvm.io/* https://github.com/rvm/rvm/== DESCRIPTION:RVM is the Ruby enVironment Manager (rvm).It manages Ruby application environments and enables switching between them.== Installation curl -L https://get.rvm.io | bash -s stable --autolibs=enabled [--ruby] [--rails] [--trace]stable :: Install stable RVM, good for servers.--ruby :: Additionally install latest ruby version (MRI).--rails :: Install gem rails into default gemset (also installs ruby=).--trace :: Print trace of the installation, gist output when you have problems.--autolibs :: Enable or disable autolibs see: https://rvm.io/rvm/autolibsMore details here: https://rvm.io/rvm/install/== Usage rvm [Flags] [Options] Action [Implementation[,Implementation[,...]]== Flags--default :: with 'rvm use X', sets the default ruby for new shells to X.--debug :: Toggle debug mode on for very verbose output.--disable-binary :: Install from source instead of using binaries--trace :: Toggle trace mode on to see EVERYTHING rvm is doing.--force :: Force install, removes old install & source before install.--summary :: Used with rubydo to print out a summary of the commands run.--latest :: with gemset --dump skips version strings for latest gem.--gems :: with uninstall/remove removes gems with the interpreter.--docs :: with install, attempt to generate ri after installation.--reconfigure :: Force ./configure on install even if Makefile already exists.--skip-gemsets :: with install, skip the installation of default gemsets.--quiet-curl :: Makes curl silent when fetching data--64 :: Option for installing 64-bit Rubies (default behavior)--32 :: Option for installing 32-bit Rubies== Options-v|--version :: Emit rvm version loaded for current shell-l|--level :: patch level to use with rvm use / install --bin :: path for binaries to be placed (~/.rvm/bin/)-S :: Specify a script file to attempt to load and run (rubydo)-e :: Execute code from the command line.--gems :: Used to set the 'gems_flag', use with 'remove' to remove gems--archive :: Used to set the 'archive_flag', use with 'remove' to remove archive--patch :: With MRI Rubies you may specify one or more full paths to patches for multiple, specify comma separated: --patch /.../.../a.patch[%prefix],/.../.../.../b.patch 'prefix' is an optional argument, which will be bypassed to the '-p' argument of the 'patch' command. It is separated from patch file name with '%' symbol.-C|--configure :: custom configure options. If you need to pass several configure options then append them comma separated: -C --...,--...,--...--nice :: process niceness (for slow computers, default 0)--ree-options :: Options passed directly to ree's './installer' on the command line.--with-rubies :: Specifies a string for rvm to attempt to expand for set operations.== Action(Note: for most actions, 'rvm help action-name' may provide more information.)*usage* :: show this usage informationversion :: show the rvm version installed in rvm_pathuse :: setup current shell to use a specific ruby versionreload :: reload rvm source itself (useful after changing rvm source)implode :: (seppuku) removes the rvm installation completely. This means everything in $rvm_path (~/.rvm || /usr/local/rvm). This does not touch your profiles. However, this means that you must manually clean up your profiles and remove the lines which source RVM.get :: {head,stable} upgrades rvm to latest head or stable version. Check 'rvm help get' for more details. (If you experience bugs try this first with head version, then ask for help in #rvm on irc.freenode.net and hang around)reset :: remove current and stored default & system settings. (If you experience odd behavior try this second)info :: show the *current* environment information for current rubycurrent :: print the *current* ruby version and the name of any gemset being used.debug :: show info plus additional information for common issuesinstall :: install one or many ruby versions See also: https://rvm.io/rubies/installing/uninstall :: uninstall one or many ruby versions, leaves their sourcesremove :: uninstall one or many ruby versions and remove their sourcesreinstall :: reinstall ruby and runs gem pristine on all gems, make sure to read output, use 'all' for all rubies.migrate :: Lets you migrate all gemsets from one ruby to another.upgrade :: Lets you upgrade from one version of a ruby to another, including migrating your gemsets semi-automatically.wrapper :: generates a set of wrapper executables for a given ruby with the specified ruby and gemset combination. Used under the hood for passenger support and the like.cleanup :: Lets you remove stale source folders / archives and other miscellaneous data associated with rvm.repair :: Lets you repair parts of your environment e.g. wrappers, env files and similar files (e.g. general maintenance).fix-permissions :: Repairs broken permissions (e.g. by sudo or chef)osx-ssl-certs :: Helps update certificates for OpenSSL installed by rvm on OSX.snapshot :: Lets you backup / restore an rvm installation in a lightweight manner.alias :: Lets you set shortcut strings for convenience with 'rvm use'.disk-usage :: Tells you how much disk space rvm install is using.tools :: Provides general information about the ruby environment, primarily useful when scripting rvm.docs :: Tools to make installing ri and rdoc documentation easier.rvmrc :: Tools related to managing rvmrc trust and loading.patchset :: Tools related to managing ruby patchsets.do :: runs an arbitrary command against specified and/or all rubiescron :: Manages setup for using ruby in cron tasks.gemset :: gemsets: https://rvm.io/gemsets/rubygems :: Switches the installed version of rubygems for the current ruby.config-get :: display values for RbConfig::CONFIG variables.gemdir :: display the path to the current gem directory (GEM_HOME).fetch :: Performs an archive / src fetch only of the selected ruby.list :: show currently installed rubies, interactive output. https://rvm.io/rubies/list/autolibs :: Controls settings for automatically installing dependencies.pkg :: Install a dependency package {readline,iconv,zlib,openssl} https://rvm.io/packages/notes :: Display notes, with operating system specifics.export :: Temporarily set an environment variable in the current shell.unexport :: Undo changes made to the environment by 'rvm export'.requirements :: Installs additional OS specific dependencies/requirements for building various rubies. Usually run by install.mount :: Install rubies from external locations.user :: Tools for managing RVM mixed mode in multiuser installations.group :: Tools for managing groups in multiuser installations.== Implementation*ruby* :: MRI/YARV Ruby (The Gold Standard) {1.8.x, 1.9.x, 2.0.x, 2.1.x, 2.2.x}jruby :: JRuby, Ruby interpreter on the Java Virtual Machine.rbx :: Rubiniusree :: Ruby Enterprise Edition, MRI Ruby with several custom patches for performance, stability, and memory.macruby :: MacRuby, insanely fast, can make real apps (Mac OS X Only).maglev :: GemStone Ruby, awesome persistent ruby object store.ironruby :: IronRuby, NOT supported yet. Looking for volunteers to help.system :: use the system ruby (eg. pre-rvm state)default :: use the default ruby (or the system ruby if a default hasn't been set). https://rvm.io/rubies/default/== Resources:https://rvm.io/https://www.pivotaltracker.com/projects/26822== Contributions:Any and all contributions offered in any form, past present or future, to theRVM project are understood to be in complete agreement and acceptance with theApache License v2.0.== LICENSE:Copyright (c) 2009-2011 Wayne E. SeguinCopyright (c) 2011-2016 Michal PapisLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License. Action documentation available with 'rvm help action-name': alias autolibs cleanup config-get cron current debug disk-usage do docs fetch fix-permissions gemdir gemset gemset/copy get group implode info install list migrate mount notes osx-ssl-certs patchset pkg prepare reinstall remove repair requirements reset rubygems rvmrc rvmrc/to rvmrc/warning snapshot srcdir tools uninstall upgrade use user wrapper Other topics of documentation available with 'rvm help topic': alt color upgrade-notes For additional information please visit RVM's documentation website: https://rvm.io/ If you still cannot find an answer to your question, find 'wayneeseguin', 'mpapis', 'richo', 'lemoinem' or 'rys' in #rvm on irc.freenode.net: http://webchat.freenode.net/?channels=rvm[root@localhost ~]# rvm install 2.1.3Searching for binary rubies, this might take some time.Found remote file https://rvm_io.global.ssl.fastly.net/biChecking requirements for centos.Installing requirements for centos.Installing required packages: libyaml-devel, readline-devRequirements installation successful.ruby-2.1.3 - #configureruby-2.1.3 - #download % Total % Received % Xferd Average Speed Time Dload Upload Total 100 19.6M 100 19.6M 0 0 50213 0 0:06:50 0ruby-2.1.3 - #validate archiveruby-2.1.3 - #extractruby-2.1.3 - #validate binaryruby-2.1.3 - #setupruby-2.1.3 - #gemset created /usr/local/rvm/gems/ruby-2.1ruby-2.1.3 - #importing gemset /usr/local/rvm/gemsets/gloruby-2.1.3 - #generating global wrappers........ruby-2.1.3 - #gemset created /usr/local/rvm/gems/ruby-2.1ruby-2.1.3 - #importing gemsetfile /usr/local/rvm/gemsetsruby-2.1.3 - #generating default wrappers........

 我们采用rvm安装ruby的步骤如上面所示,然后,我们可以验证安装成功后的版本提示如下:

[root@localhost ~]# ruby -vruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]

除此之外,我们也可以使用源码编译进行安装,在此就不赘述了.

下面可以直接运行一下Ruby程序了,

可以直接输入ruby,

也可以输入irb来交互式的输入ruby命令,

也可以编写ruby源文件,然后运行它.

我们这里简单的编写了一个函数,注意ruby文件的后缀为*.rb

[root@localhost ~]# rubyputs "hello,world!"hello,world![root@localhost ~]# irb2.1.3 :001 > puts "hello,work"hello,work => nil 2.1.3 :002 > def sum(n1,n2)2.1.3 :003?>   n1+n22.1.3 :004?>   end => :sum 2.1.3 :005 > sum(12,12) => 24 [root@localhost ~]# vi demo.rbputs "hello,world"def hello(name)        puts "hello #{name}!"endhello("centos")[root@localhost ~]# ruby demo.rb hello,worldhello centos!

帮助文档的查阅可以使用ri命令,

[root@localhost ~]# riEnter the method name you want to look up.You can use tab to autocomplete.Enter a blank line to exit.>>

下面继续玩一下ruby吧:

[root@localhost ~]# irb2.1.3 :001 > "hello,world" => "hello,world" 2.1.3 :002 > "hello,world".object_id => 22256660 2.1.3 :003 > "hello,world".object_id => 22239000 2.1.3 :004 > "hello,world".object_id => 22230060 2.1.3 :005 > "hello,world".object_id => 22220540 2.1.3 :006 > 1.object_id => 3 2.1.3 :007 > 1.object_id => 3 2.1.3 :008 > 1.object_id => 3 2.1.3 :009 > true.object_id => 20 2.1.3 :010 > false.object_id => 0 2.1.3 :011 > false.object_id => 0 [root@localhost ~]# cat name.rb $prefix = "hello:"class Name	@@count = 0  def initialize(firstName, lastName)	@firstName = firstName	@lastName = lastName	@@count=@@count + 1  end  def self.get_name_count        @@count  end  def lastName        @lastName  end  def firstName        @firstName  end  def name_with_prefix        $prefix + fullName  end  def fullName        name = @firstName+" "+@lastName        name  endend[root@localhost ~]# irb2.1.3 :001 > load 'name.rb' => true 2.1.3 :002 > Name => Name 2.1.3 :003 > Name.new('a','b') => #
2.1.3 :004 > $prefix => "hello:" 2.1.3 :005 > ab = Name.new('a','b') => #
2.1.3 :006 > ab.firstName => "a" 2.1.3 :007 > ab.lastName => "b" 2.1.3 :008 > ab.name_with_prefix => "hello:a b" 2.1.3 :009 > Name.get_name_count => 2

需要注意Ruby是一种完全面向对象的编程语言.

转载于:https://my.oschina.net/donngchao/blog/783400

你可能感兴趣的文章
开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
查看>>
开源磁盘加密软件VeraCrypt教程
查看>>
本地vs云:大数据厮杀的最终幸存者会是谁?
查看>>
阿里云公共镜像、自定义镜像、共享镜像和镜像市场的区别 ...
查看>>
shadowtunnel v1.7 发布:新增上级负载均衡支持独立密码
查看>>
Java线程:什么是线程
查看>>
mysql5.7 创建一个超级管理员
查看>>
【框架整合】Maven-SpringMVC3.X+Spring3.X+MyBatis3-日志、JSON解析、表关联查询等均已配置好...
查看>>
要想成为高级Java程序员需要具备哪些知识呢?
查看>>
带着问题去学习--Nginx配置解析(一)
查看>>
onix-文件系统
查看>>
java.io.Serializable浅析
查看>>
我的友情链接
查看>>
多线程之线程池任务管理通用模板
查看>>
CSS3让长单词与URL地址自动换行——word-wrap属性
查看>>
CodeForces 580B Kefa and Company
查看>>
开发规范浅谈
查看>>
Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming
查看>>
鼠标增强软件StrokeIt使用方法
查看>>
本地连接linux虚拟机的方法
查看>>