From: Arlen Christian Mart Cuss Date: 2012-11-29T05:16:37+09:00 Subject: Re: class method defined, still undefinded method exeption --0016e6d9a35411af0c04cf93c069 Content-Type: text/plain; charset=UTF-8 Look carefully at the backtrace: ./video.rb:11:in `initialize': undefined method `screen' for RA:Class (NoMethodError) from ./game.rb:32:in `new' from ./game.rb:32:in `initialize' from ./resourceaggregator.rb:10:in `new' from ./resourceaggregator.rb:10 from ./main.rb:3:in `require' from ./main.rb:3 Notice this line: from ./resourceaggregator.rb:10:in `new' It hasn't finished loading RA yet when it calls VideoManager.new; which, itself, expects RA to be fully defined. As a workaround, you could wrap up all the class variable definitions in an "init" method, called after the class is defined. Or just move them to the bottom of the class. On 29/11/2012 7:01 AM, "Michael Sas" wrote: > Hi > > i try to untangle tightly coupled classes. To that end theres a class to > hold all resources that are needed by multiple other classes. Its called > RA for Resource Aggregator. > > Moving one central object to that class broke the programm. Here's the > output: > > ./video.rb:11:in `initialize': undefined method `screen' for RA:Class > (NoMethodError) > from ./game.rb:32:in `new' > from ./game.rb:32:in `initialize' > from ./resourceaggregator.rb:10:in `new' > from ./resourceaggregator.rb:10 > from ./main.rb:3:in `require' > from ./main.rb:3 > > RA is pretty big, so i ommit most Methods. Its intended to be a utility > class and has no custom initialize: > > class RA > @@xRes = 1280 > @@yRes = 800 > @@colordepth = 16 > @@screen = SDL.setVideoMode(@@xRes, @@yRes, @@colordepth, > SDL::SRCCOLORKEY) #SDL::FULLSCREEN > @@game = Game.new > @@videoManager = VideoManager.new > ... > def self.screen > @@screen > end > end > > Here is initialisite from VideoManager > > def initialize > @screen = RA.screen > @xOffset = 0 > @yOffset = 0 > end > > -- > Posted via http://www.ruby-forum.com/. > > --0016e6d9a35411af0c04cf93c069 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Look carefully at the backtrace:

./video.rb:11:in `initialize': undefined method `screen&= #39; for RA:Class
(NoMethodError)
=C2=A0 from ./game.rb:32:in `new'
=C2=A0 from ./game.rb:32:in `initialize'
=C2=A0 from ./resourceaggregator.rb:10:in `new'
=C2=A0 from ./resourceaggregator.rb:10
=C2=A0 from ./main.rb:3:in `require'
=C2=A0 from ./main.rb:3

Notice this line:

from ./resourceaggregator.rb:10:in `new'

It hasn't finished loading RA yet when it calls VideoMan= ager.new; which, itself, expects RA to be fully defined.

As a workaround, you could wrap up all the class variable de= finitions in an "init" method, called after the class is defined.= Or just move them to the bottom of the class.

On 29/11/2012 7:01 AM, "Michael Sas" &= lt;lists@ruby-forum.com> wro= te:
Hi

i try to untangle tightly coupled classes. To that end theres a class to hold all resources that are needed by multiple other classes. Its called RA for Resource Aggregator.

Moving one central object to that class broke the programm. Here's the<= br> output:

./video.rb:11:in `initialize': undefined method `screen' for RA:Cla= ss
(NoMethodError)
=C2=A0 from ./game.rb:32:in `new'
=C2=A0 from ./game.rb:32:in `initialize'
=C2=A0 from ./resourceaggregator.rb:10:in `new'
=C2=A0 from ./resourceaggregator.rb:10
=C2=A0 from ./main.rb:3:in `require'
=C2=A0 from ./main.rb:3

RA is pretty big, so i ommit most Methods. Its intended to be a utility
class and has no custom initialize:

class RA
=C2=A0 @@xRes =3D 1280
=C2=A0 @@yRes =3D 800
=C2=A0 @@colordepth =3D 16
=C2=A0 @@screen =3D SDL.setVideoMode(@@xRes, @@yRes, @@colordepth,
SDL::SRCCOLORKEY) #SDL::FULLSCREEN
=C2=A0 @@game =3D Game.new
=C2=A0 @@videoManager =3D VideoManager.new
=C2=A0 ...
=C2=A0 def self.screen
=C2=A0 =C2=A0 @@screen
=C2=A0 end
end

Here is initialisite from VideoManager

def initialize
=C2=A0 @screen =3D RA.screen
=C2=A0 @xOffset =3D 0
=C2=A0 @yOffset =3D 0
end

--
Posted via http://= www.ruby-forum.com/.

--0016e6d9a35411af0c04cf93c069--