Compare instance_eval and class_eval. When would you use each?

Last updated 27 June 2026

  • instance_eval sets self to one object, so a def inside creates a singleton method on that object only. Use it for per-object config and DSL builder blocks.

  • class_eval (alias module_eval) runs as if inside the class body, so def creates an instance method shared by all instances.

  • Use class_eval to reopen or patch a class dynamically, especially when the class is held in a variable.

  • https://www.turing.com/interview-questions/ruby
  • https://www.youtube.com/watch?v=lkunzd5uDrw
  • https://blog.jacobvosmaer.nl/0004-ruby-scheduler-interaction/
  • https://wohotech.in/coding/top-java-interview-questions
  • https://jetthoughts.com/blog/ruby-3-4-yjit-performance-guide/
  • https://speed.ruby-lang.org/
  • https://flexiple.com/python/interview-questions
All Ruby interview questions