Permalink
Browse files

Added the MetaPrograming related files

  • Loading branch information...
Attila Domokos Attila Domokos
Attila Domokos authored and Attila Domokos committed Jan 10, 2010
1 parent ef25dba commit 786dc5e0855344f36ed310d540bf18babb9aa1cc
Showing with 90 additions and 0 deletions.
  1. +59 −0 MetaProgramming/animals.rb
  2. +31 −0 MetaProgramming/new_plant.rb
View
@@ -0,0 +1,59 @@
+module Carnivore
+ def diet
+ 'meat'
+ end
+
+ def teeth
+ 'sharp'
+ end
+end
+
+module Herbivore
+ def diet
+ 'plant'
+ end
+
+ def teeth
+ 'flat'
+ end
+end
+
+module Nocturnal
+ def sleep_time
+ 'day'
+ end
+
+ def awake_time
+ 'night'
+ end
+end
+
+module Diurnal
+ def sleep_time
+ 'night'
+ end
+
+ def awake_time
+ 'day'
+ end
+end
+
+def new_animal(diet, awake)
+ animal = Object.new
+
+ if diet == :meat
+ animal.extend(Carnivore)
+ else
+ animal.extend(Herbivore)
+ end
+
+ if awake == :day
+ animal.extend(Diurnal)
+ else
+ animal.extend(Nocturnal)
+ end
+end
+
+animal1 = new_animal(:meat, :day)
+
+puts "animal1 diet: #{animal1.diet} teeth: #{animal1.teeth} sleep_time: #{animal1.sleep_time} wake_time: #{animal1.awake_time}"
@@ -0,0 +1,31 @@
+def new_plant(stem_type, leaf_type)
+ plant = Object.new
+
+ if stem_type == :fleshy
+ def plant.stem
+ 'fleshy'
+ end
+ else
+ def plant.stem
+ 'woody'
+ end
+ end
+
+ if leaf_type == :broad
+ def plant.leaf
+ 'broad'
+ end
+ else
+ def plant.leaf
+ 'needle'
+ end
+ end
+
+ plant
+end
+
+plant1 = new_plant(:fleshy, :broad)
+plant2 = new_plant(:woody, :needle)
+
+puts "Plant 1's stem: #{plant1.stem} leaf: #{plant1.leaf}"
+puts "Plant 2's stem: #{plant2.stem} leaf: #{plant2.leaf}"

0 comments on commit 786dc5e

Please sign in to comment.