Ah...thanks!

It seems like I've been using ruby for six years without ever
realizing that Enumerable.count behaved differently than
Enumerable.length or Enumerable.size. Those nifty enumerators usually
make it unnecessary to deal with the index at all.

I'll just switch to Enumerable.size for now. That will give me an
argument error if I'll ever drop that space again. :)

Thanks again for enlightening me!

/lasso



On 19 mar, 12:39, Chris Kottom <ch... / chriskottom.com> wrote:
> [Note: =A0parts of this message were removed to make it a legal post.]
>
> Removing the space causes the minus to be interpreted as a unary negative
> operator rather than the binary minus that you get with the space. =A0The
> unary operator has a relatively higher precedence than the binary minus a=
nd
> more importantly than the dot operator used for invoking #count on your
> array, so it changes the expression evaluation completely -- first,
> evaluating the unary negative operator on 1, and then passing the result =
as
> an argument to #count. =A0Because Array#count can take an argument that s=
ays,
> "count how many of this thing I have in my array", you get 0 as your outp=
ut
> because your array has no -1 elements. =A0See how the result changes belo=
w
> when I begin adding -1's to my array.
>
> ruby-1.9.2-p0 :001 > elems =3D ["a", "b", "c"]
> =A0=3D> ["a", "b", "c"]
> ruby-1.9.2-p0 :002 > elems.count - 1
> =A0=3D> 2
> ruby-1.9.2-p0 :003 > elems.count -1
> =A0=3D> 0
> ruby-1.9.2-p0 :004 > elems << -1
> =A0=3D> ["a", "b", "c", -1]
> ruby-1.9.2-p0 :005 > elems.count -1
> =A0=3D> 1
> ruby-1.9.2-p0 :006 > elems << -1
> =A0=3D> ["a", "b", "c", -1, -1]
> ruby-1.9.2-p0 :007 > elems.count -1
> =A0=3D> 2
>
> On Sat, Mar 19, 2011 at 12:13 PM, Lars Olsson <la... / lassoweb.se> wrote:
> > Hi,
>
> > Yesterday I introduced a bug in one of my scripts after accidentally
> > deleting av space. It took me quite I while to find where the bug was,
> > but I still can't understand *why* the missing space leads to a
> > different result. Anyone out there that can explain it to me?
>
> > lasso@lasso-laptop:~$ ruby -v
> > ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
>
> > lasso@lasso-laptop:~$ irb
> > irb(main):001:0> elems =3D ['foo', 'bar', 'baz']
> > =3D> ["foo", "bar", "baz"]
> > irb(main):002:0> puts elems.count -1
> > 0
> > =3D> nil
> > irb(main):003:0> puts elems.count - 1
> > 2
> > =3D> nil
>
> > How does the missing space between the minus sign and the number 1
> > change the expression?
>
> > Any answers appreciated
>
> > /lasso
>
>