How can I access value of an instances

M

mepython

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
 
L

linus sellberg

mepython said:
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.
 
M

mepython

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
 
R

Robert Klemme

mepython said:
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
 
M

mepython

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

gga

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
 
M

mepython

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

Csaba Henk

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 = {
:mad:foo => 13,
:mad: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
 
R

Robert Klemme

mepython said:
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top