From: Yuri Leikind Date: 2001-08-23T19:46:53+09:00 Subject: [ruby-talk:20177] Ruby article example suggestion Hello all, As soon as Joshua Drake is writing an article about Ruby-specific features I have a suggestion for an example that shows such features as a. Ability to easily extend built-in classes (like I do with class Object) b. Classes are really objects (constants) of class Class c. Reflexion methods like 'class', 'superclass','ancestors',etc b. Dynamic method call - object.send('method_string') This is just a suggestion. The example to show Ruby specific features can be different, but I hope you understand what I mean by this example. Reading and writing to text files is cool, but... This wouldn't look _much_ different than in , say, Python. You will recognize a few lines form the Ruby book. Here it goes: ##################################### class Object def explore print "Object id -\t", self.id, "\n" # 'self' can be ommited here print "Object inspected -\t", self.inspect, "\n" print "Object class -\t", self.class, "\n" puts 'Class hierarchy:' print "\t" klass=self.class klasses=[] begin # Dave & Andy's lines here ;-) print klass klasses << klass klass=klass.superclass print ' < ' if klass end while klass puts print "Mixed in modules:\n\t" (self.class.ancestors - klasses).each do |m| print m, ' ' end puts end def explore_class klass=self.class print "=== class #{klass} ===\n" ['singleton_methods', # other similar methods can be added 'public_instance_methods', 'protected_instance_methods', 'constants'].each do |method_string| puts method_string.gsub(/_/,' ')+':' n=0 klass.send(method_string).each do |method| # This is cooool !!! print "\t", method,"\t" n += 1 if n == 3 n=0 print "\n" else print "\t" end end puts end end end ######################################### If these lines are placed in some file, say, explore.rb, once you have require 'explore.rb' you can use the 2 above methods with any object: a=3.6 a.explore b='apple' b.explore_class Or one can write wonderful example with ObjectSpace , but I can't think of a practical example right now. Just my 2 roubles. Yuri Leikind