From: Bertram Scharpf Date: 2006-03-15T06:04:46+09:00 Subject: Re: Proposal: String#notempty? Am Dienstag, 14. M�r 2006, 12:02:45 +0900 schrieb nobu@ruby-lang.org: > At Tue, 14 Mar 2006 04:37:43 +0900, > Bertram Scharpf wrote in [ruby-core:07536]: > > > unless string.empty? > > > > How do you make the unless modifier return a string? > > string[/.+/mn] || "default" puts RUBY_VERSION require "benchmark" class String ; def ne? ; empty? ? nil : self ; end ; end a = "stuff ".split nil, -1 a.each { |s| Benchmark.bm { |x| x.report("c") { 1_000_000.times { s.nonempty? || "default" } } x.report("d") { 1_000_000.times { s.ne? || "default" } } x.report("a") { 1_000_000.times { s.any? ? s : "default" } } x.report("e") { 1_000_000.times { s != "" ? s : "default" } } x.report("n") { 1_000_000.times { s[/.+/mn] || "default" } } } } ---- 1.9.0 user system total real c 0.480000 0.000000 0.480000 ( 0.477972) d 0.870000 0.000000 0.870000 ( 0.871681) a 2.630000 0.000000 2.630000 ( 2.625230) e 1.280000 0.000000 1.280000 ( 1.278209) n 2.220000 0.000000 2.220000 ( 2.227200) user system total real c 0.860000 0.000000 0.860000 ( 0.851721) d 1.260000 0.000000 1.260000 ( 1.264583) a 1.680000 0.000000 1.680000 ( 1.674639) e 1.020000 0.000000 1.020000 ( 1.021995) n 1.420000 0.000000 1.420000 ( 1.420242) ---- class String def complexity scan(/\w+|./).uniq.length end end puts <