Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Simple program with a simple(?) error -- what's wrong?

Reply
Thread Tools

Simple program with a simple(?) error -- what's wrong?

 
 
RichardOnRails
Guest
Posts: n/a
 
      07-20-2008
Hi,

I put together to exercise simple Ruby constructs. It's posted at
http://www.pastie.org/237288.

Before I introduced the "indx" variable it worked fine. The statement
incrementing indx in the def causes the error:
undefined method `+' for nil:NilClass (NoMethodError)

I'm running:
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Why doesn't the def use the instance of indx defined before the def?

Thanks in Advance,
Richard
 
Reply With Quote
 
 
 
 
reuben doetsch
Guest
Posts: n/a
 
      07-20-2008
[Note: parts of this message were removed to make it a legal post.]

The scope of indx is wrong, when you call it outside the function, it is
only accesible witin that scope, the outer scope and so you have you do this

def foo
indx=0
....
end

that will then work, you only need to call foo someplace if you want foo to
run. You only define it, creating the function does not call it.

On Sat, Jul 19, 2008 at 10:39 PM, RichardOnRails <
> wrote:

> Hi,
>
> I put together to exercise simple Ruby constructs. It's posted at
> http://www.pastie.org/237288.
>
> Before I introduced the "indx" variable it worked fine. The statement
> incrementing indx in the def causes the error:
> undefined method `+' for nil:NilClass (NoMethodError)
>
> I'm running:
> ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
>
> Why doesn't the def use the instance of indx defined before the def?
>
> Thanks in Advance,
> Richard
>
>


 
Reply With Quote
 
 
 
 
Siep Korteling
Guest
Posts: n/a
 
      07-20-2008
reuben doetsch wrote:
> The scope of indx is wrong, when you call it outside the function, it is
> only accesible witin that scope, the outer scope and so you have you do
> this
>
> def foo
> indx=0
> ....
> end
>

(...)
Or replace "indx" with "@indx".
Regards,
Siep

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
RichardOnRails
Guest
Posts: n/a
 
      07-21-2008
On Jul 20, 4:35 pm, Siep Korteling <s.kortel...@gmail.com> wrote:
> reuben doetsch wrote:
> > The scope of indx is wrong, when you call it outside the function, it is
> > only accesible witin that scope, the outer scope and so you have you do
> > this

>
> > def foo
> > indx=0
> > ....
> > end

>
> (...)
> Or replace "indx" with "@indx".
> Regards,
> Siep
>
> --
> Posted viahttp://www.ruby-forum.com/.


Thanks Reuben and Siep for you responses.

The following worked perfectly for creating sequentially numbered
lines: as "def" was invoked successively with various arguments:

@indx=0
def disp(s)
@index += 1
printf( [snip]

However, "index=0" inside the def, despite subsequent incrementing,
had the dual faults of assigning the same index to every line and
being inaccessible outside the "def".

I had also tried "if !defined?(indx)" somehow, but that was useless
across multiple invocations of "def".

Again, thanks to you both for your responses.




 
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
simple program error when it runs jack Java 5 04-08-2008 08:14 PM
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
looking for a simple way to load a program from another python program.. Eric_Dexter@msn.com Python 10 08-15-2006 05:00 AM
Simple simple program error...please help tasheeta@gmail.com C++ 14 11-02-2005 12:52 PM
A few simple problems in a simple program. jmac@berkeley.edu C Programming 7 07-23-2003 09:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57