In Ruby, inside a class method, is self the class or an instance?

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 ).

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.) You can easily test these tidbits with irb: class Foo def self.

Bar puts self. Inspect end end Foo. Bar # => Foo.

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: class Post { :name => 'cool' } end Used like this: Post.cool.

Short answer: yes What I like to do with these questions is just fire up an irb or . /script/console session 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!

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.

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 Baz Or this: module Foo def bar self end end class Baz extend Foo end Baz.

Bar # => Baz.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions