Google Groups Home
Help | Sign in
Container wont contain! (noob question)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Extreme Noob  
View profile
 More options May 11, 4:22 pm
Newsgroups: comp.lang.ruby
From: Extreme Noob <bengaly...@hotmail.com>
Date: Sun, 11 May 2008 15:22:21 -0500
Local: Sun, May 11 2008 4:22 pm
Subject: 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/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sandro Paganotti  
View profile
 More options May 11, 4:45 pm
Newsgroups: comp.lang.ruby
From: Sandro Paganotti <sandro.pagano...@gmail.com>
Date: Sun, 11 May 2008 15:45:11 -0500
Local: Sun, May 11 2008 4:45 pm
Subject: 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

--
Go outside! The graphics are amazing!

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7stud --  
View profile
 More options May 11, 5:05 pm
Newsgroups: comp.lang.ruby
From: 7stud -- <bbxx789_0...@yahoo.com>
Date: Sun, 11 May 2008 16:05:12 -0500
Local: Sun, May 11 2008 5:05 pm
Subject: Re: Container wont contain! (noob question)

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Extreme Noob  
View profile
 More options May 11, 5:05 pm
Newsgroups: comp.lang.ruby
From: Extreme Noob <bengaly...@hotmail.com>
Date: Sun, 11 May 2008 16:05:48 -0500
Local: Sun, May 11 2008 5:05 pm
Subject: Re: Container wont contain! (noob question)
Wow, didn't know about that =() function. Thanks for the help

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7stud --  
View profile
 More options May 11, 5:18 pm
Newsgroups: comp.lang.ruby
From: 7stud -- <bbxx789_0...@yahoo.com>
Date: Sun, 11 May 2008 16:18:06 -0500
Local: Sun, May 11 2008 5:18 pm
Subject: Re: Container wont contain! (noob question)

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google