StandardClassExtensions for Ruby

Tags:

StandardClassExtensions

루비 standard library의 확장들입니다. 예를들어, standard library중 하나인 matrix는 immutable하죠. 즉,

a = Matrix[[1,2], [3,4]]
a[0,0]    # => 1
a[0, 0]   # => Error. Because a matrix is immutable.

하지만 standard class extensions을 사용하면

class Matrix
  #
  # Sets element (+i+,+j+) of the matrix to +value+.  That is: row +i+, column +j+.
  #
  def []=(i, j, value)
    @rows[i][j] = value
  end
end

와 같은 코드를 사용해 Matrix를 확장해 Mutable 하게 쓸 수 있습니다. (그나저나 대체 왜 기본 matrix는 immutable한겁니까….)