Ruby Evaluation

see the Lang Project for more source code

#create a folder "Talker"
#create a file "talker.rb"

class Talker
  attr_accessor :name, :age
 
  def sayHello
    puts "Hello world"
  end

  def sayYourName
    puts @name
  end

  def sayYourAge
    puts @age
  end

  # Simple Reflective Data Access
  def sayYourClassName
    puts self.class.name   # getter method "name" is called
  end

  # Advanced Reflective Data Access 
  def thisMethodName
    caller[0] =~ /in `([^']+)'/ ? $1 : '(anonymous)';
  end
  #LIMITATION: cryptic syntax, not directly OO based

  def sayHelloAdvanced
    puts "#{thisMethodName}: Hello World"
  end

  # Expert Reflective Data Access 
  def sayYourClassDefinition   
    puts "LIMITATION: precise recreation of class definition not possible"
    puts %{
    Class #{self.class.name}
      Methods:
     public:
      #{public_methods.inspect}
     protected:
      #{protected_methods.inspect}
     private:
      #{private_methods.inspect}
     non-inherited:
      #{(methods - self.class.superclass.instance_methods).inspect}
       
     Instance Variables:
     #{instance_variables.inspect}
     }        
  end

  def sayYourClassCode
    puts "LIMITATION: recreation of source-code not possible"
    puts "LIMITATION: Class Definition / AST is not accessible via MetaClass"
    puts "  Possible solution:"    
    puts "  http://rubyforge.org/projects/parsetree/"   
  end   

  def sayYourInstanceVarName
    puts "LIMITATION: instance variable name not available"
  end

  # Apply Metadata to any Object (Classes, Object Intances, Variables, Methods)
  # a Standard is not defined
  # below a simple implementation (add to "talker.rb"):
  class Object
    def meta     # adds variable "meta" to all objects in the system
  end

end
  # create a file "main.rb"

  require 'talker'

  john = Talker.new

  john.sayHello

  john.name = "John Doe Ruby"   # setter method "name=" is called
  john.age = 19
  john.sayYourName
  john.sayYourAge

  john.sayYourClassName 

  john.thisMethodName    
  john.sayHelloAdvanced

  john.sayYourClassDefinition
  john.sayYourClassCode
  john.sayYourInstanceVarName

  john.meta = "Instance meta information"
  1234.meta = 'any Instance meta information"

  puts Talker.meta
  puts john.meta
  puts 1234.meta  # an integer object

  puts "LIMITATION: attaching metadata to Class function def (Talker.sayHello.meta) not possible"
  puts "LIMITATION: attaching metadata to Class variables def (Talker.name.meta) not possible"

within irb:

cmd> irb  [opens interactive ruby shell]
irb> require 'talker'
irb> john = Talker.new
irb> john.sayHello