2016-03-19 47 views
0

我有一個元標記描述,我想要獲取授予名稱。然而,許多授權名稱都以「The Smithsonian Grant」爲開頭。我希望我的元標記可以說「在線申請Smithsonian Grant」而不是「Smithsonian Grant」。如果它是「The」,我將如何去除授予名稱的第一個單詞?使用切片從元標記描述中刪除「The」

我嘗試這樣做:

<% meta_description "Apply online to the #{@grant.name.slice("The")} on Instrumentl" %> 

但結果是

<meta name="description" content="Apply online to the The on Instrumentl" /> 

那是我沒怎麼期待切片工作。我也試過.slice!,.reduce和.except代替.slice,但沒有一個能夠工作。有任何想法嗎?

回答

1

我會使用gsub它將用替換字符串替換匹配字符串的任何部分。如果替換字符串爲空,它將完全刪除匹配的字符串:

>> "The Smithsonian Grant".gsub(/^the */i, "") 
=> "Smithsonian Grant" 

>> "Winnie the pooh".gsub(/^the */i, "") 
=> "Winnie the pooh"