Reflection, also called introspection, examines a running Ruby program: objects can be asked about their methods, variables, state, and capabilities while execution continues. section 2.3.2 section 7.7
Check before sending a message
Calling an unknown message raises NoMethodError. A conditional check makes the interaction explicit: `if obj.respond_to?("talk"); obj.talk; else; puts "Sorry, the object doesn't understand the 'talk' message."; end`. `respond_to?` exists for every object and answers whether it currently handles a named message. section 2.3.2
Ruby reflection can inspect several layers of a running program:
- Ask objects which methods they can execute; class and module objects can expose methods available to their instances or through inclusion. section 7.7
- Inspect local and global variables, instance variables, class variables, and constants accessible at a particular runtime point. section 15.3
- Examine stack traces, including the method calls, filenames, and line numbers leading to an event. section 15.0
- Combine method queries with dynamic facilities such as `method_missing` and `respond_to_missing?` when handling unrecognized messages. section 15.6
Reflection is both passive inspection and a practical design tool. It supports capability checks, dynamic message handling, debugging, and code that reacts to runtime events; the available information depends on the object and the moment of inspection. section 7.7 section 15.0 section 15.6












