bxp2012的博客
bxp2012 · 共 2 篇文章
统计0到n的所有数字中1出现的次数
思路:先把所有的数字拼接成为一个大字符串,然后统计这个字符串中1出现的次数。 具体代码如下: require 'test/unit' class TestCountN < Test::Unit::TestCase def test assert_equal 6, count(13) end end def add_all_number_to_str n str = "" for i in 1..n str += i.to_s end str end def count n c = 0 add_all_number_to_str(n).each_char {|x| c=c+1 if(x.to_…
2013-04-23 09:19 · 评论(0)
ruby 中环绕别名的小例子
class MyClass def greet puts "Hello!" end def greet_with_log puts "Calling method..." greet_without_log puts "...method called" end alias_method :greet_without_log, :greet alias_method :greet, :greet_with_log end MyClass.new.greet MyClass.new.greet_without_log
2013-04-22 07:52 · ruby · 评论(0)