*怎么转16进制?
[code]
class Integer
def to_hex
sprintf "%x", self
end
end
16.to_hex #=> 10[/code]
*怎么scan出utf-8字符串中的中文字符?
[code]
requrie 'jcode'
$KCODE = 'u'
class String
def scan_chinese
self.scan /[\340\270\200-\351\276\237]/
end
end[/code]
*怎么在case when里匹配一个array?
[code]
colors = %w{red green blue}
case color
when *colors
...
[/code]
[code]
class Integer
def to_hex
sprintf "%x", self
end
end
16.to_hex #=> 10[/code]
*怎么scan出utf-8字符串中的中文字符?
[code]
requrie 'jcode'
$KCODE = 'u'
class String
def scan_chinese
self.scan /[\340\270\200-\351\276\237]/
end
end[/code]
*怎么在case when里匹配一个array?
[code]
colors = %w{red green blue}
case color
when *colors
...
[/code]
本文介绍了三种实用的Ruby编程技巧:如何将整数转换为16进制字符串,如何使用正则表达式扫描UTF-8字符串中的中文字符,以及如何在case when语句中匹配数组元素。
5万+

被折叠的 条评论
为什么被折叠?



