Ruby - Reflection an Object's Methods

Introduction

Reflection can inspect, analyze, and modify itself.

Ruby allows you to change the functionality of the language itself while running your own code.

It's possible to query almost any object within Ruby for the methods that are defined within it.

Demo

a = "This is a test" 
puts a.methods.join(' ')

Result

instance_variables returns the names of any object variables associated with an instance as opposed to class variables:

Demo

class Person 
  attr_accessor :name, :age # from  w  w w .j a v a  2  s  . co  m
end 

p = Person.new 
p.name = "Fred" 
p.age = 20 
puts p.instance_variables

Result

Related Topic