`
sarstime
  • 浏览: 20521 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

programming ruby - 1

 
阅读更多

1, Ruby has a shortcut: %w does what we need as below:

a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ]
a[0] ! "ant"
a[3] ! "dog"
# this is the same:
a = %w{ ant bee cat dog elk }
a[0] ! "ant"
a[3] ! "dog"

 

2, change hash default value:

A hash by default returns nil when indexed by a key it
doesn’t contain. Normally this is convenient, as nil means false when used in conditional
expressions. Sometimes you’llwant to change this default. For example, if you’re
using a hash to count the number of times each key occurs, it’s convenient to have the
default value be zero. This is easily done by specifying a default value when you create
a new, empty hash.
histogram = Hash.new(0)
histogram['key1'] ! 0
histogram['key1'] = histogram['key1'] + 1
histogram['key1'] ! 1

 

3, Code blocks

Code blocks are just chunks of code between braces or between do. . . end.
{ puts "Hello" } # this is a block
do ###
club.enroll(person) # and so is this
person.socialize #
end ###

 

4, singleton

We’ll arrange things so that the only way to create a logging object is to call MyLogger.create, and
we’ll ensure that only one logging object is ever created.
class MyLogger
private_class_method :new
@@logger = nil
def MyLogger.create
@@logger = new unless @@logger
@@logger
end
end
By making MyLogger’s new method private, we prevent anyone from creating a logging
object using the conventional constructor.

 

5, Index an array with a negative integer, and it counts from the end.

 

6, Index array

Finally, you can index arrays using ranges, in which start and end positions are separated
by two or three periods. The two-period form includes the end position, and the
three-period form does not.

 

7, about block

If the parameters to a block are existing local variables, those variables will be used as
the block parameters, and their values may be changed by the block’s execution.
The
same thing applies to variables inside the block: if they appear for the first time in the
block, they’re local to the block. If instead they first appeared outside the block, the
variables will be shared between the block and the surrounding environment.
In this (contrived) example, we see that the block inherits the variables a and b from
the surrounding scope, but c is local to the block (the method defined? returns nil if
its argument is not defined).
a = [1, 2]
b = 'cat'
a.each {|b| c = b * a[1] }
a -> [1, 2]
b -> 2 #it's been changed in block
defined?(c) ! nil

 

notes: Although extremely useful at times, this feature may lead to unexpected behavior and is hotly debated in the Ruby community. It is possible that Ruby 2.0 will change the way blocks inherit local variables.

分享到:
评论

相关推荐

    Programming Ruby - The Pragmatic Programmer's Guide, 2nd Edition (2005) [annotated]

    詹姆斯·布里特,ruby-doc.org网站管理员,表示第一版的《Programming Ruby》就是一本智慧、优雅且充满趣味的书,而第二版更是超越前者,成为Ruby学习者的首选资料。 ### 学习Ruby的意义 查德·福勒(Chad Fowler...

    Programming-Ruby-1.9源代码

    1. **Ruby语言基础** - 变量:Ruby支持局部变量、实例变量、类变量和全局变量,源代码中会展示它们的用法和作用域。 - 数据类型:包括字符串、数字、布尔值、数组、哈希等,通过代码你可以看到它们的操作和转换...

    Programming_ruby-2nd.pdf

    1. 书籍介绍:《Programming Ruby - 2nd Edition》是一本关于Ruby编程语言的英文书籍。该书被多位业界知名人士推荐,并对Ruby语言的介绍和学习提供了重要支持。 2. Ruby语言的特点:Ruby被描述为一个强大且实用的...

    Programming-Ruby-1.9.pdf

    《Programming Ruby 1.9》是由Dave Thomas、Chad Fowler以及Andy Hunt三位作者共同编写的关于Ruby编程语言的一本权威指南。这本书是对之前版本《Programming Ruby》的大幅修订版,并得到了原出版社Addison Wesley的...

    ruby-2.5.8.tar.gz

    - 解压`ruby-2.5.8.tar.gz`:使用`tar -zxvf ruby-2.5.8.tar.gz`命令解压。 - 编译与安装:进入解压后的目录,运行`./configure`,然后`make`和`make install`进行编译和安装。 - 验证安装:通过`ruby -v`检查...

    Programming Ruby中文版第二版[高清扫描版][带书签]和Programming.Ruby-2nd[高清文字版][带书签].pdf

    1. **Ruby语法**:Ruby的语法结构清晰,易于理解。它支持多种编程范式,包括面向对象、函数式和过程式编程。书中会详细介绍变量、常量、运算符、流程控制语句、方法定义等基础语法。 2. **面向对象编程**:Ruby是...

    Ruby学习资料(含参考手册和Programming Ruby)-中文.rar

    "Ruby语言入门教程附实例"和"ruby-mht"文件很可能是包含实例的教程,实践是学习编程的关键,通过这些实例,你可以亲手操作,从而巩固理论知识。 总的来说,这个压缩包提供了全面的Ruby学习资源,既有理论讲解,也有...

    ruby-1.9.3-

    With active user groups formed in the world’s major cities and Ruby-related conferences filled to capacity. Ruby-Talk, the primary mailing list for discussion of the Ruby language, climbed to an ...

    Programming ruby.pdf

    《Programming Ruby》是一本关于Ruby编程语言的经典著作,由Dave Thomas、Andy Hunt和Chad Fowler合著。这本书自2004年初版以来,一直是学习Ruby的首选资源,被誉为“Pickaxe”书,因其封面的图标而得名。Ruby是一种...

    Programming Ruby.pdf

    1. **动态类型**:Ruby是一种动态类型的编程语言,这意味着变量的类型可以在运行时改变,无需显式声明。 2. **元编程**:Ruby允许程序在运行时修改自身结构或行为,这种能力被称为元编程。通过元编程,可以实现高度...

    Ruby - Ruby 开发 - 常用知识点

    Ruby - Ruby 开发 - 常用知识点 backtracking、bit_manipulation、ciphers、conversions、data_structures、discrete_mathematics、dynamic_programming、electronics、maths

    Programming Ruby 1.9 (3rd edition)和源码

    1. **Ruby 1.9的关键改进**: - 字符串编码:Ruby 1.9引入了对多种字符编码的支持,使得处理多语言文本变得更加灵活。 - 更强的错误检测:在1.9版本中,语法错误和类型检查更加严格,减少了运行时错误。 - 全新的...

    Programming Ruby

    1. **Ruby语言概述**:Ruby由松本行弘(Yukihiro Matsumoto)创建,设计理念是结合Smalltalk的动态性、Perl的实用性以及Lisp的表达力。Ruby强调代码的可读性和简洁性,其语法清晰,易于理解。 2. **面向对象编程**...

    Programming Ruby (English Version) and Source Code

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

    ruby学习资源(Programming Ruby, Learning Ruby, The Ruby Way)

    内含以下4个文档: 1、Addison.Wesley.The.Ruby.Way.2nd.Edition.Oct.2006.chm 2、O'Reilly.Learning.Ruby.May.2007.chm 3、Programming Ruby 2e.pdf 4、ruby中文文档.chm

    《Programming Ruby》中文第2版源代碼下載

    《Programming Ruby》是一本经典的Ruby编程语言教程,中文第二版为中国的程序员提供了深入学习Ruby的宝贵资源。这本书的源代码下载对于读者来说是极其有用的,因为它允许读者在实践中探索和理解书中所阐述的概念。...

Global site tag (gtag.js) - Google Analytics