Ruby中的类方法与实例方法

原文:[url]http://railstips.org/2009/5/11/class-and-instance-methods-in-ruby[/url]
-----------------------------------------------------
类方法在从一个类直接调用的,而实例方法是从一个类的实例来调用 的。这儿给一个简短的例子来说说具体一些情况。

class Foo
def self.bar
puts 'class method'
end

def baz
puts 'instance method'
end
end

Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class

Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ for #<Foo:0x1e820>


看来有什么不同了吗?bar是一个类的方法,因此可以直接在Foo类上正常调用。baz是一人实例方法,因此在Foo类上调用就引发了一个NoMethodError。在接下来的几代码中,我们都从Foo的一个实例(Foo.new)上调用。

现在我们有了一些基础,让我来展示一下创建类方法和实例方法的几种方式和它们的用途。

[size=medium][b]类方法(Class Methods)[/b][/size]

Ruby语言扩展性很强,因此定义一个类方法可以有多种方式。
以下是最为常见的几种方式:

# Way 1
class Foo
def self.bar
puts 'class method'
end
end

Foo.bar # "class method"

# Way 2
class Foo
class << self
def bar
puts 'class method'
end
end
end

Foo.bar # "class method"

# Way 3
class Foo; end
def Foo.bar
puts 'class method'
end

Foo.bar # "class method"


我最喜欢使用第一种方法。当我一看到self.method_name我就立即意识到那是一个类方法。许多人都用第二种方法,它在Rails中用得相当地多。

使用方式二 没有什么过错,但是当你在一个类中有一大堆的类方法在<< 块中,很难认出哪些是类方法,哪些是实例方法,因为它们都是def method_name形式。如果没有注意到这一点,你用使用这种方法,就会明白我所说的这一点。

方式三目前不很常见,它经常被快速地给一个类添加类方法。这里并不是只有这三种方式来创建类方法(Class Methods),但是它们是我见过最多的。
So when would you use a class method? Class methods are for anything that does not deal with an individual instance of a class. ActiveRecord::Base#find is one example. If you look in ActiveRecord::Base, you’ll see something like this:

module ActiveRecord
class Base
# some stuff
class << self
def find(...)
# blah
end
end
end
end


Looks familiar, eh? Some other uses of class methods in Rails are validations and associations in ActiveRecord and before/after/around filters in ActionPack. The way this works is something like this (simplified for clarity):

module ActiveRecord
class Base
def self.validates_presence_of(...)
# make sure present
end
end
end

class Foo < ActiveRecord::Base
validates_presence_of :bar
end


当你写上validates_presence_of,AR:Base中的这个方法就会被调用。

[size=medium][b]实例方法(Instance Methods)[/b][/size]
Enough about class methods, lets move on. Instance methods are a bit more simple. Here are a few common ways that instance methods are defined.

# 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.new.baz # "instance method"


关键的不同点是:实例方法只能来实例来调用,因此你必须来创建一个类的实例来调用它们(Foo.new)。另外,比起这个,有更多的方法来设义实例方法,如果你掌握元编程(meta programming)。

So what are some examples uses of instance methods in Rails, to give you a better idea? Ever do a find in a destroy action and then call destroy on the found instance? destroy is an instance method.

class FoosController < ActionController
def destroy
foo = Foo.find(params[:id])
foo.destroy
redirect_to foos_url
end
end

So are save and update_attributes, which you have definitely used before if you’ve done any Rails.

foo = Foo.new(:title => 'Bar')
foo.save # is an instance method


[size=medium][b]推论[/b][/size]
类方法(Class methods)只能被类来调用,而实例方法(instance methods)只能被类的实例来调用。理解这个很容易,但我刻我学Ruby的时候被这个搞迷糊了。希望写的这个有些帮助。如果你们有什么不懂的,问我好了。

接下来的一篇是关于Ruby中的Include和Extend,敬请期待。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值