2012-04-13 249 views
2

果然有超過一種轉換爲以下字符串兩種形式從左到右更多,反之亦然字符串操作

"content-management-systems" <=> "Content Management Systems" 

什麼是Ruby之道嗎?

+0

ERM對不起所有貢獻你改寫這個問題根本沒有意義 – 2012-04-13 12:41:51

+0

鑑於字符串「content-management-systems」,你如何獲得帶有Ruby的「Content Management Systems」字符串? 然後給「內容管理系統」,你如何獲得「內容管理系統」? – 2012-04-13 12:47:37

回答

5

這是棘手之一:

puts "content-management-systems".split("-").map(&:capitalize).join(" "). 
    tap{ |str| puts str}. 
    split.map(&:downcase).join("-") 

#=> Content Management Systems 
#=> content-management-systems 

簡化變體:

"content-management-systems".split("-").map(&:capitalize).join(" ") 
#=> Content Management Systems 

"Content Management Systems".split.map(&:downcase).join("-") 
#=> content-management-systems 

清潔變體(從邁克爾):

"content-management-systems".split("-").map(&:capitalize).join(" "). 
split(" ").map(&:downcase).join("-") 
+2

和完整的往返:''content-management-systems「.split(」 - 「)。map(&:capitalize).join(」「).split(」「).map(&:downcase)。加入(「 - 」)' – 2012-04-13 12:56:14

+0

哇!這太好了,你是石頭! – 2012-04-13 12:57:33

+0

@Micheal,你是對的,但你看不到結果 – megas 2012-04-13 12:58:15

2

gsub正則表達式匹配可以在塊被操縱模式。

"content-management-systems". 
    gsub(/(\w+)(-)?/) { ($2 ? $1 + " " : $1).capitalize! }. 
    gsub(/(\w+)(\s)?/) { ($2 ? $1 + "-" : $1).downcase! } 

,併爲這些基準測試顯示正則表達式和 noregexp版本之間沒有太大的區別。

require 'benchmark' 

STR = "content-management-systems".freeze 

Benchmark.bmbm(10) do |x| 
    x.report("noregexp") { 
    STR.split("-").map(&:capitalize).join(" "). 
    split(" ").map(&:downcase).join("-") 
    } 

    x.report("rgexp") { 
    STR. 
    gsub(/(\w+)(-)?/) { ($2 ? $1 + " " : $1).capitalize! }. 
    gsub(/(\w+)(\s)?/) { ($2 ? $1 + "-" : $1).downcase! } 
    } 
end 

__END__ 

Rehearsal ---------------------------------------------- 
noregexp  0.000000 0.000000 0.000000 ( 0.000032) 
rgexp  0.000000 0.000000 0.000000 ( 0.000035) 
------------------------------------- total: 0.000000sec 

       user  system  total  real 
noregexp  0.000000 0.000000 0.000000 ( 0.000051) 
rgexp  0.000000 0.000000 0.000000 ( 0.000058) 
+0

我欣賞正則表達式,...但看到我的答案... – 2012-04-13 13:26:52

+0

@ LucaG.Soave我知道,只添加顯示另一種方式。 – 2012-04-13 13:31:44

+0

是啊,正則表達式在這裏失蹤,謝謝。 – 2012-04-13 13:38:27

1

我張貼這只是要記住...... 正則表達式只需雙擊計算時間:

1.9.2p290 :014 > time = Benchmark.measure do 
1.9.2p290 :015 >  puts "content-management-systems".split("-").map(&:capitalize).join(" "). 
1.9.2p290 :016 >   tap{ |str| puts str}. 
1.9.2p290 :017 >   split.map(&:downcase).join("-") 
1.9.2p290 :018?> end 
Content Management Systems 
content-management-systems 
=> 0.000000 0.000000 0.000000 ( 0.000077) 

1.9.2p290 :019 > time = Benchmark.measure do 
1.9.2p290 :020 >  "content-management-systems".gsub(/(\w+)(-)?/) { ($2 ? $1 + " " : $1).capitalize! } 
1.9.2p290 :021?> "Content Management Systems".gsub(/(\w+)(\s)?/) { ($2 ? $1 + "-" : $1).downcase! } 
1.9.2p290 :022?> end 
=> 0.000000 0.000000 0.000000 ( 0.000164) 

,我想感謝:-)

+1

感謝您的數據!正則表達式從來沒有手動操作那麼快,但它很漂亮......如果你知道正則表達式,那麼它的編碼速度會更快 – texasbruce 2012-04-13 13:31:35

+0

@Luca G. Soave,在這種情況下,你不需要使用'tap {| str |把' – megas 2012-04-13 13:36:49

+0

@megas正確,在這種情況下甚至更快一點(0.000010) – 2012-04-13 13:50:15