2011-06-22 63 views
-2

我一直在試圖寫在Ruby中while循環類似於以下:未定義的方法 '+ @' - 紅寶石錯誤

number = 3 
string = "lalaLand" 
def main(number, string) 
    n = 1 
    while n <= number do 
     print string + 'something else' + n.to_s 
     n = n + 1 
    end 
end 
main(number, string) 

而且我不斷收到錯誤:

...: undefined method `[email protected]' for "lalaLand":String (NoMethodError) 

燦任何人都可以幫助我? 感謝

----編輯---(實際的代碼,也許有一些像我錯過)

devel_number被聲明爲INT唯一一個(即:devel_number = 4,其餘都是字符串:project_name = "lalaLand") ...

def b_sani_dev(project_home, source_dir, project_name, devel_number) 

    n = 1 
    while n <= devel_number do 
     system 'mkdir -p ' + project_home + 'dev_'+ n.to_s + '/main' 
     system 'mkdir -p ' + project_home + 'dev_' + n.to_s + '/LOG' 
     n = n + 1 
    end    
    b_sani_dev(project_home, source_dir, project_name, devel_number) 
+1

對我來說運行良好。 – User1578

+0

DEF b_sani_dev(PROJECT_HOME,source_dir,PROJECT_NAME,devel_number) n = 1的 而N <= devel_number做 系統 '的mkdir -p' + PROJECT_HOME + 'dev的+' + n.to_s + '/ M' 系統「的mkdir - p'+ project_home +'dev_'+ n.to_s +'/ L' n = n + 1 end – mhz

+0

hmm我的不好,將添加編輯真實代碼 – mhz

回答

0

所有你已經發布應該工作正常,但錯誤試圖使用一元+操作上的繩子,這是違法的莖的代碼。

+17 # totally fine 
+"hi" # throws an error; what does this even mean? 
+1

+運算符適用於Ruby中的字符串:) – mhz

+0

http://www.wellho.co.uk/solutions/ruby-string-functions-in-ruby.html – mhz

+2

是的,當添加兩個字符串時,'+'將起作用。你的錯誤信息來自試圖在一個字符串上使用「+」,這是沒有意義的。 –

相關問題