From: "Wai-Ming S. Li" <WLi@thoughtworks.com>
Date: April 17, 2005 6:51:43 PM CDT
To: submission@rubyquiz.com
Subject: Please Forward: Ruby Quiz SubumissionThis is my solution to the Madlabs Quiz and the unit tests.
Best Regards,
Steve Li.--------------------------------------------------------------------------------------------------------------------
Madlibs.rb
class Story
attr_accessor :placeholdersdef initialize(base)
@placeholders =story\_parts = \[\] match = Placeholder\.getPattern\(\)\.match\(base\) reuseMap = \{\} while\(match \!= nil\) story\_parts << match\.pre\_match placeholderString = match\[1\] placeholder = Placeholder\.new\(placeholderString, story\_parts\.size\) \# if name is reused if reuseMap\[placeholder\.name\] == nil @placeholders << placeholder \# if the name is reusable, add it to the reuse table if placeholder\.reusable\(\) reuseMap\[placeholder\.name\] = placeholder end \# replace the placeholder with the system generated position string story\_parts << get\_position\_string\(story\_parts\.size\.to\_s\) else \# for reuse placeholder, \# replace the placeholder with the system generated position string for the referenced placeholder story\_parts << get\_position\_string\(reuseMap\[placeholder\.name\]\.position\.to\_s\) end remaind = match\.post\_match match = Placeholder\.getPattern\(\)\.match\(match\.post\_match\) if \(match == nil\) story\_parts << remaind end end @base = story\_parts\.join\(""\)
end
def to_s
result = @base
@placeholders.each do |placeholder|
result.gsub!(Regexp.new(get_position_string(placeholder.position.to_s)), placeholder.value)
end
return result
enddef get_position_string(position)
"%%" + position.to_s + "%%"
end
endclass Placeholder
attr_accessor :name, :display_name, :position, :valuedef initialize(placeholderString, position)
@value = ""
@position = positionif placeholderString\.include?\(":"\) @name = placeholderString\.split\(":"\)\[0\] @display\_name = placeholderString\.split\(":"\)\[1\] else @name = placeholderString @display\_name = placeholderString end
end
def getTemplate()
Regexp.new("\\(\\(\\s*(#{name}|#{name}\\s*:\\s*#{display_name})\\s*\\)\\)")
enddef Placeholder.getPattern()
/\(\(([^)]*)\)\)/
enddef getValueQuestion()
"Give me #{display_name}: "
enddef reusable()
name != display_name
end
endif $0 == __FILE__
# read story from standard input
story_string = ""
ARGF.each_line do |line|
story_string += line
end# create story
story = Story.new(story_string)# request uesr to enter the corresponding value for each placeholder
print "There are #{story.placeholders.size} placeholders.\n"
story.placeholders.each do |placeholder|
print placeholder.getValueQuestion()
placeholder.value = gets().chop()
end# display the story
print story.to_s, "\n"
end--------------------------------------------------------------------------------------------------------------------
UnitTest;
MadlibsTest.rb
require 'runit/testcase'
require 'Madlibs'class TestMadlibs < RUNIT::TestCase
def testStoryTemplate()
# parse simple story
# e.g. "Our favorite language is ((a gemstone))."
template = "Our favorite language is ((a gemstone))."
story = Story.new(template)\# should return a Story with a symbol name='a gemstome' and alias=nil assert\_equals\(1, story\.placeholders\.size\) assert\_not\_nil\(story\.placeholders\[0\]\) assert\_equals\("a gemstone", story\.placeholders\[0\]\.display\_name\)
end
def testStoryTemplateWithAlias()
# parse story with name alias
# e.g. "Our favorite language is ((gem:a gemstone)). We think ((gem)) is
# better than ((a gemstone))."
template = "Our favorite language is ((gem:a gemstone)). "
template += "We think ((gem)) is better then ((a gemstone))."
story = Story.new(template)\# should return a Story with 2 symbole \# Symbol 1: name = 'gem' alias='a gemstome' \# Symbol 2: name = 'a gemstome' assert\_equals\(2, story\.placeholders\.size\) assert\_not\_nil\(story\.placeholders\[0\]\) assert\_equals\("gem", story\.placeholders\[0\]\.name\) assert\_equals\("a gemstone", story\.placeholders\[0\]\.display\_name\) assert\_not\_nil\(story\.placeholders\[1\]\) assert\_equals\("a gemstone", story\.placeholders\[1\]\.name\) assert\_equals\("a gemstone", story\.placeholders\[1\]\.display\_name\)
end
def testStoryGeneration()
# give: "Our favorite language is ((a gemstone))."
# input: gemstone = Ruby
# result: Our favorite language is Ruby."
String template = "Our favorite language is ((a gemstone))."
story = Story.new(template)
story.placeholders[0].value = "Ruby"
assert_equals("Our favorite language is Ruby.", story.to_s())
enddef testStoryGenerationWithAlias()
# given: "Our favorite language is ((gem:a gemstone)).
# We think ((gem)) is better than ((a gemstone))."
# input: a gemstone = Ruby, a genstone = Emerald
# given: "Our favorite language is Ruby.
# We think Ruby is better than Emerald."
template = "Our favorite language is ((gem:a gemstone)). "
template += "We think ((gem)) is better then ((a gemstone))."
story = Story.new(template)
story.placeholders[0].value = "Ruby"
story.placeholders[1].value = "Emerald"
assert_equals("Our favorite language is Ruby. We think Ruby is better then Emerald.", story.to_s())
end
endif $0 == __FILE__
require 'runit/cui/testrunner'
RUNIT::CUI::TestRunner.run(TestMadlibs.suite)
end
ยทยทยท
Begin forwarded message: