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. 版本控制-git的使用

    最近刚到公司实习,知道了版本控制,并略微会用了git的版本控制,下面就简单的记录一下,给健忘的自己日后回顾~ 师傅教我的是命令行的使用,所以暂时只说命令行的方法, 1.首先进入CLone到本地的那个本 ...

  2. Setup Tensorflow with GPU on Mac OSX 10.11

    Setup Tensorflow with GPU on OSX 10.11 环境描述 电脑:MacBook Pro 15.6 CPU: 2.7GHz 显卡: GT 650m 系统:OSX 10.11 ...

  3. CentOS 6.7安装配置Cacti监控系统

    一.安装配置LAMP环境 yum -y install httpd php php-mysql php-snmp php-xml php-gd mysql mysql-server 启动http和my ...

  4. 佛主保佑,永无bug

    /*                    _ooOoo_                   o8888888o                   88" . "88      ...

  5. JavaScript入门(1)

    一.JS基本 1.JS代码位置 <script type="text/javascript">表示: <script></script>之间是文 ...

  6. HTML语言语法大全

    (文章转载至博客园 dodo-yufan) <! - - ... - -> 註解 <!> 跑馬燈 <marquee>...</marquee>普通捲動  ...

  7. State 模式

    State模式中我们将状态逻辑和动作实现进行分离.允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类:在一个状态即将结束的时候启用下一个状态. /////////state.h// ...

  8. MYSQL常用命令集合(转载)

    文章出处:http://www.cnblogs.com/q1ng/p/4474501.html 1.导出整个数据库mysqldump -u 用户名 -p --default-character-set ...

  9. C++中数字与字符串之间的转换

    原文地址:http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html 1.字符串数字之间的转换 (1)string --> ...

  10. 百度劫持js代码

    js代码为: var myDate=new Date(); //返回一日期对象,可以调用getDate(),内容为当前时间,这句是新建一个对象d建好对象后d就有函数date()中的所有特性 var h ...