ruby简要笔记

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {mso-style-priority:34; mso-style-unhide:no; mso-style-qformat:yes; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; text-indent:21.0pt; mso-char-indent-count:2.0; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} p.1, li.1, div.1 {mso-style-name:正文+1; mso-style-priority:99; mso-style-unhide:no; mso-style-next:正文; margin:0cm; margin-bottom:.0001pt; mso-pagination:none; mso-layout-grid-align:none; text-autospace:none; font-size:12.0pt; font-family:"Times New Roman","serif"; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast;} p.Default, li.Default, div.Default {mso-style-name:Default; mso-style-unhide:no; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:none; mso-layout-grid-align:none; text-autospace:none; font-size:12.0pt; font-family:"Times New Roman","serif"; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; color:black;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page WordSection1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.WordSection1 {page:WordSection1;} /* List Definitions */ @list l0 {mso-list-id:1110167826; mso-list-type:hybrid; mso-list-template-ids:179473029 -1 -1 -1 -1 -1 -1 -1 -1 -1;} @list l0:level1 {mso-level-text:""; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:0cm; text-indent:0cm;} @list l0:level2 {mso-level-start-at:0; mso-level-text:""; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:0cm; text-indent:0cm;} @list l0:level3 {mso-level-start-at:0; mso-level-text:""; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:0cm; text-indent:0cm;} @list l0:level4 {mso-level-start-at:0; mso-level-text:""; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:0cm; text-indent:0cm;} @list l0:level5 {mso-level-start-at:0; mso-level-text:""; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:0cm; text-indent:0cm;} @list l0:level6 {mso-level-start-at:0; mso-level-text:""; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:0cm; text-indent:0cm;} @list l0:level7 {mso-level-start-at:0; mso-level-text:""; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:0cm; text-indent:0cm;} @list l0:level8 {mso-level-start-at:0; mso-level-text:""; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:0cm; text-indent:0cm;} @list l0:level9 {mso-level-start-at:0; mso-level-text:""; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:0cm; text-indent:0cm;} @list l1 {mso-list-id:1340887031; mso-list-type:hybrid; mso-list-template-ids:-878828074 1820917000 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l1:level1 {mso-level-tab-stop:none; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

Ruby 语法

1.       注释 使用 # 注释单行,使用= begin =end 来注释多行

2.       连行使用 /

3.         分隔符, ; () , 使用与 java 相同, 使用类 . 方法,使用模块::常量

4.       关键字

模块定义: module

类定义: class

方法定义: def,undef

检查类型: defined?

条件语句: if,then,else

循环语句: for,in,while,until,next,break,do,redo,retry,yield

判断语句: not,and,or

逻辑值和空值: true,false,nil

异常 处理: super,self

块起始: begin/end

文件相关: _FILE_,_LINE_

方法返回: return

别名: alias

5 标识名和变量

局部变量、方法参数和方法名称应该用一个小写字母开头或者用一个下划线开头; 全局变量用美元符作为前缀 $ ;而实例变量用 @ 开头;类变量用 @@ 开头 ;类名、模块名和常量应该用大写字母开

Ruby 程序代码现在是用 7 ACSII 码来表示,通过语言扩展来支持 EUC, SJIS UTF-8 8 位编码系统。 Ruby 2.0 版本将支持 16 位的 Unicode 编码。

6 类库: 如果当前程序要用到类库中某个程序已经定义好的类、方法,就应该使用 require 或者 include 将类库程序名包含在当前程序中

 

7 数据类型:    Ruby 数据类型有数字,字符串,数组,哈希表,区间,正则表达式

Ruby 的数组和其它语言不同,数组的每个元素可以是不同的类型: [ 2.4 99 “thank you” [ a, b ,c ] 78 ]

8.Ruby 赋值

x = 0 #x=0

a,b,c = x, (x+1), (x+2) #a=0 ,b=1,c=2

9 ruby 条件判断:判断条件是否相等用 “==” ,注意不要写成 “=”

9 循环

break next & redo & retry

在循环体内,如果遇到: break ,跳出当层循环;

next ,忽略本次循环的剩余部分,开始下一次的循环 ;(类似java continue

redo ,重新开始循环,还是从这一次开始;

retry ,重头开始这个循环体

 

10 Java 中的 try…catch…finally…throw 相对应 , Ruby 中用 begin/end …rescue…ensure … raise 来处理异常, retry 可以用在 rescue 中。可以只用 rescue 或是 ensure ,两者都使用时, rescue 必须在 ensure

 

11 Ruby 是动态语言,你可以改变 Ruby 程序的结构,功能,在 Ruby 程序运行中。方法、属性可以被加入或去除,新的类或对象可以被建立,新的模块可以出现。 Ruby 语言灵活,因为 Ruby 是动态语言; Ruby 语言强大,因为 Ruby 是动态语言; Ruby 语言初学者容易犯错误,也因为 Ruby 是动态语言。

class Person

         def talk

                   puts "todsy"

         end

end

 

p1=Person.new

p1.talk

 

class Person

         def talk

           puts "today is #@date."

           end

           attr_accessor:date

end

 

p1.date="Sunday"

p1.talk

 

11 Ruby 语言,只有重写,没有其它语言具有的严格意义上的重载。 下面仔细分析 Ruby 为何没有重载,只有重写. Java 中,重载( overload )和重写( override )是用来表现多态性的两种重要方式。 override 也有译作 覆盖 覆写 ”.

原因 1 Ruby 支持缺省参数,例如可以调用sum(3),sum(3,6)

def sum( a, b=5 )

a+b

end

原因2 Ruby 还支持可变参数

def sum( *num )

numSum = 0

num.each { |i| numSum+=i }

return numSum

end

由于缺省参数和可变参数,参数个数不同而产生的重载,在 Ruby 中不再有效。 Ruby 语言中,定义方法时,不指定参数类型,因此第二种形式的重载也不存在。第三种形式的重载,实际是第二种形式的演化,所以,也就不存在了。综上所述, Ruby 语言,没有方法的重载。

 

12 单例方法: Ruby 里,可以给具体的实例对象添加实例方法,这个方法只属于这个实例对象,我们把这样的方法称之为单例方法。 单例方法也叫作单件方法。定义单例方法,首先要生成一个实例对象,其次,要在方法名前加上对象名和一个点号 “.”

 

13 模块: 定义模块用 module...end 。模块与类非常相似,但是:

A) 模块不可以有实例对象;

B) 模块不可以有子类。

 

14 mix -in

现实生活中,一个乒乓球不仅是球类物体,也是有弹性的物体。 C++ 支持多重继承,多重继承有时会导致继承关系的混乱, Java 只提供了单继承,通过接口可以得到多重继承的优点,又没有多重继承的缺点。 Ruby 也是单继承,不是通过接口,而是通过 Mix-in 模块,来实现多重继承的优点 .

通过 “ < 父类名 ,一个子类可以得到父类的属性和方法;通过 “ include 模块名 ,一个子类可以得到某个模块的常量和方法。类不能被 include.

include 方法为一个类的所有对象包含某个模块; extend 方法为一个类的某个对象包含某个模块

 

15 require load

使用 require 方法让你的程序文件变得简洁有力。 require 方法包含另一个文件,另一个文件名需要是一个字符串 . 还有一个 load 方法与 require 方法相对应,也用来包含另一个文件 .

require 包含文件,只加载一次,遇到同一文件时自动忽略;不同路径下的同名文件会多次加载。 load 包含文件,加载多次,即使是相同路径下同一文件

 

      require load 用于包含文件; include extend 则用于包含模块。

      require 加载文件一次, load 加载文件多次。

      require 加载文件时可以不加后缀名, load 加载文件时必须加后缀名。

      require 一般情况下用于加载库文件,而 load 用于加载配置文件。

16 元编程

子类调用方法时,如果是已有的方法,则正常调用;如果是不存在的方法,就由 E9-1.rb 中的超类自动生成;如果你觉得方法不合你意,你能够很轻易地修改方法体,这个工作也由 E9-1.rb 中的超类自动完成。 E9-1.rb 中的超类是元编程的一个简单展现。你加入自己修改后的代码,能够使这个程序更加智能化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值