`
haiyang
  • 浏览: 69099 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

programming ruby 之第7,8章学习总结

阅读更多

1.基本上所有的东西在ruby里边都是表达式
One of the first differenceswith Ruby is that anything that can reasonably return a value does: just about everything is an expression.What does this mean in practice?
Some obvious things include the ability to chain statements together.
 

  1. handle = if song.artist == "Gillespie" then  
  2. "Dizzy"  
  3. elsif song.artist == "Parker" then  
  4. "Bird"  
  5. else  
  6. "unknown"  
  7. end  

You can have zero or more elsif clauses and an optional else clause.
if 是个表达式而不是一条语句,它返回值
9.If the statement they are modifying is a begin/end block, the code in the block will always execute at least one time, regardless of the value of the boolean expression.

print "Hello\n" while false
begin
print "Goodbye\n"
end while false
10. for in 和 each的区别:
for song in songlist
song.play
end

Ruby translates it into something like

songlist.each do |song|
song.play
end

11.在改变循环方向的时候:有:break, redo, and next
break -->break
next --> continue
redo 重新做循环:这个是比较新的 。

retry 这个是要从头开始做循环,比如现在从i=0已经到i=5了,那么现在retry就会从i=0开始。
12.关于作用域的问题:
The while, until, and for loops are built into the language and do not introduce new scope; previously existing locals can be used in the loop, and any new locals createdwill be available afterward.
但是块却不是很一样;

ruby 代码
  1. [ 1, 2, 3 ].each do |x|   
  2. y = x + 1   
  3. end  
  4. [ x, y ]  


produces:
prog.rb:4: undefined local variable or method `x' for
main:Object (NameError)

-----------------

  1. x = nil  
  2. y = nil  
  3. [ 1, 2, 3 ].each do |x|   
  4. y = x + 1   
  5. end  
  6. [ x, y ] ! [3, 4]  

外边定义好的在快里边可以用,但是块里边定义的变量在外边时是用不了的。

ruby 代码

ruby 代码
  1. a = b = c = 0 ! 0   
  2. , 1, 7, 0 ].sort.reverse ! [7, 3, 1, 0]  


if and case statements both return the value of the lastexpression executed.
2.在ruby当中很多操作符都是以方法形式实现的:
In Ruby, many operators are actually implemented as method calls.
3.In older Ruby versions, the result of the assignment was the value returned by the attribute-setting method. In Ruby 1.8, the value of the assignment is always the value of the parameter; the return value of the method is discarded.

ruby 代码
  1.  class Test   
  2. def val=(val)   
  3. @val = val   
  4. return 99   
  5. end  
  6. end  
  7. t = Test.new  
  8. a = t.val = 2   
  9. a ! 2   

4Parallel Assignment 平行赋值:如果左边的变量比右边的值多,那么多出的赋予nil,如果左边的少,那么平行赋值后右边的一部分被截断,如左边只有一个变量,右边有很多个参数的话,那么这么多的参数将会以Array的形式赋给左边的变量.

ruby 代码
  1. a,b =b,a    
  2. a,b = [1,2]   
  3. a,b = 1,2   
  4. b, (c, d), e = [1,2,3,4] ! b == 1, c == 2, d == nil, e == 3   
  5. b, (c,*d), e = 1,[2,3,4],5 ! b == 1, c == 2, d == [3, 4], e == 5   

5.布尔表达式
and和&&是等价的
or和||是等价的
唯一区别的就是优先级
6.关于操作符有一些是要记住的:
== Test for equal value.
<=> General comparison operator. Returns −1, 0, or +1, depending on whether its receiver is less than, equal to, or greater than its argument.
=~ Regular expression pattern match.
eql? True if the receiver and argument have both the same type and equal values. 1 == 1.0 returns true, but 1.eql?(1.0) is false.
equal?   true if the receiver and the argument have the same Object ID.
7.下边这段代码很nb:
hash = Hash.new
(hash[:key] ||= [])<<"hello"
puts hash[:key]
对于布尔表达式,如果||左边是nil,那么就会计算右边,然后会返回这个表达式的值。
8.if 的几种表达方式:

ruby 代码
  1. #with then   
  2. if song.artist == "Gillespie" then  
  3. handle = "Dizzy"  
  4. elsif song.artist == "Parker" then  
  5. handle = "Bird"  
  6. else  
  7. handle = "unknown"  
  8. end  

---------------------

ruby 代码
  1. #without then   
  2. if song.artist == "Gillespie"  
  3. handle = "Dizzy"  
  4. elsif song.artist == "Parker"  
  5. handle = "Bird"  
  6. else  
  7. handle = "unknown"  
  8. end  

-------------------------------

ruby 代码
  1. # in one line   
  2.  if song.artist == "Gillespie" then handle = "Dizzy"  
  3. elsif song.artist == "Parker" then handle = "Bird"  
  4. else handle = "unknown"  
  5. end  

--------------------------------

ruby 代码
  1. #You can get even terser and use a colon ( : ) in place of the then.   
  2. if song.artist == "Gillespie": handle = "Dizzy"  
  3. elsif song.artist == "Parker": handle = "Bird"  
  4. else handle = "unknown"  
  5. end  

-----------------------------

ruby 代码
分享到:
评论

相关推荐

    Ruby程序设计(简洁清新的教程)

    可能会出乎大家的意料,Ruby 并不是一种近年来才诞生的语言,它的历史可以追溯到 1993 年,Ruby之父Matz开始对脚本语言感兴趣。在通过一些分析和思考之后,Matz 认为脚本语言是可以变得很强大和灵活的,于是他准备把...

    JavaScript权威指南(第6版)(附源码)

    第7章 数组 7.1 创建数组 7.2 数组元素的读和写 7.3 稀疏数组 7.4 数组长度 7.5 数组元素的添加和删除 7.6 数组遍历 7.7 多维数组 7.8 数组方法 7.9 ECMAScript 5中的数组方法 7.10 数组类型 7.11 类数组对象 7.12 ...

    JavaScript权威指南(第6版)(中文版)

    第7章 数组 7.1 创建数组 7.2 数组元素的读和写 7.3 稀疏数组 7.4 数组长度 7.5 数组元素的添加和删除 7.6 数组遍历 7.7 多维数组 7.8 数组方法 7.9 ECMAScript 5中的数组方法 7.10 数组类型 7.11 类数组对象 7.12 ...

    JavaScript权威指南(第6版)

    第7章 数组 7.1 创建数组 7.2 数组元素的读和写 7.3 稀疏数组 7.4 数组长度 7.5 数组元素的添加和删除 7.6 数组遍历 7.7 多维数组 7.8 数组方法 7.9 ECMAScript 5中的数组方法 7.10 数组类型 7.11 类数组对象 7.12 ...

    JavaScript权威指南(第6版)中文文字版

    第7章 数组 144 7.1 创建数组 144 7.2 数组元素的读和写 145 7.3 稀疏数组 147 7.4 数组长度 148 7.5 数组元素的添加和删除 149 7.6 数组遍历 149 7.7 多维数组 151 7.8 数组方法 152 7.9 ecmascript 5中的数组方法 ...

    JavaScript权威指南(第六版) 清晰-完整

    第7章 数组 7.1 创建数组 7.2 数组元素的读和写 7.3 稀疏数组 7.4 数组长度 7.5 数组元素的添加和删除 7.6 数组遍历 7.7 多维数组 7.8 数组方法 7.9 ECMAScript 5中的数组方法 7.10 数组类型 7.11 类数组对象 7.12 ...

    JavaScript权威指南(第6版)

    第7章 数组 144 7.1 创建数组 144 7.2 数组元素的读和写 145 7.3 稀疏数组 147 7.4 数组长度 148 7.5 数组元素的添加和删除 149 7.6 数组遍历 149 7.7 多维数组 151 7.8 数组方法 152 7.9 ecmascript 5中的数组方法 ...

    编程新手真言......

    第7章 抽象之高级语法机制 149 7.1 真正的OO解 149 7.2真正的对象 151 7.3真正的继承 152 7.4真正的OO 152 7.5真正的OOP 154 7.6 真正的构造函数 155 7.7 真正的私有,保护和公有 156 7.8 真正的重载与复写 156 7.9 ...

    Maven权威指南 很精典的学习教程,比ANT更好用

    7. 多模块企业级项目 7.1. 简介 7.1.1. 下载本章样例 7.1.2. 多模块企业级项目 7.1.3. 本例中所用的技术 7.2. simple-parent项目 7.3. simple-model模块 7.4. simple-weather模块 7.5. simple-persist...

    network-security-7:IT Ladkrabang Openhouse 2013 网络安全竞赛

    2013 年 IT Ladkrabang Openhouse 网络安全排名第 7 网络安全是一项由以下规则管理的竞赛本次比赛是第七届,比赛重点是高中生应用他们的计算机系统安全知识,以发现所提供系统中的漏洞。 并了解如何保护您的计算机...

    dynamic-programming:动态编程练习

    P(1) = 1P(2) = 1for all n &gt; 2P(n) = P(P(n - 1)) + P(n - P(n - 1))给定数字n然后打印n项纽曼康韦序列例子: Input : 13Output : 1 1 2 2 3 4 4 4 5 6 7 7 8Input : 20Output : 1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 ...

    C++ 100本好书

    │ [Computer.Science.Programming.Basics.in.Ruby(2013.4)].Ophir.Frieder.文字版..epub │ [C安全编码标准].(美)塞克德.扫描版-.pdf │ [Dynamic.HTML权威指南].(Dynamic.HTML权威指南).Danny.Goodman.扫描版-.pdf...

Global site tag (gtag.js) - Google Analytics