用了这么久ruby,知道String对象可以通过[]操作得到字符或者子字符串,比如:
97就是字符a的ASCII码了,却不知道[]操作同样可以接受正则表达式,返回匹配正则的那部分字符串,比如:
>
"
abc
"
[
0
]
97
> " abc " [ 0 , 2 ]
" ab "
97
> " abc " [ 0 , 2 ]
" ab "
97就是字符a的ASCII码了,却不知道[]操作同样可以接受正则表达式,返回匹配正则的那部分字符串,比如:
>
"
has 5 and 3
"
[
/\
d
+/
]
5
> " hello there " [ / ( .. )e / ]
the
5
> " hello there " [ / ( .. )e / ]
the
ruby的API设计充分体现了马教主所说的人本接口。
文章转自庄周梦蝶 ,原文发布时间2007-7-6