# Modify Class Instance Inside Another Class (scope problem?)

**URL:** <https://www.ruby-forum.com/t/modify-class-instance-inside-another-class-scope-problem/188228>\
**Category:** Ruby\
**Created:** [May 8, 2010, 1:49am UTC](https://www.ruby-forum.com/t/modify-class-instance-inside-another-class-scope-problem/188228 "2010-05-08T01:49:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon-torres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c2a13f/32.png) [@leon-torres](https://www.ruby-forum.com/u/leon-torres)\
**Post date:** [May 8, 2010, 1:49am UTC](https://www.ruby-forum.com/t/modify-class-instance-inside-another-class-scope-problem/188228/1 "2010-05-08T01:49:25Z")

</div>

First, I’ve not used a forum before, but I want to be more involved.  
Please excuse (and correct) poor etiquette.

The context for what I’m really trying to do is that I want to make card  
games. I have general purpose classes for “card” objects (Card, Deck,  
Shoe). And I have game specific classes, which may need to modify  
attributes of the card objects. For example, each card has ‘suit’,  
‘rank’, and ‘value’ - the ‘value’ of Jack in Black Jack is 10, but in  
solitaire it’s 11.

I’ve created this simplified version of the code to focus on this  
problem.

Problem: Game.cards is the same before and after Game.assign\_values (no  
values assigned).

My questions are:

1. Why doesn’t this work the way I’m trying to do it?
2. Should I be using a different approach?

class Card  
attr\_accessor :name, :value  
def initialize(name)  
@name = name  
@value = nil  
end  
end

class Game  
attr\_accessor :cards  
NAMES = [‘a’,‘b’,‘c’,‘d’]  
NAME\_VALUES = { ‘a’ =\> 1,‘b’ =\> 2,‘c’ =\> 3,‘d’ =\> 4 }  
def initialize  
@cards= self.cards  
end

def cards  
cards = []  
NAMES.each do |n|  
card = Card.new(n)  
cards \<\< card  
end  
cards  
end

def assign\_values  
@cards.each do |card|  
name = card.name  
value = NAME\_VALUES[name]  
card.value = value  
end  
end  
end

g = Game.new  
p g.cards  
g.assign\_values  
p g.cards

---

<div class="post-metadata">

**Author:** ![leon-torres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c2a13f/32.png) [@leon-torres](https://www.ruby-forum.com/u/leon-torres)\
**Post date:** [May 8, 2010, 6:12am UTC](https://www.ruby-forum.com/t/modify-class-instance-inside-another-class-scope-problem/188228/2 "2010-05-08T06:12:00Z")

</div>

On Fri, May 7, 2010 at 6:49 PM, Leon T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## I’ve created this simplified version of the code to focus on this attr\_accessor :name, :value def initialize end g = Game.new p g.cards g.assign\_values p g.cards
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

You have an instance method called cards which returns an initialized  
set of  
cards. When you say “p g.cards” at the end, you are calling this method,  
so  
you are simply calling this method, so you are getting the returned  
value of  
Game#cards, the value returned by the cards method that you have  
defined.  
However, your @cards instance variable is initialized when you say  
g.assign\_values, but that isn’t what you are printing out.

To see the difference:

g = Game.new  
p g.cards  
g.assign\_values  
p g.cards  
p g.instance\_variable\_get(“@cards”)

---

<div class="post-metadata">

**Author:** ![leon-torres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c2a13f/32.png) [@leon-torres](https://www.ruby-forum.com/u/leon-torres)\
**Post date:** [May 8, 2010, 4:14pm UTC](https://www.ruby-forum.com/t/modify-class-instance-inside-another-class-scope-problem/188228/3 "2010-05-08T16:14:31Z")

</div>

Josh C. wrote:

> You have an instance method called cards which returns an initialized  
> set of  
> cards. When you say “p g.cards” at the end, you are calling this method,  
> so  
> you are simply calling this method, so you are getting the returned  
> value of  
> Game#cards, the value returned by the cards method that you have  
> defined.  
> However, your @cards instance variable is initialized when you say  
> g.assign\_values, but that isn’t what you are printing out.
> 
> To see the difference:
> 
> g = Game.new  
> p g.cards  
> g.assign\_values  
> p g.cards  
> p g.instance\_variable\_get("@cards")

So basically, my method was working as intended, but I was looking for  
the result in the wrong place. Seems like an obvious mistake (name  
conflict) to avoid now.

Thank you!
