Let me see if I fully understand this.

This:

Object.instance_eval
  def a
   puts 'a'
  end
end

is equivalent to this:

class Object
  def self.a
   puts 'a'
  end
end

Basically, instance_eval invoked on Object creates a new class method
for the Object class (or for class Class? and if it is class Class, then
why bother invoking instance_eval it on Object?). The reason why I say
this is because that method is now available to class Class and all
classes created, since they all inherit from class Class when using the
class construct. The only logical explanation I can come up for this
behavior is that whenever you create a class method, the method is
automatically copied to class Class and therefore all classes inherit
from it. Correct? If so, then why use instance_eval on Object?

Example:

ruby-1.8.7-p330 :001 > class Object
ruby-1.8.7-p330 :002?>   def self.a
ruby-1.8.7-p330 :003?>     puts 'a'
ruby-1.8.7-p330 :004?>     end
ruby-1.8.7-p330 :005?>   end


ruby-1.8.7-p330 :006 > class A
ruby-1.8.7-p330 :007?>   end

ruby-1.8.7-p330 :011 > Object.a
a

ruby-1.8.7-p330 :008 > A.a
a

This fails because @a is an instance method that does not have access to
class methods, even class methods of Object:
ruby-1.8.7-p330 :009 > @a = A.new
 => #<A:0x1069a3088>
ruby-1.8.7-p330 :010 > @a.a
NoMethodError: undefined method `a' for #<A:0x1069a3088>
  from (irb):10
  from :0


ruby-1.8.7-p330 :015 > @a = Object.new
 => #<Object:0x1064fcf98>
ruby-1.8.7-p330 :016 > @a.a

Thanks for response

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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to