`
cookoo
  • 浏览: 651422 次
  • 性别: Icon_minigender_1
  • 来自: Shanghai
社区版块
存档分类
最新评论

Ruby小窍门3则

    博客分类:
  • Ruby
阅读更多
*怎么转16进制?
class Integer
  def to_hex
    sprintf "%x", self
  end
end

16.to_hex #=> 10



*怎么scan出utf-8字符串中的中文字符?
requrie 'jcode'
$KCODE = 'u'
class String
  def scan_chinese
     self.scan /[\340\270\200-\351\276\237]/
  end
end


*怎么在case when里匹配一个array?
colors = %w{red green blue}
case color
when *colors
 ...

分享到:
评论
4 楼 cookoo 2007-01-23  
呼呼,我又重新发明轮子了。
3 楼 zgd 2007-01-22  
13.to_s(16)
2 楼 cookoo 2006-10-30  
啊呀,to_i是败笔啊。谢谢指正
1 楼 qiezi 2006-10-30  
转16进制这个不大好吧,13这样一转就成了0了。

我觉得16进制只在作字符串显示时我们才说它是16进制,作为整数,16进制和10进制表示的数并没有什么分别,最好返回字符串。

相关推荐

Global site tag (gtag.js) - Google Analytics