Skip to content

Commit 2cc25b6

Browse files
committed
Added more resources and exercises.
1 parent 94c4a8a commit 2cc25b6

File tree

6 files changed

+189
-1
lines changed

6 files changed

+189
-1
lines changed

README.rdoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* {Extending your include knowledge of Ruby}[http://macournoyer.wordpress.com/2007/07/06/extending-your-include-knowledge-of-ruby/]
1111
* {Include vs Extend in Ruby}[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/]
1212
* {Ruby's Metaprogramming Toolbox}[http://weare.buildingsky.net/2009/08/25/rubys-metaprogramming-toolbox]
13+
* {Ruby reflection}[http://www.khelll.com/blog/ruby/ruby-reflection/]
1314

1415
=== Videos
1516

@@ -21,4 +22,7 @@
2122

2223
* {Metaprogramming Ruby}[http://www.pragprog.com/titles/ppmetr/metaprogramming-ruby] - Entire book
2324

24-
Solutions to assignments from: http://ruby-metaprogramming.rubylearning.com
25+
== Exercises
26+
27+
* {Metaprogramming in Ruby}[http://ruby-metaprogramming.rubylearning.com/] - {Solutions}[http://github.com/geetarista/ruby-metaprogramming/tree/master/rubylearning/]
28+
* {An Exercise in Metaprogramming with Ruby}[http://www.devsource.com/c/a/Languages/An-Exercise-in-Metaprogramming-with-Ruby/]

devsource/my-csv.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class DataRecord
2+
def self.make(file_name)
3+
data = File.new(file_name)
4+
header = data.gets.chomp
5+
data.close
6+
class_name = File.basename(file_name,".txt").capitalize
7+
# "foo.txt" => "Foo"
8+
klass = Object.const_set(class_name,Class.new)
9+
names = header.split(",")
10+
11+
klass.class_eval do
12+
attr_accessor *names
13+
14+
define_method(:initialize) do |*values|
15+
names.each_with_index do |name,i|
16+
instance_variable_set("@"+name, values[i])
17+
end
18+
end
19+
20+
define_method(:to_s) do
21+
str = "<#{self.class}:"
22+
names.each {|name| str << " #{name}=#{self.send(name)}" }
23+
str + ">"
24+
end
25+
alias_method :inspect, :to_s
26+
end
27+
28+
def klass.read
29+
array = []
30+
data = File.new(self.to_s.downcase+".txt")
31+
data.gets
32+
data.each do |line|
33+
line.chomp!
34+
values = eval("[#{line}]")
35+
array << self.new(*values)
36+
end
37+
data.close
38+
array
39+
end
40+
41+
klass
42+
end
43+
end
44+
45+
data = DataRecord.make 'people.txt'
46+
p data.read
47+
p People.read
48+
49+
data = DataRecord.make 'place.txt'
50+
p data.read
51+
p Place.read

devsource/people.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name,age,weight,height
2+
"Smith, John", 35, 175, "5'10"
3+
"Ford, Anne", 49, 142, "5'4"
4+
"Taylor, Burt", 55, 173, "5'10"
5+
"Zubrin, Candace", 23, 133, "5'6"

devsource/place.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
latitude,longitude,description
2+
47.23,59.34,'Omaha'
3+
32.17,39.24,'New York City'
4+
73.11,48.91,'Carlsbad Caverns'

dwemthy/dwemthy.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# The guts of life force within Dwemthy's Array
2+
class Creature
3+
4+
# Get a metaclass for this class
5+
def self.metaclass; class << self; self; end; end
6+
7+
# Advanced metaprogramming code for nice, clean traits
8+
def self.traits( *arr )
9+
return @traits if arr.empty?
10+
11+
# 1. Set up accessors for each variable
12+
attr_accessor *arr
13+
14+
# 2. Add a new class method to for each trait.
15+
arr.each do |a|
16+
metaclass.instance_eval do
17+
define_method( a ) do |val|
18+
@traits ||= {}
19+
@traits[a] = val
20+
end
21+
end
22+
end
23+
24+
# 3. For each monster, the `initialize' method
25+
# should use the default number for each trait.
26+
class_eval do
27+
define_method( :initialize ) do
28+
self.class.traits.each do |k,v|
29+
instance_variable_set("@#{k}", v)
30+
end
31+
end
32+
end
33+
34+
end
35+
36+
# Creature attributes are read-only
37+
traits :life, :strength, :charisma, :weapon
38+
39+
# This method applies a hit taken during a fight.
40+
def hit( damage )
41+
p_up = rand( charisma )
42+
if p_up % 9 == 7
43+
@life += p_up / 4
44+
puts "[#{ self.class } magick powers up #{ p_up }!]"
45+
end
46+
@life -= damage
47+
puts "[#{ self.class } has died.]" if @life <= 0
48+
end
49+
50+
# This method takes one turn in a fight.
51+
def fight( enemy, weapon )
52+
if life <= 0
53+
puts "[#{ self.class } is too dead to fight!]"
54+
return
55+
end
56+
57+
# Attack the opponent
58+
your_hit = rand( strength + weapon )
59+
puts "[You hit with #{ your_hit } points of damage!]"
60+
enemy.hit( your_hit )
61+
62+
# Retaliation
63+
puts '#' # replaced by ashbb: p enemy
64+
if enemy.life > 0
65+
enemy_hit = rand( enemy.strength + enemy.weapon )
66+
puts "[Your enemy hit with #{ enemy_hit } points of damage!]"
67+
self.hit( enemy_hit )
68+
end
69+
end
70+
71+
end
72+
73+
class DwemthysArray < Array
74+
alias _inspect inspect
75+
def inspect; "#"; end # replaced by ashbb : def inspect; "#<#{ self.class }#{ _inspect }>"; end
76+
def method_missing( meth, *args )
77+
return unless first
78+
answer = first.send( meth, *args )
79+
if first.life <= 0
80+
shift
81+
if empty?
82+
puts "[Whoa. You decimated Dwemthy's Array!]"
83+
else
84+
puts "[Get ready. #{ first.class } has emerged.]"
85+
end
86+
end
87+
answer || 0
88+
end
89+
end

dwemthy/rabbit.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Rabbit < Creature
2+
traits :bombs
3+
4+
life 10
5+
strength 2
6+
charisma 44
7+
weapon 4
8+
bombs 3
9+
10+
# little boomerang
11+
def ^( enemy )
12+
fight( enemy, 13 )
13+
end
14+
# the hero's sword is unlimited!!
15+
def /( enemy )
16+
fight( enemy, rand( 4 + ( ( enemy.life % 10 ) ** 2 ) ) )
17+
end
18+
# lettuce will build your strength and extra ruffage
19+
# will fly in the face of your opponent!!
20+
def %( enemy )
21+
lettuce = rand( charisma )
22+
puts "[Healthy lettuce gives you #{ lettuce } life points!!]"
23+
@life += lettuce
24+
fight( enemy, 0 )
25+
end
26+
# bombs, but you only have three!!
27+
def *( enemy )
28+
if @bombs.zero?
29+
puts "[UHN!! You're out of bombs!!]"
30+
return
31+
end
32+
@bombs -= 1
33+
fight( enemy, 86 )
34+
end
35+
end

0 commit comments

Comments
 (0)