[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How can I access value of an instances

mepython

3/1/2005 8:36:00 PM

can I do this without passing instance a


class A
def initialize
@varA = 30
end
end

class B
def meth_b
@varB = a.varA ##### How can I do this? ##########
end
end

a = A.new
b = B.new


Thanks in advance

8 Answers

linus sellberg

3/1/2005 8:58:00 PM

0

mepython wrote:
> can I do this without passing instance a

class A
attr_reader :varA
def initialize
@varA = 30
end
end

The methods attr_accessor and attr_writer might be good to know about as
well.

mepython

3/1/2005 9:25:00 PM

0

problem is not accessing attributes, it is accessing instance, This is
error I get:

test.rb:12:in `meth_b': undefined local variable or method `a' for
#<B:0xb7d7ca98> (NameError)
from test.rb:21
****************************************************************************

class A
attr_reader :varA
def initialize
@varA = 30
end
end

class B
def meth_b
@varB = a.varA ##### How can I do this? ##########
puts @varB
end
end

a = A.new
b = B.new
b.meth_b

Robert Klemme

3/1/2005 10:09:00 PM

0


"mepython" <a@agni.us> schrieb im Newsbeitrag
news:1109712275.957942.264920@l41g2000cwc.googlegroups.com...
> problem is not accessing attributes, it is accessing instance, This is
> error I get:
>
> test.rb:12:in `meth_b': undefined local variable or method `a' for
> #<B:0xb7d7ca98> (NameError)
> from test.rb:21

Of course. How do you expect your Ruby interpreter to know which instance
you want to use? There's certainly a lot magic in Ruby - but not that much.
Btw other programming languages cannot do that either.

> ****************************************************************************
>
> class A
> attr_reader :varA
> def initialize
> @varA = 30
> end
> end
>
> class B
> def meth_b
> @varB = a.varA ##### How can I do this? ##########
> puts @varB
> end
> end
>
> a = A.new
> b = B.new
> b.meth_b

Well, you could use $a (global variable). But I certainly do not recommend
that usage here. Maybe you post a bit more of your problem so someone can
come up with a more appropriate solution.

Kind regards

robert

mepython

3/1/2005 10:22:00 PM

0

I am trying to create a simulation model which mainly has 3 classes:
Simulation, Processes, and Resources. Simulation class has simulation
time variable and events list. Both Processes and Resources will need
to access them to update there own state or to update simulation state.
Currently I am passing instance of Simulation as a parameter to methods
of Processes and Resources and was wondering how I can restructure it
so that I don't have to pass it as a parameter and not to resource to
global instance.

gga

3/2/2005 2:43:00 AM

0

Maybe I missed something, but something like this?

class Simulation
attr_reader :time
attr_reader :events
def initialize
@time = 5.0
@events = ['hello', 'goodbye']
end
end

class Processes
attr_accessor :simulation
def do_sth
# operate on @simulation
puts @simulation.events
end
def do_sth2
# operate on @simulation
puts @simulation.time
end
end

class Resources
attr_accessor :simulation
def do_sth
end
end

sim1 = Simulation.new
sim2 = Simulation.new

p = Processes.new
p.simulation = sim1
p.do_sth
p.do_sth2
p.simulation = sim2
p.do_sth2

r = Resources.new
r.simulation = sim1
r.do_sth

mepython

3/2/2005 3:36:00 AM

0

This will do the trick. I had brain f*rt; I will do one small change: I
will create simulation attribute at class level (for Processes and
Resources), so that I have to assign it only once. Thanks. I might need
some more help when I need Processes to hold Resources,
interrupt/kill/restart other Processes and so on.

gga wrote:
> Maybe I missed something, but something like this?
>
> class Simulation
> attr_reader :time
> attr_reader :events
> def initialize
> @time = 5.0
> @events = ['hello', 'goodbye']
> end
> end
>
> class Processes
> attr_accessor :simulation
> def do_sth
> # operate on @simulation
> puts @simulation.events
> end
> def do_sth2
> # operate on @simulation
> puts @simulation.time
> end
> end
>
> class Resources
> attr_accessor :simulation
> def do_sth
> end
> end
>
> sim1 = Simulation.new
> sim2 = Simulation.new
>
> p = Processes.new
> p.simulation = sim1
> p.do_sth
> p.do_sth2
> p.simulation = sim2
> p.do_sth2
>
> r = Resources.new
> r.simulation = sim1
> r.do_sth

Csaba Henk

3/2/2005 5:30:00 AM

0

On 2005-03-01, mepython <a@agni.us> wrote:
> I am trying to create a simulation model which mainly has 3 classes:
> Simulation, Processes, and Resources. Simulation class has simulation
> time variable and events list. Both Processes and Resources will need
> to access them to update there own state or to update simulation state.
> Currently I am passing instance of Simulation as a parameter to methods
> of Processes and Resources and was wondering how I can restructure it
> so that I don't have to pass it as a parameter and not to resource to
> global instance.

Maybe:

class Simulation
Defaults = {
:@foo => 13,
:@bar => "eh"
}

def initialize
Defaults.each &method(instance_variable_set)
end
end

Then you have those initial values at hand in a transparent, orthogonal
way, and Simulation instances will get those as instance variables.

Csaba

Robert Klemme

3/2/2005 7:43:00 PM

0


"mepython" <a@agni.us> schrieb im Newsbeitrag
news:1109734583.147981.253290@g14g2000cwa.googlegroups.com...
> This will do the trick. I had brain f*rt; I will do one small change: I
> will create simulation attribute at class level (for Processes and
> Resources), so that I have to assign it only once. Thanks.

In that case I'd make Simulation a singleton. Then you don't need any
assignments at all but can access the global Simulation instance from all
parts of the system and also it's much clearer from a documentation
perspective. I'd definitely not use a class variable in Process and
Resource for that.

However, both approaches will break if there are several simulations. Also
I have the feeling that rather lazyness dictates this one assignment only
policy than design. I'm curious to learn why it is a problem with having
simulation as a member in Process and Resource.

> I might need
> some more help when I need Processes to hold Resources,
> interrupt/kill/restart other Processes and so on.
>
> gga wrote:
>> Maybe I missed something, but something like this?
>>
>> class Simulation
>> attr_reader :time
>> attr_reader :events
>> def initialize
>> @time = 5.0
>> @events = ['hello', 'goodbye']
>> end
>> end
>>
>> class Processes
>> attr_accessor :simulation
>> def do_sth
>> # operate on @simulation
>> puts @simulation.events
>> end
>> def do_sth2
>> # operate on @simulation
>> puts @simulation.time
>> end
>> end
>>
>> class Resources
>> attr_accessor :simulation
>> def do_sth
>> end
>> end

Btw, with a little refactoring:

module SimulationClient
attr_accessor :simulation
end

class Simulation
def create(cl,*a,&b)
x = cl.new(*a,&b)
x.simulation = self if SimulationClient === x
x
end
end

class Process
include SimulationClient
...
end

class Resource
include SimulationClient
...
end

s = Simulation.new
p1 = s.create Process, "event processor"
....

If you use Simulation as factory for processes and resources then it's easy
to encapsulate the assignment in one place. Although I don't know the
details of your app it sounds a reasonable thing to do as the simulation is
the central piece and that uses / has processes and has resources.

Kind regards

robert