Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Undefined method `+'

Reply
Thread Tools

Undefined method `+'

 
 
Danny L.
Guest
Posts: n/a
 
      02-18-2011
hey guys the following line of code is producing a "undefined method
`+':

@plays += 1


cheers,

Danny

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

 
Reply With Quote
 
 
 
 
Sam Duncan
Guest
Posts: n/a
 
      02-18-2011
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
>
>


 
Reply With Quote
 
 
 
 
John Feminella
Guest
Posts: n/a
 
      02-18-2011
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
>>
>>

>
>


 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      02-18-2011
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/


 
Reply With Quote
 
John Feminella
Guest
Posts: n/a
 
      02-18-2011
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/
>
>
>


 
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
Undefine Method Only Marks the Method Undefined Su Zhang Ruby 3 04-18-2011 12:11 AM
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments