Ruby Array Index - 其他每个单词都是upcase

时间:2016-04-02 21:52:48

标签: arrays ruby indexing upcase

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

1 个答案:

答案 0 :(得分: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:"