On Fri, Jul 06, 2012 at 11:30:52PM +0900, John L. wrote:
class Circle
def what_am_i
“This is a circle”
end
end
end
why must type “def Drawing.give_me_a_circle”?
why can’t i type “def give_me_a_cirlce” instead?
As already pointed out, “def Drawing.give_me_a_circle” defines a class
method, and not an instance method. You can also use “self” instead of
“Drawing” there:
def self.give_me_a_circle
That should have the same effect as this:
def Drawing.give_me_a_circle
. . . unless I’ve overlooked something. I’m pretty worn out for a
Friday
evening.
Well explained. But what good will it do to make a class method? Compare
to a instance method.
There are many reasons to use class methods.
First of all, you need class methods to interact with class variables.
Sometimes a method makes more sense if it’s associated with the class
itself rather than with the instances of the class. For example, a
method which escapes special characters in database queries would
probably be associated with the database class and not with a specific
database instance (representing a single connection).
Sometimes class methods are just for convencience. Compare
Well explained. But what good will it do to make a class method? Compare
to a instance method.
The answer to that question depends entirely on your (or your project’s)
needs.
Will code that uses the Drawing class be working with an instance most
of
the time? If so, your factory method perhaps should very well be an
instance method:
code that uses an instance of the Drawing class named @drawing
…
c = @drawing.give_me_a_circle
However, that may not make sense for your project or you may not have or
generally be working with an instance of the Drawing class. In this
case,
the current technique may make the most sense:
On Wed, Jul 11, 2012 at 03:43:29AM +0900, John L. wrote:
Dear Ruby programmers,
Well explained. But what good will it do to make a class method? Compare
to a instance method.
There are generally two reasons to have a class method, I think (though
I
may be missing something).
Technical Requirements
Sometimes, a method simply has to be a class method rather than an
instance method. For instance, let’s say you have a class that is meant
to represent characters in a computer RPG, and instances of it are
created to represent specific characters. Thus:
bob_the_blacksmith = CharacterRecord.new(hash_of_stats)
player_character = Player
The new method obviously won’t work as an instance method.
Conceptual Model
It makes more sense to make a given method a class method than to make
it
an instance method. In the same CRPG, the following situation applies:
In the latter case, you’re checking the preferences if perfectly
average,
default people in your CRPG indicate that bob_the_blacksmith is a
likable
guy by the standards of those preferences. Individual characters might
have different preferences, so a preferences instance method might make
more sense in many circumstances, but when you’re just checking averages
it does not really make sense to do so with an instance method because
those average preferences are not in any meaningful way related to
individual characters.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.