I know that self
is the instance inside of an instance method. So, then, is self
the class inside of a class method? E.g., Will the following work in Rails?
我知道self是实例方法中的实例。那么,在类方法中是自我类吗?例如,以下是否会在Rails中工作?
class Post < ActiveRecord::Base
def self.cool_post
self.find_by_name("cool")
end
end
4 个解决方案
#1
21
That is correct. self
inside a class method is the class itself. (And also inside the class definition, such as the self
in def self.coolpost
.)
那是对的。类方法中的self是类本身。 (还有在类定义中,例如def self.coolpost中的self。)
You can easily test these tidbits with irb:
您可以使用irb轻松测试这些花絮:
class Foo
def self.bar
puts self.inspect
end
end
Foo.bar # => Foo
#2
5
class Test
def self.who_is_self
p self
end
end
Test.who_is_self
output:
输出:
Test
测试
Now if you want a Rails specific solution, it's called named_scopes:
现在,如果你想要一个Rails特定的解决方案,它叫做named_scopes:
class Post < ActiveRecord::Base
named_scope :cool, :conditions => { :name => 'cool' }
end
Used like this:
像这样使用:
Post.cool
#3
5
A lot of answers already, but here is why self is the class:
已经有很多答案,但这就是为什么自我是班级的原因:
The dot changes self
to whatever is before the dot. So, when you do foo.bar
then for the bar
-method, self
is foo
. There is no difference with class methods. When calling Post.cool_post
, you'll change self
to Post
.
点会将自身更改为点之前的任何内容。所以,当你执行foo.bar然后使用bar方法时,self就是foo。类方法没有区别。在调用Post.cool_post时,您将自己更改为Post。
The important thing to note here is that it's not how the method is defined that determines self
, but how it's called. This is why this works:
这里要注意的重要一点是,不是如何定义方法来决定自我,而是如何调用它。这就是为什么这有效:
class Foo
def self.bar
self
end
end
class Baz < Foo
end
Baz.bar # => Baz
Or this:
或这个:
module Foo
def bar
self
end
end
class Baz
extend Foo
end
Baz.bar # => Baz
#4
4
Short answer: yes
简短回答:是的
What I like to do with these questions is just fire up an irb or ./script/console session
我喜欢这些问题只是启动irb或./script/console会话
and then you can do the following to see the magic:
然后你可以做以下事情来看魔术:
ruby-1.8.7-p174 > class TestTest
ruby-1.8.7-p174 ?> def self.who_am_i
ruby-1.8.7-p174 ?> return self
ruby-1.8.7-p174 ?> end
ruby-1.8.7-p174 ?> end
=> nil
ruby-1.8.7-p174 > TestTest.who_am_i
=> TestTest
Happy fishing!
快乐钓鱼!
#1
21
That is correct. self
inside a class method is the class itself. (And also inside the class definition, such as the self
in def self.coolpost
.)
那是对的。类方法中的self是类本身。 (还有在类定义中,例如def self.coolpost中的self。)
You can easily test these tidbits with irb:
您可以使用irb轻松测试这些花絮:
class Foo
def self.bar
puts self.inspect
end
end
Foo.bar # => Foo
#2
5
class Test
def self.who_is_self
p self
end
end
Test.who_is_self
output:
输出:
Test
测试
Now if you want a Rails specific solution, it's called named_scopes:
现在,如果你想要一个Rails特定的解决方案,它叫做named_scopes:
class Post < ActiveRecord::Base
named_scope :cool, :conditions => { :name => 'cool' }
end
Used like this:
像这样使用:
Post.cool
#3
5
A lot of answers already, but here is why self is the class:
已经有很多答案,但这就是为什么自我是班级的原因:
The dot changes self
to whatever is before the dot. So, when you do foo.bar
then for the bar
-method, self
is foo
. There is no difference with class methods. When calling Post.cool_post
, you'll change self
to Post
.
点会将自身更改为点之前的任何内容。所以,当你执行foo.bar然后使用bar方法时,self就是foo。类方法没有区别。在调用Post.cool_post时,您将自己更改为Post。
The important thing to note here is that it's not how the method is defined that determines self
, but how it's called. This is why this works:
这里要注意的重要一点是,不是如何定义方法来决定自我,而是如何调用它。这就是为什么这有效:
class Foo
def self.bar
self
end
end
class Baz < Foo
end
Baz.bar # => Baz
Or this:
或这个:
module Foo
def bar
self
end
end
class Baz
extend Foo
end
Baz.bar # => Baz
#4
4
Short answer: yes
简短回答:是的
What I like to do with these questions is just fire up an irb or ./script/console session
我喜欢这些问题只是启动irb或./script/console会话
and then you can do the following to see the magic:
然后你可以做以下事情来看魔术:
ruby-1.8.7-p174 > class TestTest
ruby-1.8.7-p174 ?> def self.who_am_i
ruby-1.8.7-p174 ?> return self
ruby-1.8.7-p174 ?> end
ruby-1.8.7-p174 ?> end
=> nil
ruby-1.8.7-p174 > TestTest.who_am_i
=> TestTest
Happy fishing!
快乐钓鱼!