ITeye博客

Ruby自学笔记3

coolbi · 2010-02-24 14:29 · 学习笔记 · 编程语言 · Markdown

9.正则表达式
正则表达式以/开始/结束
+:一个或多个
*:0个或多个
\s:一个空格(空格,TAB,换行等等)
\d:一个数字
\w:字母
.:任何字符
1)/Perl|Python/
2)/P(erl|ython)/
3)/ab*c/    /ab+c/

/\d\d:\d\d:\d\d/     # 时间例如 12:34:56
/Perl.*Python/       # Perl, zero or more other chars, then Python
/Perl\s+Python/      # Perl, one or more spaces, then Python
/Ruby (Perl|Python)/ # Ruby, a space, and either Perl or Python
10.继承和方法
ruby是单继承,但是可以使用Mixins来实现多继承。
语法:
class KaraokeSong < Song 
方法调用是先找子类。再找父类,直到继承连结束。如果都没有找到,会抛出异常。

11.属性
可以对实例变量创建类似JAVA的get和set方法
例如,类似set方法:
class Song
  attr_writer :duration
end
类似get方法:
class Song 
  attr_reader :name, :artist, :duration 
end

12.类变量(Class Variables)
what is Class Variables:为所有对象(实例)共享。类似JAVA的static变量

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。