From: William Song on
How can I properly define a struct in a class?

If I have

class Cards
Card = Struct.new(:suit, :number)
@cardsOnHand=[]

@cardsOnHand.push(Card.new("S","5"))
end

This will give me an error message saying the push is not defined for
NilClass. How should I fix this?
--
Posted via http://www.ruby-forum.com/.

From: Rein Henrichs on
On 2010-06-07 15:16:33 -0700, William Song said:

> How can I properly define a struct in a class?
>
> If I have
>
> class Cards
> Card = Struct.new(:suit, :number)
> @cardsOnHand=[]
>
> @cardsOnHand.push(Card.new("S","5"))
> end
>
> This will give me an error message saying the push is not defined for
> NilClass. How should I fix this?

The error you reported has nothing to do with your Card struct. Besides
which, your code works for me as is.

--
Rein Henrichs
http://puppetlabs.com
http://reinh.com

From: William Song on
Rein Henrichs wrote:
> On 2010-06-07 15:16:33 -0700, William Song said:
>
>>
>> This will give me an error message saying the push is not defined for
>> NilClass. How should I fix this?
>
> The error you reported has nothing to do with your Card struct. Besides
> which, your code works for me as is.

Sorry, I meant to put it into a function. I over-simplified it when
posted here.

class Cards
Card = Struct.new(:suit, :number)
@cardsOnHand=[]

def a()
@cardsOnHand.push(Card.new("S","5"))
end
end

cards = Cards.new
cards.a()

The above code will give an error.
--
Posted via http://www.ruby-forum.com/.

From: Rein Henrichs on
On 2010-06-07 16:16:57 -0700, William Song said:

> Rein Henrichs wrote:
>> On 2010-06-07 15:16:33 -0700, William Song said:
>>
>>>
>>> This will give me an error message saying the push is not defined for
>>> NilClass. How should I fix this?
>>
>> The error you reported has nothing to do with your Card struct. Besides
>> which, your code works for me as is.
>
> Sorry, I meant to put it into a function. I over-simplified it when
> posted here.
>
> class Cards
> Card = Struct.new(:suit, :number)
> @cardsOnHand=[]
>
> def a()
> @cardsOnHand.push(Card.new("S","5"))
> end
> end
>
> cards = Cards.new
> cards.a()
>
> The above code will give an error.

You create an instance variable, @cardsOnHand, in the class scope. That
means it is an instance variable of the object Cards (the class). You
then attempt to access it in the scope of an instance method, i.e. on a
Cards instance. They are two different objects, two different scopes
and two different variables.

I would suggest reading the chapter Classes, Objects and Variables in
http://ruby-doc.org/docs/ProgrammingRuby to better understand the
concept of scope in Ruby and how it effects variables. Hint: you
probably want to set this instance variable when you *initialize* an
Cards object.

I would also suggest reading
http://github.com/chneukirchen/styleguide/blob/master/RUBY-STYLE to get
a better grasp of common Ruby style, such as two space indention,
adding spacing before and after the = in a variable assignment, using
camel_case for variable and method names, and removing empty () on the
ends of methods.

--
Rein Henrichs
http://puppetlabs.com
http://reinh.com

From: Angus Hammond on
I assume you want the first two lines of you class definition to be run
every time someone creates an instance of you class. If you do you
should put the into a method called initialize. That will be run
automatically when new is called and should solve your problem

On 08/06/10 00:16, William Song wrote:
> Rein Henrichs wrote:
>
>> On 2010-06-07 15:16:33 -0700, William Song said:
>>
>>
>>> This will give me an error message saying the push is not defined for
>>> NilClass. How should I fix this?
>>>
>> The error you reported has nothing to do with your Card struct. Besides
>> which, your code works for me as is.
>>
> Sorry, I meant to put it into a function. I over-simplified it when
> posted here.
>
> class Cards
> Card = Struct.new(:suit, :number)
> @cardsOnHand=[]
>
> def a()
> @cardsOnHand.push(Card.new("S","5"))
> end
> end
>
> cards = Cards.new
> cards.a()
>
> The above code will give an error.
>