2016-04-02 83 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