No_gravatar_small Jon Stenqvist 4 posts

Hi,

Thanks for great screencasts.

After getting the “metaclass” class << self; self; end. I can’t find any difference between in using instance_eval and class_eval. I get the feeling that the right method for the job would be instance_eval because it that will create instance methods on the metaclass, and as you told on the screencast there arn’t really any class_methods.

Are there a difference between this two snippets? instance_eval/class_eval.

class Dave
  # def self.const_missing(name)
  #   puts "Missing #{name} in Dave" 
  #   super
  # end
  ghost = class << self; self; end
  ghost.class_eval do
    original_const_missing = instance_method(:const_missing)
    define_method(:const_missing) do |name|
      puts "Missing #{name} in Dave" 
      original_const_missing.bind(self).call(name)
    end
  end
end
Dave::Fred

class Dave
  ghost = class << self; self; end
  ghost.*instance_eval* do
    original_const_missing = instance_method(:const_missing)
    define_method(:const_missing) do |name|
      puts "Missing #{name} in Dave" 
      original_const_missing.bind(self).call(name)
    end
  end
end
 
Dave_8_trans_small Dave Thomas Administrator 72 posts

Jon:

define_method ignores class_ or instance_eval and always defines an instance method.

Dave

 
No_gravatar_small Jon Stenqvist 4 posts

As a novice on this I want rules ;) After getting the metaclass, the preferred would be to use class_eval because I want to add a instance method to the metaclass?

 
No_gravatar_small Jon Stenqvist 4 posts

Does also const_set ignore instance/class_eval?

class Application end Application.instance_eval do const_set(:TEST_CONST,"JON") end Application.class_eval do const_set(:TEST_CONST,"JON") end

4 posts, 2 voices