PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Container wont contain! (noob question)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Container wont contain! (noob question)

Réponse
 
LinkBack Outils de la discussion
Vieux 11/05/2008, 21h22   #1
Extreme Noob
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Container wont contain! (noob question)

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

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

  Réponse avec citation
Vieux 11/05/2008, 21h45   #2
Sandro Paganotti
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Container wont contain! (noob question)

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




On Sun, May 11, 2008 at 8:22 PM, Extreme Noob <bengalyean@hotmail.com> 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
>
> #************************************************* ******
> --
> Posted via http://www.ruby-forum.com/.
>
>




--
Go outside! The graphics are amazing!

  Réponse avec citation
Vieux 11/05/2008, 22h05   #3
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Container wont contain! (noob question)

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
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 11/05/2008, 22h05   #4
Extreme Noob
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Container wont contain! (noob question)

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


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/.

  Réponse avec citation
Vieux 11/05/2008, 22h18   #5
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Container wont contain! (noob question)

Extreme Noob wrote:
> Wow, didn't know about that =() function. Thanks for the
>
>


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.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h53.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13413 seconds with 13 queries