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.

安装界面如下:

点击install就安装成功了。

安装结束后,运行ruby -v 显示版本号。如果正常显示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 $(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. [ruby]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. [转载]js中__proto__和prototype的区别和关系

          首先,要明确几个点:1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性_ ...

  2. Android ListView实现仿iPhone实现左滑删除按钮

    需要自定义ListView.这里就交FloatDelListView吧. 复写onTouchEvent方法.如下: @Override public boolean onTouchEvent(Moti ...

  3. SQL删除重复行和查询所有大于某成绩的语句分析

    有这样一个题,用一条SQL语句 查询出每门课都大于80分的学生姓名. 下面是表 分析,查询每门课程都大于80的学生.SELECT DISTINCT name FROM dbo.student WHER ...

  4. iis5.1/6.0/7.0+ 配置url重写 无扩展名伪静态

    原文链接:http://www.cnblogs.com/diose/archive/2013/02/21/2920324.html 最近在搞url重写 遇到iis 无扩展名及html映射问题 供后人查 ...

  5. [功能帮助类] 最新的Functions 类 (转载)

    代码 using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptogr ...

  6. Error parsing XML: not well-formed (invalid token)

    从网络上或别的文件复制粘贴进来的代码有隐含格式,可将内容先粘贴进记事本清除格式,再复制粘贴进工程文件,即可解决此问题 注:1. 要使工程文件全选清空, 2. 若粘贴后刷新仍无效果,可手动输入

  7. .NET中的消息队列

    下文参考:http://hi.baidu.com/21tian/blog/item/ce5464097ddf10cb3ac76335.html为何使用消息队列 您可能认为您能够通过一个简单的数据库表( ...

  8. js获取当前url参数

    //抓取url参数 function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theReque ...

  9. 分享red hat linux 6上安装oracle11g时遇到的gcc: error trying to exec 'cc1': execvp: No such file or directory的问题处理过程

    安装环境:Red Hat Linux 6.5_x64.oracle11g 64bit 报错详情: 安装到68%时弹窗报错: 调用makefile '/test/app/Administrators/p ...

  10. Android简单例子——IpHone样式AlertDialog

    此例子源于网络,下载下来之后,自己加了写注释,作为总结,发到博客中,谢谢原作者 通过这个例子学到的东西 1.自定义对话框的使用 2.程序中使用颜色如何进行存放,增加复用性 3.加深线性布局.常用控件的 ...