Tutorial by Examples

Inspecting an Object You can find the public methods an object can respond to using either the methods or public_methods methods, which return an array of symbols: class Foo def bar; 42; end end f = Foo.new def f.yay; 17; end p f.methods.sort #=> [:!, :!=, :!~, :<=>, :==, :===, :=...
It is possible to query an object about its instance variables using instance_variables, instance_variable_defined?, and instance_variable_get, and modify them using instance_variable_set and remove_instance_variable: class Foo attr_reader :bar def initialize @bar = 42 end end f = F...
The Kernel exposes methods for getting the list of global_variables and local_variables: cats = 42 $demo = "in progress" p global_variables.sort #=> [:$!, :$", :$$, :$&, :$', :$*, :$+, :$,, :$-0, :$-F, :$-I, :$-K, :$-W, :$-a, #=> :$-d, :$-i, :$-l, :$-p, :$-v, :$-w, :$...
Classes and modules have the same methods for introspecting instance variables as any other object. Class and modules also have similar methods for querying the class variables (@@these_things): p Module.methods.grep(/class_variable/) #=> [:class_variables, :class_variable_get, :remove_class_va...

Page 1 of 1