Project

General

Profile

Actions

Misc #19912

open

[Small potential for slight improvement in regards to trailing / that were omitted in a regex]

Added by rubyFeedback (robert heiler) about 1 year ago. Updated about 1 year ago.

Status:
Open
Assignee:
-
[ruby-core:114955]

Description

Not sure if I should file this under bug or not; it is probably not a bug per se
and just the current behaviour. So I filed it under Misc here.

So let me copy/paste the code next:

alias e puts


i = 'abc'

  case i
  when /help
    e 'No help options are currently documented.'
  when /-?-?rotate=(\d+)$/i
      pp $1.to_s
      exit
  end

end

I isolated this from a larger .rb file. I was adding support for
--rotate= for a class tasked with commandline stuff in regards to
.pdf files, e. g. rotating wrong .pdf files.

When I run this .rb file, that is the one I showed above, I get
the following error, as-is (the extra "end" is kind of deliberate,
I had a much larger case/when structure before).

foo.rb: --> foo.rb
Unmatched `end', missing keyword (`do', `def`, `if`, etc.) ?
>  4  i = 'abc'
>  6    case i
>  7    when /help
>  8      e 'No help options are currently documented.'
>  9    when /-?-?rotate=(\d+)$/i
> 12    end
> 14  end
foo.rb:9: syntax error, unexpected backslash (SyntaxError)
  when /-?-?rotate=(\d+)$/i
                ^

Because there are at the least two problems in this .rb file,
it appears to be that the ruby parser focuses on the last
error found. To those who can not instantly find the error,
I simply forgot a trailing / in the case/when menu, at
"when /help". That actually was the real bug too. But I first
looked down, where ruby alerted me that there is an issue
with "when /-?-?rotate=(\d+)$/i". The real problem was the
missing "/".

Is there potential to improve handling of such small mistakes,
specifically someone forgetting a trailing / in a case/when
menu? I assume ruby has to decide what is going on, but usually
when I use a case/when menu, I have multiple such entries, so
perhaps this gives the ruby parser additional information it
could use, such as: "look at the immediate surrounding and
then determine whether this may be a missing /".

I don't know how difficult it is to add that, and if it is too
much work then I think the time may be spent elsewhere. But on
the other hand, if the ruby parser could detect such small
mistakes more easily (a forgotten /), this would be rather nice
to have.

This may also have to do with how ruby reports errors; I guess
the part "Unmatched end', missing keyword (do', def, if,
etc.) ?" is ruby's attempt to give useful information to the
user, and in many cases this should work. But in case of simple
errors such as the above it may not work, and secondary errors
may disguise or confuse the user momentarily. I usually look at
the last part of any long trace shown by the ruby parser to me,
so that is why I sometimes miss prior errors.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0