Some new coverage of an old but favorite quiz.
James Edward G. II
Begin forwarded message:
Some new coverage of an old but favorite quiz.
James Edward G. II
Begin forwarded message:
On Tue, 6 Feb 2007, James Edward G. II wrote:
Some new coverage of an old but favorite quiz.
thanks james
behind). Anyways, I think that the koans are missing or have incorrect
assertions.I’m not sure if you post corrections, or if you can forward this on to the
author, but I posted some of the problems that I discovered while solving
the quiz at http://kinderman.net/articles/2007/02/05/
problems-with-the-metakoans-rb-ruby-quiz.
hi ryan-
yeah. interesting point. in fact, you are right but for reasons you
might
not suspect. regarding the
assert{ (o.a = nil) == nil }
assertions you’ll note they all start when default values are being
provided,
what’d i’d meant to testing here was that the setter should always
return the
new value and not the one provided as a default. obviously that’s a
mistake
since, in ruby, setters always do this, one cannot change it:
harp:~ > cat a.rb
def a=(x) 42 end
p(self.a = ‘forty-two’)
harp:~ > ruby a.rb
“forty-two”
so the best thing would be to simple remove those test or, as you point
out,
do a two step test. i chose the latter for the new version.
regarding incrementing 42 - that’s impossible! see if you like my
new
approach. it acheives the same end goal, which is valuable:
harp: ~> cat a.rb
‘attribute’ which
deleving
follows
will see
file
below.
guru whose
module MetaKoans
def koan_1
c = Class::new {
attribute 'a'
}
o = c::new
assert{ not o.a? }
assert{ o.a = 31 }
assert{ o.a == 31 }
assert{ o.a? }
end
def koan_2
c = Class::new {
class << self
attribute 'a'
end
}
assert{ not c.a? }
assert{ c.a = 32 }
assert{ c.a == 32 }
assert{ c.a? }
end
module
def koan_3
m = Module::new {
class << self
attribute 'a'
end
}
assert{ not m.a? }
assert{ m.a = 33 }
assert{ m.a == 33 }
assert{ m.a? }
end
operate
def koan_4
m = Module::new {
attribute 'a'
}
c = Class::new {
include m
extend m
}
o = c::new
assert{ not o.a? }
assert{ o.a = 34 }
assert{ o.a == 34 }
assert{ o.a? }
assert{ not c.a? }
assert{ c.a = 35 }
assert{ c.a == 35 }
assert{ c.a? }
end
objects
def koan_5
o = Object::new
class << o
attribute 'a'
end
assert{ not o.a? }
assert{ o.a = 36 }
assert{ o.a == 36 }
assert{ o.a? }
end
hash
def koan_6
c = Class::new {
attribute 'a' => 37
}
o = c::new
assert{ o.a == 37 }
assert{ o.a? }
assert{ o.a = nil; o.a == nil }
assert{ not o.a? }
end
block
def koan_7
c = Class::new {
attribute('a'){ b }
def b
38
end
}
o = c::new
assert{ o.b == 38 }
assert{ o.a == 38 }
assert{ o.a? }
assert{ o.a = nil; o.a == nil }
assert{ not o.a? }
assert{ o.b == 38 }
end
class and
def koan_8
b = Class::new {
class << self
attribute 'a' => 39
attribute('b'){ a }
end
attribute 'a' => 40
attribute('b'){ a }
}
c = Class::new b
assert{ c.b == 39 }
assert{ c.a == 39 }
assert{ c.a? }
assert{ c.a = nil; c.a == nil }
assert{ not c.a? }
assert{ c.b == 39 }
o = c::new
assert{ o.b == 40 }
assert{ o.a == 40 }
assert{ o.a? }
assert{ o.a = nil; o.a == nil }
assert{ not o.a? }
assert{ o.b == 40 }
end
def koan_9
b = Class::new {
class << self
attribute 'a' => 41
attribute('b'){ a }
end
include Module::new {
attribute 'a' => 42
attribute('b'){ a }
}
}
c = Class::new b
assert{ c.b == 41 }
assert{ c.a == 41 }
assert{ c.a? }
assert{ c.a = nil; c.a == nil }
assert{ not c.a? }
assert{ c.b == 41 }
o = c::new
assert{ o.b == 42 }
assert{ o.a == 42 }
assert{ o.a? }
assert{ o.a = nil; o.a == nil }
assert{ not o.a? }
assert{ o.b == 42 }
end
def assert() raise unless yield end
end
class MetaStudent
def initialize knowledge
require knowledge
end
def ponder koan
begin
send koan
true
rescue => e
STDERR.puts %Q[#{ e.message } (#{ e.class })\n#{
e.backtrace.join 10.chr }]
false
end
end
end
class MetaGuru
require “singleton”
include Singleton
def enlighten student
student.extend MetaKoans
koans = student.methods.grep(%r/koan/).sort
attainment = nil
koans.each do |koan|
awakened = student.ponder koan
if awakened
puts "#{ koan } has expanded your awareness"
attainment = koan
else
puts "#{ koan } still requires meditation"
break
end
end
puts(
case attainment
when nil
"mountains are merely mountains"
when 'koan_1', 'koan_2'
"learn the rules so you know how to break them properly"
when 'koan_3', 'koan_4'
"remember that silence is sometimes the best answer"
when 'koan_5', 'koan_6'
"sleep is the best meditation"
when 'koan_7'
"when you lose, don't lose the lesson"
when 'koan_8'
"things are not what they appear to be: nor are they
otherwise"
else
“mountains are again merely mountains”
end
)
end
def self::method_missing m, *a, &b
instance.send m, *a, &b
end
end
knowledge = ARGV.shift or abort “#{ $0 } knowledge.rb”
student = MetaStudent::new knowledge
MetaGuru.enlighten student
-a
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs