Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old November 16th, 2007, 12:18 PM
Dankelly07 Dankelly07 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 1 Dankelly07 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 45 sec
Reputation Power: 0
Can anyone help?

Hi, I just need a little help with a problem Iam doing, I think I've almost figured it out I just need some help finishing it. Ive been asked to write a regular expression to see whether a string represents a valid roman numeral. So I did that pretty much..

I=1
V=5
X=10
L=50
C=100
D=500
M=1000

r1 = "M{0,1}"
r2 = "(CM|CD|D?C{0,3})"
r3 = "(XC|XL|L?X{0,3})"
r4 = "(IX|IV|V?I{0,3})"
pattern = "^#{r1}#{r2}#{r3}#{r4}$"

roman = Regexp.new(pattern)

end

How would I go about testing this program, is there anything I need to add, to proceed from here?
Any help would be much appreciated,
Thanks,
Dan

Reply With Quote
  #2  
Old December 30th, 2007, 02:07 AM
twostepted's Avatar
twostepted twostepted is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: Central Washington (USA)
Posts: 515 twostepted User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 50 m
Reputation Power: 5
Send a message via Yahoo to twostepted
You should try to look at the output of roman.inspect() or roman.to_yaml() will give you an idea of what the object that Regexp.new() has returned. That may help you to proceed.

Reply With Quote
  #3  
Old December 30th, 2007, 02:23 AM
twostepted's Avatar
twostepted twostepted is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: Central Washington (USA)
Posts: 515 twostepted User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 50 m
Reputation Power: 5
Send a message via Yahoo to twostepted
I forgot to include that in order to test your example, you'll have to do:
Code:
numeral = "XVIII"
roman.match(numeral)

Also, you can evaluate a regexp using the =~ operator like this:
Code:
"XVIII" =~ /romanRegexp/

the results of the match are available in the global variables $1, $2, $3, ...

Have a look at the documentation

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Can anyone help?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway