贫道今天开始复习ruby了。。
那些配置环境的废话就不多说了 直接进入ruby语言的特性学习。
如何子弟工艺方法:
def sayGoodnight(name)
result = "Goodnight, #{name}"
return result
end
其中取传进来的参数用“#{参数名}” 就ok。
变量格式:
本地变量 可以直接是英文字母;---这种变量只能在类本身访问 默认是private的 想要他被外界访问 要变成类变量。
全局变量 以$开头。
对象 以@开头
类对象 以@@开头
数组与哈希表
ruby的数据可以包含多种数据类型,其实因为ruby中存在就是对象 所以它装的是对象就行了。
新建的时候可以直接初始化里面的数据
a = [ 1, 'cat', 3.14 ]
也可以新建一个空数组
empty1 = []
empty2 = Array.new
数组的元素还可以放数组 太tmd松散了。。似乎丢什么鬼东西进去都行。
也可以直接用字符来填充数组,不过需要转义 %W
a = %w{ ant bee cat dog elk }
哈希表
新建和初始化:每一个以逗号隔开
instSection = {
'cello' => 'string',
'clarinet' => 'woodwind',
'drum' => 'percussion',
'oboe' => 'woodwind',
'trumpet' => 'brass',
'violin' => 'string'
}
hashname = {
'key1' => 'value1' ,
'key2' => 'value132' ,
'key3' => 'value1' ,
'key4' => 'value14' ,
'key5' => 'value15'
}
访问:hashname['keyname'] 当所访问的key是空则会返回一个空nil回来
if-else 控制
传统的if else 如下:
if 条件表达式
业务
elseif
业务
else
业务
end
不要忘记‘end’结束标记。
ruby中还可以写成: do if 条件
while循环
while 条件1 and 条件2
业务
end
字符串操作
=~ 是一个查找字符串起始位置的操作。。
line =~ /Perl|Python/
查line字符串中 包含Perl或者python的开始位置。
替换操作
sub 替换第一个匹配的字符串。返回新的字符串
gsub 替换所有匹配的字符串。返回新的字符串
Block 块
在调用否个方法的时候你可以定义一个块 在每次这个方法执行完的时候运行块里面的内容 同时你可以把那个方法所返回的结果放到一个参数 然后再块中访问它。
def yamadie
yield
yield
yield
end
chicks = %w{cangjingkong xiaozeyuan jiji yy}
chicks.each {|name| puts name+' is crying ya ma die..'}
或者写成:
puts 'block'
def yamadie
yield
yield
yield
end
chicks = %w{cangjingkong xiaozeyuan jiji yy}
chicks.each do |name|
puts name+' is crying ya ma die..'
end
这里有java中常用的迭代器的思想
分享到:
相关推荐
the ruby way the ruby way
The Ruby Way(第2版) <br>The Ruby Way assumes that the reader is already familiar with the subject matter. Using many code samples it focuses on "how-to use Ruby" for specific applications, either ...
The Ruby Way 第三版(英文版),全书22章,书中包含600多个按主题分类的示例。每个示例都回答了“如何使用Ruby来完成”的问题。 ——Ruby on Rails之父David Heinemeier Hansson倾力推荐!
内含以下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
Are you frustrated with demanding languages that seem to get in your way, instead of getting the work done? If so, then we've got a language and book for you! Ruby is a fully object-oriented ...
express algorithms in a very natural way, and then it’s just a matter of typing ruby at the command line and pressing enter, and your Ruby script is running. However, Ruby’s syntax is deceptively ...
《The Ruby Way 2nd Edition》是一本深入探讨Ruby编程语言的经典著作,旨在帮助读者全面理解和掌握Ruby的精髓。这本书的第二版在2006年出版,由Addison-Wesley出版,作者通过深入浅出的方式,揭示了Ruby语言的强大...
With its simple commands, flags, and parameters, a well-formed command-line application is the quickest way to automate a backup, a build, or a deployment and simplify your life. With this book, you...
考阿路由器找到我的路 用于路由器... 旧版本: koa-router-find-my-way @ 3 => find-my-way @ 2 koa-router-find-my-way @ 2 => find-my-way @ 1基本用法const koaRouter = require ( 'koa-router-find-my-way' ) ;cons
MyWay的Wallpapers是您每天都在浏览主页和新标签的理想方式。 无论您的心情如何,都有免费的壁纸可以匹配它。 最重要的是,它是免费的。 无需注册。 立即加入我们的忠实用户,享受美丽,免费的动态壁纸。 现在点击...
《Ruby Way》是由Hal Fulton编写的关于Ruby编程语言的一本著作。这本书深入浅出地探讨了Ruby语言的各种特性,旨在帮助读者理解并掌握这门强大的动态脚本语言。Ruby以其简洁、优雅的语法和强大的元编程能力而备受赞誉...
《The Ruby Way 第二版》中文版采用“如何解决问题”的方式阐述Ruby编程,书中包含400多个按主题分类的示例。每个示例都回答了“如何使用Ruby来完成”的问题。首先对要完成的任务进行了描述,并讨论了技术方面的约束...
《The Ruby Way》是一本备受推崇的Ruby编程教程,它以独特的方式深入浅出地介绍了Ruby语言。这本书的核心理念是“如何解决问题”,作者通过实际的编程示例和问题解决策略,帮助读者理解Ruby的强大功能和优雅语法。 ...
You'll make sure slow code doesn't creep back into your Ruby application by writing performance tests, and you'll learn the right way to benchmark Ruby. And finally, you'll dive into the Ruby ...
You’ll make sure slow code doesn’t creep back into your Ruby application by writing performance tests, and you’ll learn the right way to benchmark Ruby. And finally, you’ll dive into the Ruby ...