|
|
|
SearchCategories
Books by the AuthorTwitter AccountOther Ruby Projects |
RegexpChallenge
Posted over 5 years ago
in Early Steps and Ruby Tutorials.
Just recently I have been working with two different people to improve their regular expression skills. To help me in this endeavor, I built a trivial little script we have been using in IRb. To get started, you construct a new challenge object and add a couple of challenges:
You can ask for challenges to see what you would like to solve:
Finally, you attempt solutions by giving the system the index of the challenge, a method name to call on the input String, a Regexp to pass, and any other needed parameters:
After you have played with it for a while you will probably build up some solutions and challenges. You can use the Finally, for some real fun, the challenger supports sharing the challenges over a network with another user. The host should just change the initial construction call to:
And then another user can get a copy of the challenge object with:
If that sounds like something you would like to play with, here's the actual code for the library you load into IRb. Feel free to post challenges for people to try in the comments of this post. |
|
|
|
Great idea, James. Can I ask a quick question about the code? I didn't understand the purpose behind this line in the 'solutions' method:
You're replacing leading spaces in each line with what exactly (and why)?
Thanks!
\0is the entire match in a replacement String ($&after the match). I'm just doubling the leading space so that the longer word "Solution," lines up neatly with the others.Very cool. I'll definitely be playing around with this. Thanks!
Interestingly, I created something similar for my Rails Day project. You can download the source here: http://svn.railsday2006.com/railsday/team61/regex_tutor/trunk/
You may need to use the latest version of Safari or Firefox to see it properly though. Maybe someday I'll turn it into a real site.
(waps forehead) of course! Thanks, James.
Here is my (completed untested) modification which allows you to use a block for your solution and match any ruby object.
This does all sorts of destruction to the pretty 'solutions' stuff you do, but was a fun hack on the plane on the way back from OKC
http://stonecode.org/drb_challenger.rb