From: Massimiliano Mirra Date: 2002-01-04T23:22:52+09:00 Subject: [ruby-talk:30216] Re: chomp for arrays? On Fri, Jan 04, 2002 at 06:30:48PM +0900, Sean Chittenden wrote: > > I was wondering if there was a built-in for chomping an Array. I was > > going to write a little one liner to include in various places, but > > it seems like it'd be useful to have chomp be able to accept an > > array as well. Has there been any discussion on this or reasons not > > to? > > ar = ["foo\n", "bar\n"] > ar.each { |a| if a.type == String; then a.chomp!; end } class Array def chomp! each do |item| item.chomp! if item.type == String end end end Then put it in a file named `xarray.rb' located in your library search path, along with your other additions to the Array class and enough tests to explain what such eXtra Array methods do and to ensure that changes in xarray.rb don't break the programs that require it. At least that's how I do it. This is my xarray.rb: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ require 'xstring' class Array def rows2cols(index = nil, &block) if index and block_given? raise ArgumentError, "Only a block or an index must be given, not both." end if block_given? table = [] yield.length.times {table << []} each do |item| table.each_with_index do |col, i| col << yield(item)[i] end end return table elsif index col = [] each {|item| col << item[index]} return col end end def map2(*arrays, &block) 0.upto(size-1) do |i| yield [self, *arrays].collect {|array| array[i]} end end def collect2(*arrays, &block) (0..size-1).collect do |i| yield [self, *arrays].collect {|array| array[i]} end end def byteindex(item_index) item_index == 0 ? 0 : self[0..item_index-1].join.length end def hexdump(format = '%02x ') collect {|item| item.hexdump.chomp(" ")} # String#hexdump defined in xstring.rb end end if $0 == __FILE__ require 'Lapidary/TestCase' class TestArray < Lapidary::TestCase def testCollect2 a = [1,2,3] b = ['a','b','c'] c = [String, Numeric, Object] d = a.collect2(b) {|x,y| [x,y]} assert_equal [[1, 'a'], [2, 'b'], [3, 'c']], d d = a.collect2(b, c) {|x,y,z| [x,y,z]} assert_equal [[1, 'a', String], [2, 'b', Numeric], [3, 'c', Object]], d d = a.collect2(b, c) {|x,y,z| [z,x,y]} assert_equal [[String, 1, 'a'], [Numeric, 2, 'b'], [Object, 3, 'c']], d end def testRows2ColsRaisesIfIndexAndBlock a = [[1,'a',String], [2, 'b',Numeric], [3, 'c',Object]] assert_raises(ArgumentError) { b = a.rows2cols(1) {|x,y,z| [x,y,z]} } end def testRows2ColsIndex a = [[1,'a',String], [2, 'b',Numeric], [3, 'c',Object]] b = a.rows2cols(0) assert_equal [1,2,3], b b = a.rows2cols(1) assert_equal ['a','b','c'], b b = a.rows2cols(2) c = a.rows2cols {|x,y,z| [z]} assert_equal b, c.flatten end def testRows2ColsBlock a = [[1,'a',String], [2, 'b',Numeric], [3, 'c',Object]] b = a.rows2cols {|x,y,z| [x,y,z]} assert_equal [[1,2,3], ['a','b','c'], [String, Numeric, Object]], b b = a.rows2cols {|x,y,z| [x]} assert_equal [[1,2,3]], b b = a.rows2cols {|x,y,z| [x,z]} assert_equal [[1,2,3], [String, Numeric, Object]], b b = a.rows2cols {|x,y,z| [z,y,x]} assert_equal [[String, Numeric, Object], ['a','b','c'], [1,2,3]], b end def testByteIndex a = ['0123', '45', '6789'] assert_equal 0, a.byteindex(0) assert_equal 4, a.byteindex(1) assert_equal 6, a.byteindex(2) end end require 'Lapidary/UI/Console/TestRunner' Lapidary::UI::Console::TestRunner.run(TestArray) end ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ It would be nice to have a repository for people to share their x[object].rb's, whatever name they call them. :-) Massimiliano