---
title: "计算字符串长度"
author: "nnnnon"
date: "2010-11-05T21:18:31Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-11-05T21:18:31Z"
id: 804242
url: "https://malijun.iteye.com/blog/804242"
---

计算含有中文字符串的长度（同时展示如何修改已有的类）



```ruby

class String
  def count
    i=0
    self.scan(/./u){|t| i += 1}
    i
  end
end

s="世界，你好！malijun"
puts s.count


```