11 Jul 2008, 02:40
Generic-user-small

Bharat Ruparel (146 posts)

Hello David, You have this nice example of recommended use of class « self as follows:

class Dave
  @count = 0
  class << self
    attr_accessor :count
  end
  def initialize()
    Dave.count += 1
  end
end

puts "There are #{Dave.count} instances of Dave."
d1 = Dave.new
d2 = Dave.new
puts "There are #{Dave.count} instances of Dave."

Now following the logic that you explained before this to turn this into self.method syntax, I did the following:

class Dave
  @count = 0
  self.attr_accessor :count
  def initialize()
    Dave.count += 1
  end
end

puts "There are #{Dave.count} instances of Dave."
d1 = Dave.new
d2 = Dave.new
puts "There are #{Dave.count} instances of Dave."

But when I run this, I get the following error message:

main.rb:3: private method `attr_accessor’ called for Dave:Class (NoMethodError)

I am not sure if I understand what is going on here. Why can’t I use the parallel but clearer syntax that you explained before this.

By the way, brilliant example of include and extend in ActiveRecord setting. You are a really good teacher. Bharat

11 Jul 2008, 04:09
Dave_gnome_head_isolated_pragsmall

Dave Thomas (338 posts)

Two problems:

First, attr_accessor is a provate method, and so can’t be called with a receiver.

Second, ‘self’ is the current class, so self.attr_accessor (if it worked) would be the same as attr_accessopr, which isn’t what you want. You want attr_accessor to go into the ghost singleton class.

Dave

12 Jul 2008, 02:12
Generic-user-small

Bharat Ruparel (146 posts)

Thanks. I am finding that I have to go back and review the episodes multiple times to get a better understanding of the concepts. Every time I do that, I take a few steps forward, but sometimes, it is going backwards as well.

I see that I have to start using the reference section of your book more often. The attr_accessor method is clearly documented as a private instance method there.

I use NetBeans for my Ruby and Ruby on Rails work and its debugger is very helpful in steppig through the program code. I add a “watch” on the “self” variable and it is fun to see it change as control moves from line to line.

One question I have is when would self become a module in these examples? I see it bounce between the Class names and Object (at the top level).

12 Jul 2008, 15:50
Dave_gnome_head_isolated_pragsmall

Dave Thomas (338 posts)

bq. I use NetBeans for my Ruby and Ruby on Rails work and its debugger is very helpful in steppig through the program code. I add a “watch” on the “self” variable and it is fun to see it change as control moves from line to line.

bq. One question I have is when would self become a module in these examples? I see it bounce between the Class names and Object (at the top level).

Try:

module Bharat
    p self
end
12 Jul 2008, 20:32
Generic-user-small

Bharat Ruparel (146 posts)

Thanks for the tip:

I tried it, and it prints:

Bharat

Next, to see how it differs when you defined a method in it and inlcude it in a class, I created the following test scenario:

module Bharat
    p self
    def method1
      p self
    end
end

class Class1
  include Bharat  
end

m1 = Class1.new.method1

This prints the following:

Bharat #

Next, I tried the following code:

module Bharat
    p self
    def method1
      p self
    end
end

class Class1
  extend Bharat  
end

m1 = Class1.method1

This prints the following result:

Bharat Class1

When I compare and contrast these results. I am thinking that a module is nothing but a class that cannot be instantiated. Also, a method in a module can be an instance method or a class method depending on whether the module is included in the class or extended. Fundamentally, it is just a grouping of similar behavior that can be shared across different classes. Is that a correct way of approaching it?

Bharat

  You must be logged in to comment