2016-04-02 81 views
-2
wordSort.sort! 

=begin 
Conditional is performed on the array: if array index is equal to zero or has 
a remainder of zero after being divided by 2 then call upcase method on element. 
Else call lowercase method on that index number. 
=end 

puts "" 

puts "Here are the words you entered:" 

puts wordSort 
+0

是否要返回字符串或數組?如果是字符串,那麼對問題的描述包含隱含的假設,即需要將字符串轉換爲字數組,然後將數組元素重新加入字符串中。直接操作字符串更有意義。如果您希望返回一個數組,則可以在對大小寫進行更改之前或之後將該字符串拆分爲一個或多個數組。 –

+1

沒有任何證據的任何企圖的家庭作業問題。 –

+0

Ruby的慣例之一是使用「蛇情況」來表示變量和方法的名稱。這些名稱以小寫字母開頭,後跟小寫字母,數字和下劃線。例如,你會寫'word_sort'而不是'wordSort'。你不必接受這個約定,但是99%以上的Rubiests都可以。 –

回答

1
enum = [:upcase, :downcase].cycle 
    #=> #<Enumerator: [:upcase, :downcase]:cycle> 
"Here are the words you entered:".gsub(/\w+/) { |x| x.send enum.next } 
    #=> "HERE are THE words YOU entered:" 
+0

謝謝你的迴應。 – BlackArchMagusVidar