Basic queries

Consider the following code:

class Drawing
def Drawing.give_me_a_circle <-------
  Circle.new
end

class Line
end

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?

--
Posted via http://www.ruby-forum.com/.

Hi,

John Lee wrote in post #1067695:

why must type "def Drawing.give_me_a_circle"?
why can't i type "def give_me_a_cirlce" instead?

Because the method is supposed to be a class method and not an instance
method.

If you type "def give_me_a_circle", you define a method for the
instances of Drawing:

···

#-------------------------------
class Drawing
  def give_me_a_circle
    puts "Here's the circle!"
  end
end
drawing_1 = Drawing.new
# call the method for an instance of Drawing:
drawing_1.give_me_a_circle
#-------------------------------

But what you actually want is to define a method for the class Drawing
itself. That's why you have to prepend "Drawing":

#-------------------------------
class Drawing
  def Drawing.give_me_a_circle
    puts "Here's the circle!"
  end
end
# call the method for Drawing:
Drawing.give_me_a_circle
#-------------------------------

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Fri, Jul 06, 2012 at 11:30:52PM +0900, John Lee wrote:

Consider the following code:

class Drawing
def Drawing.give_me_a_circle <-------
  Circle.new
end

class Line
end

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?

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

Dear Ruby programmers,

Well explained. But what good will it do to make a class method? Compare
to a instance method.

John

···

--
Posted via http://www.ruby-forum.com/.

John Lee wrote in post #1068173:

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

content = File.read 'C:/text.txt'

to

file = File.open 'C:/text.txt', 'r'
content = file.read
file.close

(Yeah, I know the block version. I'm using this as an extreme example.)

···

--
Posted via http://www.ruby-forum.com/\.

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:

  # ...
  c = Drawing.give_me_a_circle

So, the answer is "it depends".

···

On Tue, Jul 10, 2012 at 12:43 PM, John Lee <lists@ruby-forum.com> wrote:

Dear Ruby programmers,

Well explained. But what good will it do to make a class method? Compare
to a instance method.

--
Kendall Gifford
zettabyte@gmail.com

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:

    bob_the_blacksmith.likable? CharacterRecord.average_preferences

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.

···

On Wed, Jul 11, 2012 at 03:43:29AM +0900, John Lee wrote:

Dear Ruby programmers,

Well explained. But what good will it do to make a class method? Compare
to a instance method.

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

No you don't:

% ruby -e 'class X; @@x = 42; def x; p @@x; end; end; X.new.x'
42

···

On Jul 10, 2012, at 12:24 , Jan E. wrote:

First of all, you need class methods to interact with class variables.

He probably meant class INSTANCE variables.

Henry

···

On 11/07/2012, at 8:29 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

No you don't:

Henry Maddocks wrote in post #1068187:

He probably meant class INSTANCE variables.

Yes, sorry if I confused anyone.

···

--
Posted via http://www.ruby-forum.com/\.