Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Syntax checker?

Reply
Thread Tools

Syntax checker?

 
 
William E. Rubin
Guest
Posts: n/a
 
      12-07-2005
Ruby doesn't seem to check for class names, function names, and so
forth until it actually hits a line that tries to use such a thing. Is
there a way to get it to check up front, without having to run the
script through every possible line of code?

I understand that this is not strictly "syntax" checking, and that Ruby
does actually do a "syntax" check. So I mean some other term, more
along the lines of what a traditional compiler will do (minus the
actual compilation).

 
Reply With Quote
 
 
 
 
Brian Schröder
Guest
Posts: n/a
 
      12-07-2005
On 07/12/05, William E. Rubin <(E-Mail Removed)> wrote:
> Ruby doesn't seem to check for class names, function names, and so
> forth until it actually hits a line that tries to use such a thing. Is
> there a way to get it to check up front, without having to run the
> script through every possible line of code?
>
> I understand that this is not strictly "syntax" checking, and that Ruby
> does actually do a "syntax" check. So I mean some other term, more
> along the lines of what a traditional compiler will do (minus the
> actual compilation).
>
>
>


That is actually impossible, as the system can not determine which
functions exists at any given line without executing the previous
lines. E.g.

def method_missing(name, *args, &block)
return name if args.length =3D=3D 0
super(name, *args, &block)
end

foo()
bar(foo())

cheers,
Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/


 
Reply With Quote
 
 
 
 
pat eyler
Guest
Posts: n/a
 
      12-07-2005
I'm not sure I understand your request, but are you looking for ruby -c scr=
ipt?

pate@linux:~/scratch> ruby -c cprimes.rb
Syntax OK
pate@linux:~/scratch> ruby -c primes_with_error.rb
primes_with_error.rb:32: syntax error
pate@linux:~/scratch>


On 12/7/05, William E. Rubin <(E-Mail Removed)> wrote:
> Ruby doesn't seem to check for class names, function names, and so
> forth until it actually hits a line that tries to use such a thing. Is
> there a way to get it to check up front, without having to run the
> script through every possible line of code?
>
> I understand that this is not strictly "syntax" checking, and that Ruby
> does actually do a "syntax" check. So I mean some other term, more
> along the lines of what a traditional compiler will do (minus the
> actual compilation).
>
>
>



--
thanks,
-pate
-------------------------


 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      12-07-2005
William E. Rubin schrieb:
> Ruby doesn't seem to check for class names, function names, and so
> forth until it actually hits a line that tries to use such a thing. Is
> there a way to get it to check up front, without having to run the
> script through every possible line of code?
>
> I understand that this is not strictly "syntax" checking, and that Ruby
> does actually do a "syntax" check. So I mean some other term, more
> along the lines of what a traditional compiler will do (minus the
> actual compilation).


Hi William,

if you call the ruby interpreter with the -c command line option, then
it performs a syntax check:

C:\tmp>ruby -c r.rb
Syntax OK

(http://www.ruby-doc.org/docs/Program...yworld.html#UA)

On the other hand, if you want to syntactically check for valid class
names, then you're out of luck. Ruby is too dynamic. Look at this little
script:

C:\tmp>type r.rb

print "enter class name: "
name = gets.chomp
Object.const_set name, Class.new
p X.new

This script prompts the user for a classname and then creates a new
class under that name. It then tries to create an instance of class X.
You can't check whether "X" is a valid class name without actually
running the code:

C:\tmp>ruby r.rb
enter class name: X
#<X:0x2b31708>

C:\tmp>ruby r.rb
enter class name: Y
C:/tmp/r.rb:5: uninitialized constant X (NameError)

I'm not sure the Ruby IDEs can give you more hints about misspelled
names. The best way for me is simply doing Test Driven Development.

Regards,
Pit


 
Reply With Quote
 
William E. Rubin
Guest
Posts: n/a
 
      12-07-2005
Thanks for the explanation. But there certainly could at least be a
way to produce warnings. In your example, it could warn (without
having executed any of your code) that X might be uninitialized.

I'm not saying Ruby should do this as a default, but it would be nice
to have an option (or a separate tool) to do so.

I mean, I just had a Ruby script crash because one line of code
contained "RegExp" instead of "Regexp". This script had been working
fine for quite some time, but it just happened to get into the
situation where that line was encountered for the first time. It would
have been nice to have had a tool that told me "Warning: RegExp might
not exist".

 
Reply With Quote
 
Dougan
Guest
Posts: n/a
 
      12-07-2005

William E. Rubin wrote:
> It would
> have been nice to have had a tool that told me "Warning: RegExp might
> not exist".


I agree. This bites me all the time too. Even old VB had "Option
Explicit," to check
your typos - (altho I know it did more than check typos).

Generally in Ruby my sloppy typing slows down my development more than
defining my variables would.

 
Reply With Quote
 
Tanner Burson
Guest
Posts: n/a
 
      12-07-2005
------=_Part_1301_19053959.1133977154316
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On 12/7/05, William E. Rubin <(E-Mail Removed)> wrote:
>
> Thanks for the explanation. But there certainly could at least be a
> way to produce warnings. In your example, it could warn (without
> having executed any of your code) that X might be uninitialized.
>
> I'm not saying Ruby should do this as a default, but it would be nice
> to have an option (or a separate tool) to do so.
>
> I mean, I just had a Ruby script crash because one line of code
> contained "RegExp" instead of "Regexp". This script had been working
> fine for quite some time, but it just happened to get into the
> situation where that line was encountered for the first time. It would
> have been nice to have had a tool that told me "Warning: RegExp might
> not exist".



That's the exact scenario that unit tests are great for, finding issues in
parts of code that aren't hit often, or nearly at all. If they're covered
by a unit test though you know, right away that something is wrong, and you
can fix it, without waiting for that scenario to arrive.




--
=3D=3D=3DTanner Burson=3D=3D=3D
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://tannerburson.com <---Might even work one day...

------=_Part_1301_19053959.1133977154316--


 
Reply With Quote
 
William E. Rubin
Guest
Posts: n/a
 
      12-07-2005
> That's the exact scenario that unit tests are great for, finding issues in
> parts of code that aren't hit often, or nearly at all. If they're covered
> by a unit test though you know, right away that something is wrong, and you
> can fix it, without waiting for that scenario to arrive.


Well sure, but for a quick and dirty one-off script, that you're
potentially changing a bunch as the need arises, it's not always
feasible (timewise) to design unit tests for every possible scenario,
nor to keep them up to date.

 
Reply With Quote
 
Jamis Buck
Guest
Posts: n/a
 
      12-07-2005
On Dec 7, 2005, at 10:32 AM, William E. Rubin wrote:

> Thanks for the explanation. But there certainly could at least be a
> way to produce warnings. In your example, it could warn (without
> having executed any of your code) that X might be uninitialized.
>
> I'm not saying Ruby should do this as a default, but it would be nice
> to have an option (or a separate tool) to do so.
>
> I mean, I just had a Ruby script crash because one line of code
> contained "RegExp" instead of "Regexp". This script had been working
> fine for quite some time, but it just happened to get into the
> situation where that line was encountered for the first time. It
> would
> have been nice to have had a tool that told me "Warning: RegExp might
> not exist".


At the risk of sounding pedantic, Test Driven Development really is
the best solution to this. Not only will it catch the cases you are
talking about, but if done right it can even catch the dynamic cases
that were given as counterexamples earlier. If you have tests that
cover your code sufficiently, you'll find these kinds of problems
much, much sooner. And Ruby makes TDD so easy it's almost criminal to
not take advantage of it.

- Jamis



 
Reply With Quote
 
William E. Rubin
Guest
Posts: n/a
 
      12-07-2005
> At the risk of sounding pedantic, Test Driven Development really is
> the best solution to this. Not only will it catch the cases you are
> talking about, but if done right it can even catch the dynamic cases
> that were given as counterexamples earlier. If you have tests that
> cover your code sufficiently, you'll find these kinds of problems
> much, much sooner. And Ruby makes TDD so easy it's almost criminal to
> not take advantage of it.


At the risk of repeating myself, it seems to me that for one-off
scripts that you may constantly be tweaking as the need arises, the
time cost of designing unit tests, and keeping them up to date, for
every possible scenario, is prohibitive.

That said, I notice that you have capitalized "Test Driven
Development"; that and the fact that you say that Ruby makes it so easy
that it's criminal to not take advantage of it leads me to believe that
you might be talking about something very specific, perhaps involving
some specific tool or specific coding technique, that I am not aware
of, as opposed to talking about the generic "unit test your code"
theory.

If so, could you please elaborate? Pointers to a website, or Google
search terms, or whatever, would be appreciated greatly.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM



Advertisments