From: Robert Klemme Date: 2004-10-06T18:19:55+09:00 Subject: Re: Upcase array "Thomas Leitner" schrieb im Newsbeitrag news:20041006111500.3dd9454c@localhost... > On Wed, 6 Oct 2004 17:41:50 +0900 > Fredrik Jagenheim wrote: > > | Given this array: > | foo = %w { FOO BAR baz biz } > | > | How do I make all the entries uppercase in a short and readable way? > | > | The (to me) obvious solution won't work: > | foo.map! { |x| x.upcase! } > | as upcase returns nil if it doesn't have anything to upcase... > | > | //F > | > | > > Do not use x.upcase! but x.upcase (without the exclamation mark): > > irb(main):005:0> foo = %w{ FOO BAR baz biz } > => ["FOO", "BAR", "baz", "biz"] > irb(main):006:0> foo.map! { |x| x.upcase } > => ["FOO", "BAR", "BAZ", "BIZ"] Even more efficient is the usage of #each and #upcase! >> foo = %w{ FOO BAR baz biz } => ["FOO", "BAR", "baz", "biz"] >> foo.each {|s| s.upcase!} => ["FOO", "BAR", "BAZ", "BIZ"] >> Here, no new instances are created. Regards robert