Reputation: 1701
Say I have the following code
class Monster
def self.yell
'i am yelling!'
end
def shout_something
Monster.yell
end
end
My yell
method is a class method while shout_something
is an instance method that calls yell
.
Is there anything inherently wrong with doing something like this? For example, is it bad to call a class method from an instance method? I ask because it feels wrong but maybe it's just because I'm a newbie to ruby.
On a side note, I thought doing self.yell
instead of Monster.yell
would make more sense but it doesn't work.
Upvotes: 1
Views: 114
Reputation: 434665
There's nothing particularly wrong about calling a class method from an instance method. Depending on your intent and how you want people to subclass your Monster, you might want to use this:
def shout_something
self.class.yell
end
So that subclasses can provide their yell
class method.
Upvotes: 4