LA Ruby Study Group Quiz Fun 5/15

29 views
Skip to first unread message

Bruce

unread,
May 16, 2013, 1:44:26 AM5/16/13
to laruby...@googlegroups.com
Hi folks,

In addition to our usual reading, towards the end of tonight we decided to tackle a ruby quiz found at http://www.swarut.com/node/114 (probably republished from the original ruby quiz site).  We're still working on the part of the problem where we reuse answers: "That covers the simple cases, but in some instances we may want to reuse an answer. For that, we’ll introduce a way to name them:  Our favorite language is ((gem:a gemstone)). We think ((gem)) is better than ((a gemstone))."

Here is the solution we came up with so far posted below and also as a gist: https://gist.github.com/treble37/5589616.  Feel free to join in the fun below...we're probably going to work on this next week....

sentence_library=["I had a ((an adjective)) sandwich for lunch today. It dripped all over my ((a body part)) and ((a noun)).",

  "Our favorite language is ((gem:a gemstone)). We think ((gem)) is better than ((a gemstone))."]

#token_arr = sentence.scan(/\(\(([^)]+)\)\)/)

sentence = sentence_library[rand(sentence_library.size)]

token_arr = sentence.scan(/\(\(.*?\)\)/)



hsh = {}

token_arr.each do |t|

  puts "Enter a word for #{t.delete('()')}?"

  hsh[t] = gets.chomp

end

hsh.each do |key,val|

  sentence.gsub!(key,val)

end

p sentence


Reply all
Reply to author
Forward
0 new messages