yaotti's diary

Software is Eating the World

メソッド定義

ふと,こんなことできないのかなーと思ったのだけれど,

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
def Integer.plus(n)
  return Integer.value + n
end

p 1.plus(10) #=>11が欲しい

なんかできそうだよなぁ.すべてがオブジェクトなrubyなら.


追記
あっさりできた.

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
class Integer
  def plus(n)
    return self+n
  end
end

p 1.plus(10)