Ruby Source code checker

M

Michael Gebhart

Hi,

currently I am doing my first steps with ruby. It's really great, but
sometimes I'm doing some very stupid mistakes :) I only realize this, when
running the program and starting the function, where I have done the
mistake. Is there any possibility to check the code before running it?
Because if I do not run the function, I'll never see, that there is a
error, which is followed by a crash. The problem is, that
ruby doesn't know the types, the variables have. When I do:

@variable.function

ruby does not know, if the method "function" is avaibable or not. But
nevertheless, is it possible to find out some errors without running the
program and starting the function?

Greetings

Michael
 
S

Sea&Gull

Michael said:
Hi,

currently I am doing my first steps with ruby. It's really great, but
sometimes I'm doing some very stupid mistakes :) I only realize this, when
running the program and starting the function, where I have done the
mistake. Is there any possibility to check the code before running it?

Try "ruby -c your_script.rb"

Also try a great tool - "irb" - which is the interactive ruby.
 
B

Bill Guindon

When I do:

@variable.function

ruby does not know, if the method "function" is avaibable or not.

actually, you can ask it...

@variable.respond_to?("function")
 
D

David A. Black

Hi --

actually, you can ask it...

@variable.respond_to?("function")

But that's a runtime thing too. I don't think you can leverage that
to do pre-runtime checking.


David
 
B

Bill Guindon

Hi --



But that's a runtime thing too. I don't think you can leverage that
to do pre-runtime checking.

Yep, but he started out mentioning a runtime problem. Just wanted to
make sure he knew he could check for the method during runtime
(triggered by "doing my first steps with ruby").
 
D

David A. Black

Hi --

Yep, but he started out mentioning a runtime problem. Just wanted to
make sure he knew he could check for the method during runtime
(triggered by "doing my first steps with ruby").

I was going by: "is it possible to find out some errors without
running the program and starting the function?" :) (But I certainly
don't mean to object to your imparting info about what he can do with
Ruby :)


David
 
R

Ryan Davis

currently I am doing my first steps with ruby. It's really great, but
sometimes I'm doing some very stupid mistakes :) I only realize this,
when
running the program and starting the function, where I have done the
mistake. Is there any possibility to check the code before running it?
Because if I do not run the function, I'll never see, that there is a
error, which is followed by a crash. The problem is, that
ruby doesn't know the types, the variables have. When I do:

@variable.function

ruby does not know, if the method "function" is avaibable or not. But
nevertheless, is it possible to find out some errors without running
the
program and starting the function?

As others have mentioned, unit testing is by far your best way to go.
Not only will it help you write more ruby code in general (increasing
your ruby-fu), but it'll prevent any problems you identify from coming
back. It is still a runtime solution, but you get to control the
environment and context. I suggest you check out ZenTest (below) to
help get you jumpstarted.
 
N

Neil Stevens

When I do:

@variable.function

ruby does not know, if the method "function" is avaibable or not. But
nevertheless, is it possible to find out some errors without running the
program and starting the function?

Not really. That's one reason that people have developed test
frameworks. Using those, you can test individual bits of code without
having to run the whole program.
 
J

John Carter

currently I am doing my first steps with ruby. It's really great, but
sometimes I'm doing some very stupid mistakes

Make an absolute habit of using
ruby -w

And during the development phase, ruby -wd is helpful.

Also create unit tests. They are a great idea.

grep -n test/unit /usr/lib/ruby/1.8/*.rb

will get you a list of examples to follow.

See /usr/lib/ruby/1.8/test/unit.rb for documentation.



John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand

Refactorers do it a little better every time.
 
B

BearItAll

Michael said:
Hi,

currently I am doing my first steps with ruby. It's really great, but
sometimes I'm doing some very stupid mistakes :) I only realize this, when
running the program and starting the function, where I have done the
mistake. Is there any possibility to check the code before running it?
Because if I do not run the function, I'll never see, that there is a
error, which is followed by a crash. The problem is, that
ruby doesn't know the types, the variables have. When I do:

@variable.function

ruby does not know, if the method "function" is avaibable or not. But
nevertheless, is it possible to find out some errors without running the
program and starting the function?

Greetings

Michael

Can I extend this thread a bit. I've read the responses, and fair enough
too.

But I think the poster, just like me, is struggling to think differently
in the ruby world when coming from other programming worlds, as I'm sure
many of you do/did.

The example he used of a function that isn't a function, simply wouldn't
get as far as an attempt to run in c++.

I'm not criticising ruby. It's just that in any form of programming,
specially in event driven envoronments, it is always going to be
possible to have untested routes through the code.

I'm a firm believer in that all functions must be tested with both good
and bad data, and must deal cleanly with both. So assume the functions
are tested and worked with good and bad data in a good way.

But it only takes the ommision of an @ or an accidental capital, when
you merge your code into one to produce a runtime error that could lay
dorment for an unspecified time before stopping your code.

So, how ever long winded it may seem to be, I don't think we have much
choice in ruby, we have to test each function, and in many cases the
data types too. It is best to get into that habit early with our small
test functions than to wait until we're inside a big project.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top