Instance Method in Ruby

实例方法的定义

# Way 1
class Foo
  def baz
    puts 'instance method'
  end
end

Foo.new.baz # "instance method"

# Way 2
class Foo
  attr_accessor :baz
end

foo = Foo.new
foo.baz = 'instance method'
puts foo.baz

# Way 3
class Foo; end

foo = Foo.new
def foo.bar
  puts 'instance method'
end

foo.baz # "singleton method"

方法的定义

def object.method
end

object 可以是对象引用、常量类名、或self