Ruby Matrix Output formatting

Source: Internet
Author: User

1Require'Matrix'2 3 classMatrix4     defto_pretty_s5s =""6i =07          whileI <self.column_size8s + ="\ n" ifI! =09j =0Ten              whileJ <self.row_size Ones + =' ' ifJ! =0 As + =Self.element (i, J). to_s -J + = 1 - End thei + = 1 - End - s - End +  -     defto_readable +Maximal_length = 15 AColumn_counter =0 atColumns_arrays = [] -          whileColumn_counter <self.column_size -Maximum_length =0 -Self.column (column_counter). Each do |column_element|#Get maximal size -Length =column_element.to_s.size -                 ifLength >Maximal_length inMaximum_length =length - End toEnd#Now we ' ve got the maximum size +Column_array = [] -Self.column (column_counter). Each do |column_element|#Add needed spaces to equalize each column theElement_string =column_element.to_s *Element_size =element_string.size $space_needed = Maximal_length-element_size +1Panax Notoginseng                 ifspace_needed >0 -Space_needed.times {Element_string.prepend" "} the                     ifColumn_counter = =0 +Element_string.prepend"[" A                     Else theElement_string.prepend"," + End - End $Column_array <<element_string $ End -Columns_arrays << Column_array#Now columns contains equal size strings -Column_counter + = 1 the End -Row_counter =0Wuyi          whileRow_counter <self.row_size theColumns_arrays.each do |column| -element =Column[row_counter] Wu                 PrintElement#Each column yield the correspondant row in order - End About             Print "]\n" $Row_counter + = 1 - End - End -  A     defMy_print +Matrix =self.to_a theField_size = matrix.flatten.collect{|i|I.to_s.size}.max -Matrix.each do |row| $Puts (row.collect{|i|' '* (field_size-i.to_s.size) + i.to_s}). Join ('  ')     the End the End the End the  -m = matrix[[12345678910, 333, 22.111], [3, 0.12345678, 3], [-333, 3, 4]] in  thePuts M#same as puts m.to_s the puts m.to_pretty_s About P m.to_pretty_s the m.to_readable the M.my_print the #matrix[[12345678910, 333, 22.111], [3, 0.12345678, 3], [ -333, 3, 4] + #12345678910 333 22.111 - #3 0.12345678 3 the #-333 3 4Bayi #"12345678910 333 22.111\n3 0.12345678 3\n-333 3 4" the #[12345678910, 333, 22.111] the #[3, 0.12345678, 3] - #[ -333, 3, 4] - #12345678910 333 22.111 the #3 0.12345678 3 the #-333 3 4

Ruby Matrix Output formatting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.