0

Interactive ruby ready.

> time = /\A(?­<hours>(0\d|1[0-9­]|2[0-3]))­:(?<minutes­>([1-5]\d|0­\d))\Z/
=> /\A(?<hours>(0\d|1[0-9]|2[0-3])):(?<minutes>([1-5]\d|0\d))\Z/

> match = time.­match '11:3­0'
=> #<TypeError: can't dump MatchData>

I am trying to run the code but getting error as above. What the wrong I did with the regexp help me to improve it.

EDIT

> time = /\A(0­\d|1[0-9­]­|2[0-3])­:­([0-5]\d)\­Z/
=> /\A(0\d|1[0-9??]|2[0-3])??:([0-5]\d)\Z/
> time.match­('11:30')
=> #<TypeError: can't dump MatchData>
> 
DoLoveSky
  • 777
  • 1
  • 6
  • 17

1 Answers1

2

It should be working, and the example you linked to does work on my machine. The regex is needlessly complicated, though:

time = /\A(?<hours>0\d|1[0-9]|2[0-3]):(?<minutes>[0-5]\d)\Z/

is enough.

However, it appears that somewhere in your environment, extra bytes have become embedded in the string(s), and these bytes confuse the online tester (which is working correctly). Try and copy the regex from this answer (I have removed the invisible characters) and see if it works for you now.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 1
    Yes, it does since 1.9 ? http://www.ruby-doc.org/core-1.9.3/Regexp.html#method-i-named_captures – oldergod Jan 16 '13 at 08:30
  • @oldergod: Ah, thanks. http://www.regular-expressions.info/refflavors.html appears to be outdated, then. – Tim Pietzcker Jan 16 '13 at 08:31
  • Please see my `EDIT` . Still I am having **ERROR** – DoLoveSky Jan 16 '13 at 08:31
  • 1
    @DoLoveSky: The traceback shows that there are spurious bytes between `3` and `0` in your input. Where does that string come from? – Tim Pietzcker Jan 16 '13 at 08:33
  • @TimPietzcker I got it from the [Link](http://ruby.runpaint.org/regexps) as I was going through it to learn! – DoLoveSky Jan 16 '13 at 08:36
  • 1
    I just downloaded this example, and it works fine here. Somewhere on your system, you're introducing extra bytes into the code. Perhaps a misconfigured editor? What locale are you in? Which text encoding are you using in your editor? – Tim Pietzcker Jan 16 '13 at 08:39
  • @TimPietzcker Okay,I am using the [online](http://tryruby.org/levels/1/challenges/0) – DoLoveSky Jan 16 '13 at 08:43
  • I have accepted your answer, But eager to know if that `online interptere` is `outdated or not`? – DoLoveSky Jan 16 '13 at 09:11