我有一個字符串數組。這個數組中的一些字符串包含我正在查找的某個子字符串。我想獲得包含子字符串的字符串數組。Ruby從包含子串的數組中獲取字符串
我希望做這樣的:
a = ["abc", "def", "ghi"]
o.select(&:include?("c"))
但是,這給了我這個錯誤:
(repl):2: syntax error, unexpected ')', expecting end-of-input
o.select(&:include?("c"))
^
需要使用塊'o.select {| e | e.include?('c')}' –
'['abc','def','ghi']。grep(/ c /)'也可以。 – Stefan
您正在嘗試使用所謂的「椒鹽脆餅結腸」語法,但像@SebastiánPalma所說,它不接受參數。查看這兩個文檔,瞭解更多關於椒鹽脆餅結腸語法的內容:http://technology.customink.com/blog/2015/06/08/ruby-pretzels/和https://stackoverflow.com/questions/1961030/ruby-ampersand-冒號 - 快捷鍵 – anothermh