`
haiyang
  • 浏览: 70328 次
  • 性别: 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 代码
分享到:
评论

相关推荐

    Programming Ruby

    7. **Gem生态系统**:Ruby的Gem是其强大的包管理器,通过Gem可以方便地安装和管理第三方库,如Rails(Web开发框架)、RSpec(测试框架)等,极大地扩展了Ruby的功能。 8. **Ruby on Rails**:虽然《Programming ...

    Programming Ruby (English Version) and Source Code

    《Programming Ruby》是著名的Ruby语言教程,英文版的书籍旨在为全球开发者提供深入理解Ruby编程语言的途径。这本书详尽地介绍了Ruby的语法、特性、类库以及编程实践,是学习和进阶Ruby编程的宝贵资源。源代码的提供...

    Programming Ruby 2nd.pdf

    10. 新版的期望:第二版的《Programming Ruby》被期待能够延续第一版的辉煌,并进一步提升Ruby作为一门语言的影响力。随着Ruby语言的不断进化,新版书籍被视为将引领新一代Ruby开发者进入这个精彩纷呈的编程世界的...

    Programming Ruby 第二版

    ### Programming Ruby 第二版 #### 一、书籍概述与价值 《Programming Ruby 第二版》是一本关于 Ruby 编程语言的经典著作,被广大开发者誉为“Ruby 榔头书”。该书由几位知名的 Ruby 开发者撰写,并在国际范围内受...

    Programming Ruby 1.9 3rd edition

    - **第1章**:“开始使用Ruby”,从命令行环境入手,讲解了如何安装Ruby、运行Ruby程序以及如何使用Ruby文档工具如RDoc和ri等。 - **第2章**:“Ruby的新特性”,进一步探讨了Ruby作为一门面向对象的语言的特点,并...

    Programming Ruby The Pragmatic Programmer's Guide

    ##### 第7章:表达式 本章着重讲解了Ruby中的各种表达式,包括算术表达式、条件表达式等。同时,也会介绍一些特殊的表达式形式,比如三元运算符等。 ##### 第8章:异常处理 异常处理是任何程序设计语言中的重要部分...

    Programming Ruby 2nd Edition

    《Programming Ruby 2nd Edition》是关于Ruby编程语言的经典指南,尤其对于想要深入理解并熟练掌握Ruby编程的开发者来说,这本书具有极高的价值。Ruby是一种动态、面向对象的脚本语言,以其简洁、优雅的语法和强大的...

    Programming_ruby-2nd.pdf

    8. 对新一代Ruby语言的展望:第二版《Programming Ruby》被期待能够取代第一版,向世界展示Ruby语言的最新发展和成熟度。 9. 编程理念的启示:根据上述推荐和评价,可以理解到《Programming Ruby》不仅仅是关于语言...

    Programming.Ruby_第2版_中文版

    通过阅读《Programming Ruby》第二版中文版,开发者不仅可以学习到Ruby语言的各个方面,还能了解如何利用Ruby的灵活性和强大功能来构建高效、可维护的代码。这本书是Ruby初学者和有经验的程序员的宝贵资源,无论你是...

    Programming-Ruby-1.9.pdf

    ### Ruby编程语言基础知识点概述 #### 一、书籍简介与背景 《Programming Ruby 1.9》是由Dave Thomas...该书通过清晰的讲解和丰富的示例,帮助读者掌握Ruby编程的核心概念和技术,是学习Ruby不可或缺的经典教材之一。

    Pragmatic Programmers - Programming Ruby(2nd Edition Syngress).rar

    通过阅读《Programming Ruby》第二版,开发者不仅可以掌握Ruby语言的基本语法,还能了解到Ruby的精髓和背后的编程思想,提升自己的编程技巧和解决问题的能力。这本书适合初学者入门,也对有经验的开发者有着极高的...

    Ruby_Programming

    7. **社区活跃**: Ruby拥有一个庞大的开发者社区,持续不断地贡献新功能和改进。 #### 参考资料 - [Ruby官方文档](https://www.ruby-lang.org/en/documentation/) - [Ruby on Rails官方文档]...

    ruby-2.0.0-p0.tar

    对于初学者,了解这些基础知识是学习和使用Ruby的第一步。 学习Ruby语言,不仅需要掌握其语法和特性,还要熟悉它的开发工具如Gem(Ruby的包管理器)、Rake(构建工具)、RSpec(测试框架)等。Ruby社区活跃,有许多...

    Ruby语言教程大纲.zip

    通过阅读《Ruby编程语言》和《Programming Ruby》等经典书籍,结合实践项目,你可以深入理解并掌握Ruby的精髓。在学习过程中,不断地编写代码、解决问题和参与开源项目,将有助于提升你的Ruby编程技能。

    Ruby语言教程&案例&相关项目资源.docx

    - **标准库与第三方库**:Ruby拥有丰富的标准库和第三方库,提供了大量的模块和类,涵盖了文件处理、网络编程、多线程、图形界面开发等多个方面。 - **流行框架**:如Ruby on Rails(RoR),极大地简化了Web应用的...

Global site tag (gtag.js) - Google Analytics