Forum: Ruby small question about ruby -c

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
pat.eyler (Guest)
on 2005-12-07 18:07
(Received via mailing list)
interesting little bug (I think)

When generating the answer to a previous email, I ran ruby -c against
a script and got 'syntax ok'.   (as expected)   I then changed an
if/else/end to if/otherwise/end, ran ruby -c again, and got 'syntax
ok'.  (not as expected).   I went ahead and ran the script (with the
'otherwise' change) and got:

-- primes_with_error.rb:29: undefined local variable or method
`otherwise' for main:Object (NameError)
        from primes_with_error.rb:25:in `each'
        from primes_with_error.rb:25


Any reason that ruby -c didn't catch this?


thanks,
-pate
phurley (Guest)
on 2005-12-07 18:15
(Received via mailing list)
On 12/7/05, pat eyler <removed_email_address@domain.invalid> wrote:
> When generating the answer to a previous email, I ran ruby -c against
> a script and got 'syntax ok'.   (as expected)   I then changed an
> if/else/end to if/otherwise/end, ran ruby -c again, and got 'syntax
> ok'.  (not as expected).

I can't see your code, but my guess is that the ruby interpreter
figured that otherwise is a function (not an accidental name for
else), so it is good syntax, even though there is a runtime error for
the missing method/variable name.

pth
pit (Guest)
on 2005-12-07 18:15
(Received via mailing list)
pat eyler schrieb:
>         from primes_with_error.rb:25:in `each'
>         from primes_with_error.rb:25
>
> Any reason that ruby -c didn't catch this?

Pat, this isn't a syntax error. The *syntax* is correct. Just define a
method or local variable named "otherwise" and all is fine.

Regards,
Pit
mental (Guest)
on 2005-12-07 19:53
(Received via mailing list)
Quoting pat eyler <removed_email_address@domain.invalid>:

> ok'.  (not as expected).   I went ahead and ran the script (with
> the 'otherwise' change) and got:
>
> -- primes_with_error.rb:29: undefined local variable or method
> `otherwise' for main:Object (NameError)
>         from primes_with_error.rb:25:in `each'
>         from primes_with_error.rb:25
>
>
> Any reason that ruby -c didn't catch this?

Basically, it's not a syntax error.  It's possible for a script to
be wrong in surprising ways but still syntactically valid.

-mental
This topic is locked and can not be replied to.