posts - 431,  comments - 344,  trackbacks - 0

模块定义:module
类定义:class
方法定义:def, undef
检查类型:defined?
条件语句:if, then, else, elsif, case, when, unless
循环语句:for, in, while, until, next, break, do, redo, retry, yield
逻辑判断:not, and, or
逻辑值和空值:true, false, nil
异常处理:rescue, ensure
对象引用:super, self
块的起始:begin/end
嵌入模块:BEGIN, END (require, include)
文件相关:__FILE__, __LINE__
方法返回:return
别名:alias

局部变量,方法参数和方法名称应该用一个小写字母开头或者用一个下划线开头;
全局变量用美元符作为前缀$;
实例变量使用@开头;
类变量用@@
类名,模块名和常量应该用大写字母开头

Ruby程序代码现在用7位的ACSII码来表示,通过语言扩展来支持EUC,SJIS或UTF-8等8位编码系统。Ruby2.0版本将支持16位的Unicode编码

puts print gets
printf "Number: %4.3f, String:%s", 7.8, "hi!"

数据类型:数字,字符串,数组,哈希表,区间,正则表达式
1..5  ---> 1,2,3,4,5;
1...5 ---> 1,2,3,4

a=1; b=1.0; a==b #true 比较两个对象的值是否相等
a=1; b=1.0; a.eql?(b) #false 比较两个对象的值以及类型是否相等
a=1.0; b=1.0; a.equal?(b) #false 比较两个对象在内存中的地址是否相同
a=1.0; b=a; a.equal?(b) #true
<=> 比较两个对象的大小,大于,等于,小于 分别返回1,0,-1
===右边的对象是否在左边区间之内,返回true和false
puts (0..9) === 3.14 #true
puts ('a'..'f')==='c' #true
=~用来比较是否符合一个正则表达式,返回模式在字符串中被匹配到的位置,否则返回nil
!~断言不符合一个正则表达式,返回true,false

break,跳出当层循环
next,忽略本次的循环的剩下部分,开始下一次的循环
redo,重新开始循环,还是从这一次开始
retry,重头开始这个循环体

times,upto,downto,each.step
3.times{print 'Hi'} #HiHiHi
1.upto(9) {|i| print i if i<7} #123456
9.downto(1){|i| print i if i<7} #654321
(1..9).each{|i| print i if i<7} #123456
0.step(11, 3){|i| print i} #0369

异常处理begin/end...rescue...ensure...raise  retry可以用在rescue中

attr_writer:motherland
相当于
def motherland=(value)
    return @motherland = value
end

attr_reader:motherland
相当于
def motherland
    return @motherland
end

attr_accessor:motherland 相当于 attr_reader:motherland; attr_writer:motherland

posted on 2008-04-09 22:05 周锐 阅读(196) 评论(0)  编辑  收藏 所属分类: Ruby and Rails

只有注册用户登录后才能发表评论。


网站导航: