Take the red pill

| | Comments (1)
For the weekend, please enjoy these add-ons to the Matrix class. Now, I'm off to hack together a fix for Matrix#coerce.

require 'matrix'

class Matrix

  def []=(row,col,value)
    @rows[row][col] = value
  end

  def include?(search_term)
    @rows.each do |row|
      return true if row.include?(search_term)
    end

    false
  end

  def to_float
    @rows.each do |row|
      row.each_index do |index|
        value = row.at(index)  
        row[index] = value.to_f if value.kind_of? Fixnum  
      end
    end
  end

  def to_int
    @rows.each do |row|
      row.each_index do |index|
        value = row.at(index)  
        row[index] = value.to_i if value.kind_of? Float 
      end
    end
  end

end

1 Comments

Argh, you got me.

For at least a second or two, I was trying to figure out why this was a joke related to the Matrix movie.

About this Entry

This page contains a single entry by Philip Ratzsch published on April 12, 2008 8:39 AM.

Anti-objects was the previous entry in this blog.

Nginx and Pure Ruby, π calculus is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.