Ruby Source code checker

Hi,

currently I am doing my first steps with ruby. It's really great, but
sometimes I'm doing some very stupid mistakes :slight_smile: 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

Michael Gebhart wrote:

mistake. Is there any possibility to check the code before running it?

try:

ruby -c yourfile.rb

regards,
George.

Michael Gebhart wrote:

Hi,

currently I am doing my first steps with ruby. It's really great, but
sometimes I'm doing some very stupid mistakes :slight_smile: 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.

路路路

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&g

actually, you can ask it...

@variable.respond_to?("function")

路路路

On Tue, 15 Feb 2005 23:04:57 +0900, Michael Gebhart <mail@miketech.net> wrote:

When I do:

@variable.function

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

--
Bill Guindon (aka aGorilla)

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.

路路路

On Feb 15, 2005, at 6:04 AM, Michael Gebhart wrote:

currently I am doing my first steps with ruby. It's really great, but
sometimes I'm doing some very stupid mistakes :slight_smile: 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?

--
ryand-ruby@zenspider.com - http://blog.zenspider.com/
http://rubyforge.org/projects/zentest/
Seattle.rb | Home

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.

路路路

On Tue, 15 Feb 2005 14:57:32 +0100, Michael Gebhart wrote:

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?

--
Neil Stevens - neil@hakubi.us

'A republic, if you can keep it.' -- Benjamin Franklin

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 : john.carter@tait.co.nz
New Zealand

Refactorers do it a little better every time.

路路路

On Tue, 15 Feb 2005, Michael Gebhart wrote:

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

George Moschovitis <george.moschovitis@gmail.com> writes:

Michael Gebhart wrote:

mistake. Is there any possibility to check the code before running it?

try:

ruby -c yourfile.rb

This answer is a bit misleading, ruby -c will only check the file for
syntactic correctness (which happens during parsing, anyway).

In general, there is no way of telling a method-lookup at runtime will
work, as Ruby is dynamically typed. Use unit-tests to ensure the
correctness of your methods.

Also, ruby -w may be useful to show up likely-to-be-problematic things
as uninitalized instance variables etc.

路路路

regards,
George.

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Hi --

路路路

On Wed, 16 Feb 2005, Bill Guindon wrote:

On Tue, 15 Feb 2005 23:04:57 +0900, Michael Gebhart <mail@miketech.net> wrote:

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")

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

David

--
David A. Black
dblack@wobblini.net

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").

路路路

On Wed, 16 Feb 2005 01:57:23 +0900, David A. Black <dblack@wobblini.net> wrote:

Hi --

On Wed, 16 Feb 2005, Bill Guindon wrote:

> On Tue, 15 Feb 2005 23:04:57 +0900, Michael Gebhart <mail@miketech.net> wrote:
>
>> 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")

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

--
Bill Guindon (aka aGorilla)

Hi --

路路路

On Wed, 16 Feb 2005, Bill Guindon wrote:

On Wed, 16 Feb 2005 01:57:23 +0900, David A. Black <dblack@wobblini.net> wrote:

Hi --

On Wed, 16 Feb 2005, Bill Guindon wrote:

On Tue, 15 Feb 2005 23:04:57 +0900, Michael Gebhart <mail@miketech.net> wrote:

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")

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").

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

David

--
David A. Black
dblack@wobblini.net