pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Advertising: personal loans - Mortgages - secured loan - Loans
Ruby paste-bin - collaborative debugging tool View Help

Posted by Anonymous on Tue 23rd Oct 16:44 (modification of post by view diff)
View followups from Anonymous | download | new post

  1. class File
  2.   # This method could be implemented in two lines, but I'll spread it out so you can understand.
  3.   def File.to_matrix(filename)
  4.     file_data = File.read(filename) # First, we read the file in.
  5.     rows = file_data.split("\n") # Split all the lines up into rows.
  6.    
  7.     # collect is a method on an array that takes a block (which is like an inline function).
  8.     # It calls this block once for each element of the array and then returns an array of
  9.     # all the results.
  10.     rows.collect do |row|
  11.       columns = row.split(" ") # Splits each row's string into columns (separated by spaces)
  12.       columns.collect{ |column| column.to_f } # Convert each column from a string to a floating point value.
  13.       # Those columns, now converted into numbers, are returned.
  14.     end
  15.     # Now we're returning an array of arrays like [row][column].
  16.   end
  17. end
  18.  
  19. my_matrix = File.to_matrix("my_data.txt")
  20. puts my_matrix.inspect # Writes out the matrix in legible form.
  21. my_matrix[2][3] # Returns the value in the 2nd row and the 3rd column.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me
: