# Container wont contain! (noob question)

**URL:** <https://www.ruby-forum.com/t/container-wont-contain-noob-question/137315>\
**Category:** Ruby\
**Created:** [May 11, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/container-wont-contain-noob-question/137315 "2008-05-11T22:22:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [May 11, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/container-wont-contain-noob-question/137315/1 "2008-05-11T22:22:10Z")

</div>

Here I want to change Class1’s var2 to equal 2. Now, I can do this by…  
“object.m\_container.container1 = Class1.new(2)”, but I want to do it  
with “object.change\_me = 2”  
Point is I want to change that variable by simply using a number, as  
apposed to xxx.new(arg).  
Why wont object.change\_me =2 work?

(sorry if this seems confusing or silly)

The following is a highly simplified representation of my current  
problem…

#\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

class Class1

```
def initialize(aVar)
    @var = aVar
    @var2 = nil
    if @var == 1 then @var2 = 1 elsif @var == 2 then @var2 = 2 end
end

```

end

class Class2  
attr\_accessor :container1

```
  def initialize(container1)
    @container1 = container1
       end

```

end

class M\_class  
attr\_accessor(:m\_container,:change\_me)

def initialize  
@m\_container = Class2.new(Class1.new(change\_me) )  
end

```
    def change_me
           @change_me = 1
    end

```

end

object = M\_class.new

#object.m\_container.container1 = Class1.new(2) \<— currently doing it  
like this

#object.change\_me = 2 \<----- want to do it like this (or something as  
easy)

p object

#\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

---

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [May 11, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/container-wont-contain-noob-question/137315/2 "2008-05-11T22:46:29Z")

</div>

MMH… you can try add this function to M\_class

```
   def change_me=(val)
          @m_container.container1 = Class1.new(val)
   end

```

and then call

object.change\_me = 2

Sandro

---

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [May 11, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/container-wont-contain-noob-question/137315/3 "2008-05-11T23:05:08Z")

</div>

Extreme Noob wrote:

> Here I want to change Class1’s var2 to equal 2. Now, I can do this by…  
> “object.m\_container.container1 = Class1.new(2)”, but I want to do it  
> with “object.change\_me = 2”  
> Point is I want to change that variable by simply using a number, as  
> apposed to xxx.new(arg).  
> Why wont object.change\_me =2 work?
> 
> (sorry if this seems confusing or silly)
> 
> The following is a highly simplified representation of my current  
> problem…
> 
> #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> class Class1
> 
> ```
> def initialize(aVar)
> @var = aVar
> @var2 = nil
> if @var == 1 then @var2 = 1 elsif @var == 2 then @var2 = 2 end
> end
> 
> ```
> 
> end
> 
> class Class2  
> attr\_accessor :container1
> 
> ```
> def initialize(container1)
> @container1 = container1
> end
> 
> ```
> 
> end
> 
> class M\_class  
> attr\_accessor(:m\_container,:change\_me)
> 
> def initialize  
> @m\_container = Class2.new(Class1.new(change\_me) )  
> end
> 
> ```
> def change_me
> @change_me = 1
> end
> 
> ```
> 
> end
> 
> object = M\_class.new
> 
> #object.m\_container.container1 = Class1.new(2) \<— currently doing it  
> like this
> 
> #object.change\_me = 2 \<----- want to do it like this (or something as  
> easy)
> 
> p object
> 
> #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

That’s pretty confusing. How about this:

class Class1  
attr\_accessor :var2

def initialize(aVar)  
@var = aVar  
@var2 = nil

```
if @var == 1
  @var2 = 1
elsif @var == 2
  @var2 = 2
end

```

end

end

class Class2  
attr\_accessor :container1

def initialize(container1)  
@container1 = container1  
end

end

class M\_class  
attr\_accessor :m\_container

def initialize  
@m\_container = Class2.new(Class1.new(1) )  
end

def set\_var2=(val)  
@m\_container.container1.var2 = val  
end

def get\_var2  
@m\_container.container1.var2  
end

end

obj = M\_class.new  
obj.set\_var2 = 2

puts obj.get\_var2

---

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [May 11, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/container-wont-contain-noob-question/137315/4 "2008-05-11T23:05:44Z")

</div>

Wow, didn’t know about that =() function. Thanks for the help

Sandro P. wrote:

> MMH… you can try add this function to M\_class
> 
> ```
> def change_me=(val)
> @m_container.container1 = Class1.new(val)
> end
> 
> ```
> 
> and then call
> 
> object.change\_me = 2
> 
> Sandro

---

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [May 11, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/container-wont-contain-noob-question/137315/5 "2008-05-11T23:18:06Z")

</div>

Extreme Noob wrote:

> Wow, didn’t know about that =() function. Thanks for the help

Look at this class:

class Dog  
def initialize(name)  
@name = name  
end

def name=(a\_name)  
@name = a\_name  
end

def name  
return @name  
end  
end

d = Dog.new(“Spot”)  
puts d.name

d.name = “Rover”  
puts d.name

The Dog class above is equivalent to:

class Dog  
attr\_accessor :name

def initialize(name)  
@name = name  
end

end

In other words, the line:

attr\_accessor :name

creates the methods “name=” and “name” for you.
