Ruby学习笔记(二)
来源:互联网 发布:怎么注册阿里云邮箱 编辑:程序博客网 时间:2024/06/04 08:48
Ruby学习笔记
ruby使用方法
the first half of ruby
we are defining, we are creating
例如 blue_crystal=1 , leaf_tender=5
the second half of ruby
putting things in motion
例如 pipe.catch_a_star
总结:1.defining things 2.putting those things into action ruby的使用就是这么简单
补充:whenever you use a method,you will always be given something back,you can ignore it or use it
关于Nil,false和true
nil is without value,it is not zero.zero is a number
例如 plastic_cup=nil 意味着 plastic_cup is empty(这里的plastic_cup在ruby中不是未定义)
false 和 true 和一般的语言的类似,ruby中除了if 能判断外还有unless(在nil和flase时为真)
例如 print "Yes,plastic cup is up again!" if plastic_cup
print "hardly, it is down." unless plastic_cup
其他一些要点
double equals sign is a method
例如 approaching_guy.==(true)
triple equals is a length of velvet rope,checking values much like the double equals, but it is a longer rope and it sags a bit in the middle
例如
if 1894..1913===year "Born" ##这里可以看到有范围浮动,不一定是精确的相等
end
Ruby的类定义
require 'endertromb'
class WishMaker
def initialize
@energy = rand(6)
end
def grant(wish)
if wish.length>10 or wish.include? ''
raise ArgumentError, "Bad wish"
end
end
end
创建对象 todays_wishes=WishMaker.new
everything in Ruby is an object
这一点很强大
例如
number=5 print number.next 输出为6
print 5.class 会输出“Integer”表明5是整型类的对象
print WishMaker.new.class 会输出"WishMaker"
something about writing methods
1.do not be surprised if people pass unexpected objects into your methods. 对传入的错误值进行异常报错
2.it is poor etiquette to change objects your method is given. 使用dup (x=x.dup)进行复制备份
3.the square brackets can be used to lookup parts inside any Array,Hash or String objects.只有这些中提供了[]方法,同时也提供了[]=方法
4.watch for runaway loops. avoid while and until.
Ruby中的继承
class ToastyBear < Object; end 表明ToastyBear继承了Object这个类
在Ruby中,所有的类都是继承Object类,甚至class也是一个Object,可以说when you alter the object,you alter everything in Ruby.
关于Ruby中object 和 class 以及 module的区别
If Object is the king, the one who has sired every other part of Ruby, then Module is the poor waifish nun, shielding and protecting all her little Ruby townspeople children. (To complete the analogy: Class is the village school teacher and Kernel is the self-important colonel.)
最后插入一部电影,可见这本书的作者是多么牛叉啊
- Ruby学习笔记(二)
- Ruby学习笔记(二)
- Ruby学习笔记(二)
- ruby学习笔记二
- ruby 学习笔记(二)
- ruby基本概念(学习笔记二)
- Programming Ruby学习笔记二
- ruby学习笔记之数组学习(二)
- ruby学习笔记之数组学习(二)
- Ruby on rails 开发学习笔记(二)
- Memcached 学习笔记(二)——ruby调用
- 笔记之Ruby(二)
- Ruby on Rails学习笔记(二 Ruby连接mysql数据库)
- Ruby学习笔记二——数组
- Ruby on Rails 开发学习笔记 二
- Ruby元编程-学习笔记(二)-方法
- Ruby on Rails学习笔记(二 )
- Ruby元编程-学习笔记(二)-方法
- 正确实现 IDisposable接口
- iphone 自定义控件,通讯录,网络,电子书,控件特效 等源码
- c#中this和base的区别
- 数据库名,数据库实例名,数据库域名,操作系统环境变量
- C++ Vector 使用心得
- Ruby学习笔记(二)
- PHP SQL 注入攻击的技术实现以及预防办法
- android 2.2 wifi 框架
- 虚拟机入门
- 操纵线性表的几种方式
- PB8.0中,加超链接。
- uboot分析 (2009-02-06 15:37)
- Android 服务的生命周期
- Unix/Linux下C/C++开发技术概览-平台差异简介