Undefined method `+'

Discussion in 'Ruby' started by Danny L., Feb 18, 2011.

  1. Danny L.

    Danny L. Guest

    hey guys the following line of code is producing a "undefined method
    `+':

    @plays += 1


    cheers,

    Danny

    --
    Posted via http://www.ruby-forum.com/.
    Danny L., Feb 18, 2011
    #1
    1. Advertising

  2. Danny L.

    Sam Duncan Guest

    What is the content of @plays?

    Sam


    On 18/02/11 17:01, Danny L. wrote:
    > hey guys the following line of code is producing a "undefined method
    > `+':
    >
    > @plays += 1
    >
    >
    > cheers,
    >
    > Danny
    >
    >
    Sam Duncan, Feb 18, 2011
    #2
    1. Advertising

  3. Is it an "undefined method '+' on NilClass"? If so, you forgot to
    assign a value to @plays before incrementing it. You might try
    something like this instead:

    def plays
    @plays ||=3D 0
    end

    def plays=3D(v)
    @plays =3D self.plays + 1
    end

    ~ jf
    --
    John Feminella
    Principal Consultant, BitsBuilder
    LI: http://www.linkedin.com/in/johnxf
    SO: http://stackoverflow.com/users/75170/



    On Thu, Feb 17, 2011 at 23:04, Sam Duncan <> wrote:
    > What is the content of @plays?
    >
    > Sam
    >
    >
    > On 18/02/11 17:01, Danny L. wrote:
    >>
    >> hey guys the following line of code is producing a "undefined method
    >> `+':
    >>
    >> =C2=A0@plays +=3D 1
    >>
    >>
    >> cheers,
    >>
    >> Danny
    >>
    >>

    >
    >
    John Feminella, Feb 18, 2011
    #3
  4. On Feb 18, 2011, at 7:57 AM, John Feminella wrote:

    > Is it an "undefined method '+' on NilClass"? If so, you forgot to
    > assign a value to @plays before incrementing it. You might try
    > something like this instead:
    >
    > def plays
    > @plays ||= 0
    > end
    >
    > def plays=(v)
    > @plays = self.plays + 1
    > end
    >
    > ~ jf
    > --
    > John Feminella
    > Principal Consultant, BitsBuilder
    > LI: http://www.linkedin.com/in/johnxf
    > SO: http://stackoverflow.com/users/75170/


    I think you'd want:

    def plays=(v)
    @plays = v
    end

    In your original:

    plays = 5
    puts plays
    1 # <== ???

    I can't imagine that's what the OP wants to see.



    >
    >
    >
    > On Thu, Feb 17, 2011 at 23:04, Sam Duncan <>
    > wrote:
    >> What is the content of @plays?
    >>
    >> Sam
    >>
    >>
    >> On 18/02/11 17:01, Danny L. wrote:
    >>>
    >>> hey guys the following line of code is producing a "undefined method
    >>> `+':
    >>>
    >>> @plays += 1
    >>>
    >>>
    >>> cheers,
    >>>
    >>> Danny
    >>>
    >>>

    >>
    >>

    >


    Rob Biedenharn
    http://AgileConsultingLLC.com/
    http://GaslightSoftware.com/
    Rob Biedenharn, Feb 18, 2011
    #4
  5. Whoops, that's what I get for copy-pasting into the wrong segment. You
    are correct, Rob.

    ~ jf
    --
    John Feminella
    Principal Consultant, BitsBuilder
    LI: http://www.linkedin.com/in/johnxf
    SO: http://stackoverflow.com/users/75170/



    On Fri, Feb 18, 2011 at 10:29, Rob Biedenharn
    <> wrote:
    > On Feb 18, 2011, at 7:57 AM, John Feminella wrote:
    >
    >> Is it an "undefined method '+' on NilClass"? If so, you forgot to
    >> assign a value to @plays before incrementing it. You might try
    >> something like this instead:
    >>
    >> def plays
    >> =C2=A0@plays ||=3D 0
    >> end
    >>
    >> def plays=3D(v)
    >> =C2=A0@plays =3D self.plays + 1
    >> end
    >>
    >> ~ jf
    >> --
    >> John Feminella
    >> Principal Consultant, BitsBuilder
    >> LI: http://www.linkedin.com/in/johnxf
    >> SO: http://stackoverflow.com/users/75170/

    >
    > I think you'd want:
    >
    > def plays=3D(v)
    > =C2=A0@plays =3D v
    > end
    >
    > In your original:
    >
    > plays =3D 5
    > puts plays
    > 1 =C2=A0# <=3D=3D ???
    >
    > I can't imagine that's what the OP wants to see.
    >
    >
    >
    >>
    >>
    >>
    >> On Thu, Feb 17, 2011 at 23:04, Sam Duncan <> wrote:
    >>>
    >>> What is the content of @plays?
    >>>
    >>> Sam
    >>>
    >>>
    >>> On 18/02/11 17:01, Danny L. wrote:
    >>>>
    >>>> hey guys the following line of code is producing a "undefined method
    >>>> `+':
    >>>>
    >>>> =C2=A0@plays +=3D 1
    >>>>
    >>>>
    >>>> cheers,
    >>>>
    >>>> Danny
    >>>>
    >>>>
    >>>
    >>>

    >>

    >
    > Rob Biedenharn
    > =C2=A0 =C2=A0 =C2=A0http://AgileConsultingLLC.=

    com/
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0http://GaslightSoftwa=

    re.com/
    >
    >
    >
    John Feminella, Feb 18, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor
    Replies:
    70
    Views:
    1,721
    Dan Pop
    Feb 17, 2004
  2. Kyung won Cheon
    Replies:
    0
    Views:
    181
    Kyung won Cheon
    Nov 21, 2008
  3. Su Zhang
    Replies:
    3
    Views:
    200
    Su Zhang
    Apr 18, 2011
  4. VK
    Replies:
    45
    Views:
    572
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    355
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page