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 代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值