[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

RichardOnRails

7/20/2008 3:45:00 AM

Hi,

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

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
23 Answers

reuben doetsch

7/20/2008 7:06:00 AM

0

[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 <
RichardDummyMailbox58407@uscomputergurus.com> wrote:

> Hi,
>
> I put together to exercise simple Ruby constructs. It's posted at
> http://www.pastie.....
>
> 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
>
>

Siep Korteling

7/20/2008 8:36:00 PM

0

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

RichardOnRails

7/21/2008 1:27:00 PM

0

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

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.




marcman

6/5/2010 12:12:00 AM

0

On Jun 4, 7:55 pm, James Pablos <james.pab...@gmail.com> wrote:
> On Jun 4, 6:31 pm, The old geezer <J...@aol.com> wrote:
>
> > Pretty
> > amusing, huh??
>
> "Saw mom bathin' down by the river, / Knew she'd have to come up soon
> for air!"

"She don't come and I don't swallow. Tastes like chicken, when I
sneeze."

james.pablos

6/5/2010 1:24:00 AM

0

On Jun 4, 8:12 pm, marcman <marcmanstud...@gmail.com> wrote:

> "She don't come and I don't swallow. Tastes like chicken, when I
> sneeze."

"I knew right away she did not like other girls... other girls..."

marcman

6/5/2010 1:34:00 AM

0

On Jun 4, 9:23 pm, James Pablos <james.pab...@gmail.com> wrote:
> On Jun 4, 8:12 pm, marcman <marcmanstud...@gmail.com> wrote:
>
> > "She don't come and I don't swallow. Tastes like chicken, when I
> > sneeze."
>
> "I knew right away she did not like other girls... other girls..."

"A pizza place or so it looks from space. A closer look reveals a
juicy steak."

marcman

6/5/2010 1:40:00 AM

0

On Jun 4, 9:23 pm, James Pablos <james.pab...@gmail.com> wrote:
> On Jun 4, 8:12 pm, marcman <marcmanstud...@gmail.com> wrote:
>
> > "She don't come and I don't swallow. Tastes like chicken, when I
> > sneeze."
>
> "I knew right away she did not like other girls... other girls..."

"Mike and Gloria gonna be by name"

james.pablos

6/5/2010 1:59:00 AM

0

On Jun 4, 9:34 pm, marcman <marcmanstud...@gmail.com> wrote:

> "A pizza place or so it looks from space. A closer look reveals a
> juicy steak."

fuuuuuck ... it'll take me a bit to top this one...

sacha

6/5/2010 3:11:00 AM

0

On Jun 4, 7:40 pm, marcman <marcmanstud...@gmail.com> wrote:
> On Jun 4, 9:23 pm, James Pablos <james.pab...@gmail.com> wrote:
>
> > On Jun 4, 8:12 pm, marcman <marcmanstud...@gmail.com> wrote:
>
> > > "She don't come and I don't swallow. Tastes like chicken, when I
> > > sneeze."
>
> > "I knew right away she did not like other girls... other girls..."
>
> "Mike and Gloria gonna be by name"

Let's rape the women of Sparta"

If the hunter don't get you
The blind man will

Dawn is breaking everywhere
Light a candle, curse the glare
Chocolate curtains, I don't care

Another rabitt in our rears

Wedding castles thrown at me for playing in the band

You've got two good eyes but you still want three

marcman

6/5/2010 3:47:00 AM

0

On Jun 4, 11:11 pm, sacha <p.smy...@sasktel.net> wrote:
> On Jun 4, 7:40 pm, marcman <marcmanstud...@gmail.com> wrote:
>
> > On Jun 4, 9:23 pm, James Pablos <james.pab...@gmail.com> wrote:
>
> > > On Jun 4, 8:12 pm, marcman <marcmanstud...@gmail.com> wrote:
>
> > > > "She don't come and I don't swallow. Tastes like chicken, when I
> > > > sneeze."
>
> > > "I knew right away she did not like other girls... other girls..."
>
> > "Mike and Gloria gonna be by name"
>
> Let's rape the women of Sparta"
>
> If the hunter don't get you
> The blind man will
>
> Dawn is breaking everywhere
> Light a candle, curse the glare
> Chocolate curtains, I don't care
>

It must be getting early
Cops are running late
Pink Light of the morning sky
Looks so lonely

Cows giving kerosene
She can't be seventeen
The first notes are all I've seen but it's all right

I willlll get high


> Another rabitt in our rears
>
> Wedding castles thrown at me for playing in the band
>
> You've got two good eyes but you still want three

Driving that train high on cocaine
Casey Jones you better wash your sleeve

Switchman's sleeping, train oh no that Jew
Is on the wrong track and headed for you

Since it costs a lot to win and even mortal ooze

Wake up to find out that you are despised by the world

I was drinking last night with Bob Barker, and I sold him a pitcher of
juice

Like a song that sends some color all the way to Spain

Errors on me for flashing my keys out on Main Street

I cut my deck to the queen of spades but the cards were all insane

Winona Rider gonna miss me when I'm gone

If you play at dice you're gonna harvest wins