File: metaclass.rb

Project: Invitation to Ruby

#!/usr/bin/env ruby

class Dog
  def talk() puts "Woof" end
end
  
class Cat
  def talk() puts "Meow" end
end
  
animals = { 'dog'=>Dog, 'cat'=>Cat }

print "What kind of pet do you want? "
pet_name = gets
pet = animals[pet_name.chomp].new
pet.talk


[ Index ][ Table of Contents ]
Generated by [ source2html ]