2016-11-11 30 views
-1

您將如何在Ruby中解決這個問題。 「定義一個名爲yeller的方法,它接收一組字符並返回一個帶有ALLCAPS版本輸入的字符串,驗證yeller(['o','l','d'])是否返回」OLD「。 .join,.map,.upcase方法。「在Ruby中創建一個接受數組並返回一個字符串的方法

到目前爲止,我有:

def yeller(x) 
    x.map do |y| 
    y.upcase.join  
    puts y 
    end 
end 
yeller(['o', 'l', 'd']) 
+1

另一個說明,在紅寶石中,你也可以做'%w(o l d)'而不是'['o','l','d']''。 –

回答

-1

試試這個

def yeller(x) 
    imsupercool = x.map do |y| 
    y.upcase 
    end 
    imsupercool.join 
end 
puts yeller(['o', 'l', 'd']) 
+0

「定義一個名爲yeller的方法,該方法接受一組字符並返回一個字符串(...)」。這返回'nil'(因爲'puts'返回nil)。 – steenslag

+0

從方法定義中刪除'puts'並在調用函數之前使用'puts',即'puts yeller(['o','l','d'])' –

5

它是如此簡單

def yeller(x) 
    x.join.upcase 
end 

yeller(['o', 'l', 'd']) 
=> "OLD" 

join讓你的角色列表的字符串並upcase,使該字符串大寫

相關問題