(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
#*******************************************************
--
Posted via http://www.ruby-forum.com/.
def change_me=(val)
@m_container.container1 = Class1.new(val)
end
and then call
object.change_me = 2
Sandro
--
Go outside! The graphics are amazing!
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
Sandro Paganotti 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
--
Posted via http://www.ruby-forum.com/.
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
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.