07 Sep 2008, 20:50
No_gravatar_pragsmall

Jon Stenqvist (5 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
08 Sep 2008, 03:35
Dave_gnome_head_isolated_pragsmall

Dave Thomas (338 posts)

Jon:

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

Dave

08 Sep 2008, 17:51
No_gravatar_pragsmall

Jon Stenqvist (5 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?

09 Sep 2008, 18:23
No_gravatar_pragsmall

Jon Stenqvist (5 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 `

  You must be logged in to comment