Ruby安装和简介

时间:2022-11-05 19:32:34

Ruby下载地址:https://www.ruby-lang.org/zh_cn/downloads/

我安装的是RubyInstaller.it is a self-contained Windows-based installer that includes the Ruby language, an execution environment, important documentation, and more.

安装界面如下:

Ruby安装和简介

点击install就安装成功了。

安装结束后,运行ruby -v 显示版本号。如果正常显示Ruby版本号,表示安装成功。

Ruby安装和简介

如果没有正常显示ruby的版本号,则自行添加系统变量

SET RUBY_HOME=D:/ruby
SET PATH=%PATH%;%RUBY_HOME%/bin
SET RUBYOPT=rubygems

把以上代码复制到记事本,另存为ruby.bat,然后执行文件即可。

Ide现在用notepad++。

notepad++ NppExec 扩展可以很方便运行各类程序。

按下快键 F6 ,将出现下面窗口(NppExec),在窗口中输入:

Ruby安装和简介
  ruby $(FULL_CURRENT_PATH)

点击ok,  啊哦,ruby: No such file or directory -- new (LoadError) 
  呵呵  你忘了保存(注:不要保存在有空格的目录下),save and ok again, 结果出来了。

ruby F:\ruby\rubySource\test1.rb
Process started >>>
hello
<<< Process finished. (Exit code 0)

更多参考:http://lemonzc.iteye.com/blog/150919

http://www.rubytips.org/2011/12/22/using-notepad-for-writing-ruby-programs-in-windows/

快速入门:

https://www.ruby-lang.org/zh_cn/documentation/quickstart/2/

irb(main):019:0> def h(name = "World")
irb(main):020:1> puts "Hello #{name.capitalize}!"
irb(main):021:1> end
=> nil
irb(main):022:0> h "chris"
Hello Chris!
=> nil
irb(main):023:0> h
Hello World!
=> nil

这里还有几个小窍门。第一是我们又一次省略了函数的括号。如果我们的命令看起来意图很明显的话, 函数的括号是可以省略的。另一个是函数缺省的参数值是World。意思就是说 “如果 name 参数没有给出的话, name 的缺省值就设置为“World"

Ruby是一门完全面向对象的语言。

irb(main):023:0> 4.class
=> Fixnum
irb(main):024:0> 4.methods
=> [:to_s, :inspect, :-@, :+, :-, :*, :/,
, :abs, :magnitude, :==, :===, :<=>, :>, :。。。

irb(main):025:0> false.class
=> FalseClass
irb(main):026:0> true.class
=> TrueClass
irb(main):027:0>

ruby中的true和false是一等对象(first-clas object)。(即与整数,浮点数等基本类型同等处理的对象,注意,这里说的对象并非特指面对对象语言中的对象,而是泛指编程语言中的类型,一等对象应具有几个性质:可存储变量或数据结构中;可作为参数传递给函数;可作为返回值从函数函数,可在运行时创建。举例来说:
c++对象就是一等对象。但其函数无法在运行时创建,所以不是一等对象;与之相反,函数式语言中的函数是一等对象,因为它既可以传递和返回,也可以在运行时动态创建)。

if:

x=4
puts "this appear to be true" if x==4
if x==4
puts "this appear to be true"
end

puts "this appeart to be false unless x==4

当使用if,unless时,可以使用block块形式(if condition,statements,end),也可以选用单行形式(statement if condition).

irb(main):041:0> puts "yes" if x
yes
=> nil
irb(main):042:0>

除了nil和false,其他值都代表true.即使是0.

Duck Typing
Let’s get into Ruby’s typing model a little. The first thing you need to
know is how much protection Ruby will give you when you make a
mistake with types. We’ re talking about type safety. Strongly typed lan-

guages check types for certain operations and check the types before
you can do any damage. This check can happen when you present the
code to an interpreter or a compiler or when you execute it. Check out
this code:
>> 4 + 'four'
TypeError: String can't be coerced into Fixnum
from (irb):51:in `+'
from (irb):51
>> 4.class
=> Fixnum
>> (4.0).class
=> Float
>> 4 + 4.0
=> 8.0
So, Ruby is strongly typed,3 meaning you’ll get an error when types
collide. Ruby makes these type checks at run time, not compile time.
I’m going to show you how to define a function a little before I normally
would to prove the point. The keyword def defines a function but doesn’t
execute it. Enter this code:

>> def add_them_up
>> 4 + 'four'
>> end
=> nil
>> add_them_up
TypeError: String can't be coerced into Fixnum
from (irb):56:in `+'
from (irb):56:in `add_them_up'
from (irb):58
So, Ruby does not do type checking until you actually try to execute
code. This concept is called dynamic typing. 只有真正运行时,才进行类型检查,这个叫做动态类型。

There are disadvantages:
you can’t catch as many errors as you can with static typing because
compilers and tools can catch more errors with a statically typed system.
But Ruby’s type system also has several potential advantages.
Your classes don’t have to inherit from the same parent to be used
in the same way:

=> 0
irb(main):044:0> a=["100",100.0]
=> ["100", 100.0]
irb(main):045:0> while i<2
irb(main):046:1> puts a[i].to_i
irb(main):047:1> i=i+1
irb(main):048:1> end
100
100
=> nil

You just saw duck typing in action. The first element of the array is
a String, and the second is a Float. The same code converts each to an
integer via to_i. Duck typing doesn’t care what the underlying type might
be. If it walks like a duck and quacks like a duck, it’s a duck. In this
case, the quack method is to_i.
Duck typing is extremely important when it comes to clean objectoriented
design. An important tenet of design philosophy is to code
to interfaces rather than implementations. If you’re using duck typing,
this philosophy is easy to support with very little added ceremony. If
an object has push and pop methods, you can treat it like a stack. If it
doesn’t, you can’t.

散列表hashes

>> numbers = {1 => 'one', 2 => 'two'}
=> {1=>"one", 2=>"two"}
>> numbers[1]
=> "one"
>> numbers[2]
=> "two"
>> stuff = {:array => [1, 2, 3], :string => 'Hi, mom!'}
=> {:array=>[1, 2, 3], :string=>"Hi, mom!"}
>> stuff[:string]
=> "Hi, mom!

最后那个散列表很有趣,因为我在其中首次引入了符号(symbol)。符号是前面带有冒号的标识符,类似于:symbol的形式,他在给事物和概念命名时非常好友,尽管两个同值字符串在物理上不同,但相同的符号却是同一物理对象。我们可以通过多次获取相同的符号对象标识符来证实这一点,类似下面:

irb(main):055:0> "string".object_id
=> 14967024
irb(main):056:0> "string".object_id
=> 18150060
irb(main):057:0> :string.object_id
=> 94184
irb(main):058:0> :string.object_id
=> 94184

hash有一些别处新裁的应用,比如:ruby虽然不支持named argument,但可以利用hash来模拟它,只要加进一颗小小的语法糖,他就能获得一些有趣的特性:

>> def tell_the_truth(options={})
>> if options[:profession] == :lawyer
>> 'it could be believed that this is almost certainly not false.'
>> else
>> true
>> end
>> end
=> nil
>> tell_the_truth
=> true
Report erratum
this copy is (P1.0 printing, October 2010)
Download from Wow! eBook <www.wowebook.com>
DAY 2: FLOATING DOWN FROM THE SKY 39
>> tell_the_truth :profession => :lawyer
=> "it could be believed that this is almost certainly not false."

hash用作函数最后一个参数时,{}可有可无。

Code Blocks and Yield 代码块和yield
A code block is a function without a name. You can pass it as a parameter
to a function or a method. For example:

代码块是没有命名的函数,可以传给fucntion或method作为参数。

irb(main):059:0> 3.times{puts "helo"}
helo
helo
helo
=> 3

大括号之间的代码就叫做代码块,times是Fixnum类的方法,可以采用
{/}和do/end 两种界定代码块的形式,ruby一般惯例是:代码块只占一行时用大括号,代码块占多行时用do/end,代码块可带有一个或多个参数:

>> animals = ['lions and ', 'tigers and', 'bears', 'oh my']
=> ["lions and ", "tigers and", "bears", "oh my"]
>> animals.each {|a| puts a}
lions and
tigers and
bears
oh my

上面这段代码能让你见识到代码块的威力,它指示Ruby在集合里的每个元素上执行某些行为,仅仅用上少许代码块语句,Ruby就遍历了每一个元素,还把他们全都打印出来。To

really get a feel for what’s going on, here’s a custom implementation of
the times method:

class Fixnum
def my_times
i=self
while i>0
i=i-1
yield
end
end
end
3.my_times{puts 'mangy moose'}

This code opens up an existing class and adds a method. In this case,
the method called my_times loops a set number of times, invoking the
code block with yield. Blocks can also be first-class parameters. Check
out this example:

>> def call_block(&block)
>> block.call
>> end
=> nil
>> def pass_block(&block)
>> call_block(&block)
>> end
=> nil
>> pass_block {puts 'Hello, block'}
Hello, block

This technique will let you pass around executable code. Blocks aren’t
just for iteration. In Ruby, you’ll use blocks to delay execution...

这技术能让你把可执行代码派发给其他方法。在Rbuy中,代码块不仅用于循环,还可用于延迟执行。

execute_at_noon { puts 'Beep beep... time to get up'}

(在Ruby中,参数名之前加一个“&”,表示将代码块作为闭包传递给函数。)

执行某些条件行为:

...some code...
in_case_of_emergency do
use_credit_card
panic
end
def in_case_of_emergency
yield if emergency?
end
...more code...
enforce policy...
within_a_transaction do
things_that
must_happen_together
end
def within_a_transaction
begin_transaction
yield
end_transaction
end

以及诸多其他用途,你会见到各种使用代码块的Ruby库,包括处理文件的每一行,执行HTTp事务中的任务,在集合上进行各种复杂操作,Ruby简直就是一个代码块的大联欢。

Ruby安装和简介的更多相关文章

  1. ruby 安装 运行

    Ruby基础 一 简介 1.Ruby在windows平台下的安装 (1)下载地址:http://rubyinstaller.org/downloads/ (2)安装过程 这里我们选择安装路径为 D:\ ...

  2. 【Ruby】ruby安装

    Ruby简介 Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人*(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License.它 ...

  3. Ruby安装Scss

    Ruby安装Scss 引言 已经许久不写HTML了,今天有点以前的东西要改.但是刚装的Windows10,已经没有以前的Web开发环境了.只好重新安装. 结果Webstorm装好后配置Scss出现错误 ...

  4. InfluxDB学习之InfluxDB的安装和简介

    最近用到了 InfluxDB,在此记录下学习过程,同时也希望能够帮助到其他学习的同学. 本文主要介绍InfluxDB的功能特点以及influxDB的安装过程.更多InfluxDB详细教程请看:Infl ...

  5. &lbrack;ruby&rsqb;Windows Ruby安装步骤

    Windows Ruby 安装步骤 准备工作: 1.http://rubyinstaller.org/downloads/ 下载选择Ruby 1.9.3 2.http://rubyinstaller. ...

  6. ruby安装方法

    安装 Ruby Ruby官网下载:http://www.ruby-lang.org/en/downloads/(官网下载链接) 安装过程中,得注意,勾选上添加到环境变量 安装完成后,查看是否安装成功 ...

  7. 前端开发环境之GRUNT自动WATCH压缩JS文件与编译SASS文件环境下Ruby安装sass常见错误分析

    前言: 1.sass编译为css文件,早先时刻写css,后来看了sass挺不错的,于是在新的项目中开始使用上了sass.(grunt需要ruby环境,所以需要先安装ruby,sass环境) ①安装ru ...

  8. 雷林鹏分享:Ruby 安装 - Windows

    Ruby 安装 - Windows 下面列出了在 Windows 机器上安装 Ruby 的步骤. 注意:在安装时,您可能有不同的可用版本. 下载最新版的 Ruby 压缩文件.请点击这里下载. 下载 R ...

  9. 雷林鹏分享:Ruby 安装 - Unix

    Ruby 安装 - Unix 下面列出了在 Unix 机器上安装 Ruby 的步骤. 注意:在安装之前,请确保您有 root 权限. 下载最新版的 Ruby 压缩文件.请点击这里下载. 下载 Ruby ...

随机推荐

  1. MVP解析

    一套可以直接复制使用的MVP框架 通过对MVP设计模式学习,对MVP也有了一个初步的认识,以登录Login模块为例,封装MVP如下: package com.example.administrator ...

  2. Maven exclusion

    <dependency><exclusions> <exclusion> <groupId>xx</group> <artifactI ...

  3. Django搭建及源码分析&lpar;一&rpar;

    一.关于Django以下两个站点,在使用方面有详细说明. http://www.nowamagic.net/academy/part/13/286 http://www.w3cschool.cc/dj ...

  4. XML巩固

    一.XML基础 1.XML区分大小写, 2.XML属性值必须有引号(单引双引均可) 3.XML必须有根元素 4.一些特殊字符的需要用实体引用来替换 < < 小于 > > 大于 ...

  5. PhpMyAdmin隐藏数据库设置同前缀失效的问题

    用PhpMyAdmin默认会把所有数据库都显示出来,一些如 MySQL,information_schema之类的也会显示,这样既不安全看着也不爽,隐藏掉最好. 修改 config.inc.php 或 ...

  6. 解析&colon;type t&lowbar;string is table of varchar2&lpar;32767&rpar; index by binary&lowbar;integer

    @ 理解一: table 相当于是数组,这里定义了一个数组类型t_string; INDEX BY BINARY_INTEGER这里是定义数组下标是整数,因为ORACLE中下标可以是字符串. VARC ...

  7. oracle 建表空间-&gt&semi;创建用户并把表空间分配给用户-&gt&semi;给用户授权-&gt&semi;导库

    首先注意:我参考网上使用的sysdba模式(normal)登陆的,其他的模式建不了用户(个人没有进行其他模式的表空间尝试,如有人尝试欢迎补充,感激不尽) 表空间相当于表的容器(一下所有的操作都适用于o ...

  8. Sublime使用及配置C编译器

    一.环境配置 在安装了MinGW+Gcc的基础上做如下设置—— 新建编译系统c.sublime-build: { "cmd" : ["gcc", "$ ...

  9. Linux系统编程——水平触发和边沿触发

    事件模型 EPOLL事件有两种模型: Edge Triggered (ET) 边缘触发只有数据到来才触发,不管缓存区中是否还有数据. Level Triggered (LT) 水平触发只要有数据都会触 ...

  10. python基础自学 第三天

    变量的命名 01.标识符和关键字 标识符 标识符就是程序员定义的变量名.函数名. 标识符可以由字母,下划线,和数字组成. 不能以数字开头 不能与关键字重名 关键字 就是在python内部已经使用的标识 ...