2017-08-16 110 views
-3
x = ["1", "2", "3", "4"] 
#=> ["1", "2", "3", "4"] 

x.split(", ") 
#=> NoMethodError: undefined method `split' for ["1", "2", "3", "4"]:Array 
+7

因爲它不。你的意思是使用'join(',')'? –

+0

x是一個數組。數組不需要分裂。 –

+2

您的預期成果是什麼?什麼應該'[「1」,「2」,「3」,「4」]。split(「,」)'return? – Stefan

回答

0

數組不能拆分。你可能想分割一個字符串?

對於0索引(數組中的第一個對象),您只需通過x [0]即可達到所需的值。

這對任何幫助嗎?

2

在紅寶石的String#split方法用於劃分字符串成子

'a,b,c,d'.split(',') # => ["a", "b", "c", "d"] 

您正在試圖調用Array#split(又名上的陣列對象)。由於這種陣列方法不存在,您將得到:

error undefined method split for ["1", "2", "3", "4"]:Array` 
相關問題