Toby Hutton <thutton / vet.com.au> wrote:
> Hello, I have another potential parsing bug (or of course I don't
> understand what I'm doing).
> 
> I have a file with a section that contains a string count and then a
> bunch of null terminated strings one after the other.  I'm reading
> them all one by one and putting them in an array.
> 
> ((fp.read 4).unpack "l")[0].times do
>   names.push (fp.gets "\0").chop       # <- line 39
> end
> 
> Produces:
> ./parseBlend.rb:39: private method `chop' called for ["*next\000"]:Array (NameError)
>         from ./parseBlend.rb:38:in `times'
>         from ./parseBlend.rb:38
> 
> "*next" is the first string in the file.  But:
> 
> ((fp.read 4).unpack "l")[0].times do
>   names.push fp.gets("\0").chop
> end
> 
> Works fine.  The distinction is (fp.gets "\0").chop
> vs. fp.gets("\0").chop.  Shouldn't they both work?

The difference is in the push method call.

names.push (fp.gets "\0").chop # == names.push(fp.gets "\0").chop
names.push fp.gets("\0").chop # == names.push(fp.gets("\0").chop)

I don't really like it myself, and have problems with it all the time, but 
method grouping via parentheses  works such that "method (stuff)" is 
interpreted to mean "call method with arguments stuff" despite there being a 
space.  I just use parentheses on nearly everything myself to work around 
this and remain consistent (^_^)

-- 
 Brian Fundakowski Feldman           \  FreeBSD: The Power to Serve!  /
 green / FreeBSD.org                    `------------------------------'