rubycookbook 
Author Message
 rubycookbook

Any news on http://www.*-*-*.com/
--
Bil


Sat, 27 Nov 2004 22:00:28 GMT  
 rubycookbook
Hi,
  I'm just trying to learn Ruby and am wondering what the proper way of
having a composite class (not a mixin).  I'd like to have one class be
an instance variable of another class.

Class X
  def myX
  End
End

Class Y
  myNest = X::new

  def myY
  end
end

myObject = Y::new

myObject::myNest.myX       #  <------  gives an error

What's the proper way of doing this in Ruby?  

Thanks,
S



Sat, 27 Nov 2004 22:10:58 GMT  
 rubycookbook
Am Dienstag, 11. Juni 2002 15:56 schrieben Sie:
Quote:
> Any news on http://www.rubycookbook.org/ availability?

if webspace is the problem, we can host it for free here,
or maybe the best would be to host it on sf.net

Ciao,
        A



Sat, 27 Nov 2004 22:21:25 GMT  
 rubycookbook

Quote:
> Hi,
>   I'm just trying to learn Ruby and am wondering what the proper
> way of having a composite class (not a mixin).  I'd like to have
> one class be an instance variable of another class.

Do you mean that you want *an instance* of one class be an instance
variable of another class? This is done all the time.

class X
        def myX
        end
end

class Y
        def initialize

        end
        attr_reader :myX
end

Quote:
> Class Y
>   myNest = X::new

This would be a local variable, not an instance variable.

--
Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE



Sat, 27 Nov 2004 22:47:27 GMT  
 rubycookbook

Quote:

> Class X
>   def myX
>   End
> End

> Class Y
>   myNest = X::new

>   def myY
>   end
> end

> myObject = Y::new

> myObject::myNest.myX       #  <------  gives an error

> What's the proper way of doing this in Ruby?  

myNest in your code isn't visable externally to the area of the class (it is I
guess a local variable).  If you want it to be a constant (in which case it
would be visible) capitalize the first character of its name.  If you want it
to be an instance variable do this:

class Y
    attr_reader :myNest
    def initialize()

    end
end

then the call that you have above should work.

-kyle

--
http://mas.cs.umass.edu/~rawlins
--
the pieces of the puzzle are waiting the water of the dark boats

  application_pgp-signature_part
< 1K Download


Sun, 28 Nov 2004 00:32:16 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Ruby-libgtop (and RubyCookbook, RAA)

2. rubycookbook.org down for indeterminate time

3. what happened to rubycookbook site?

4. rubycookbook.org

5. BOFH sample from rubycookbook

6. ANN: Ruby shard Config.rb posted at RubyCookbook

7. rubycookbook.org opens new section for code

8. possible addition to rubycookbook.org

 

 
Powered by phpBB® Forum Software