Accessing instance variables in different classes

I have a program like the following, where I the instances are declared
within the body of the program and I’d like to access ‘count’ in a
method of obj2, as indicated, but it doesn’t work.

I thought it might be because object one needed to be a global variable
but according to http://www.rubyist.net/~slagell/ruby/localvars.html
it’s scope would be the entire script anyway as it is not defined with a
block. I tried using $obj1 and $obj2 anyway but I can’t seem to refer to
$obj1.count within $obj2’s methods.

If I’m reading things right, class variables would be of no use either
in this situation. So, short of reading the value from obj1 first then
passing it as a parameter to obj2 is there another way?

TIA

Paul

class One
attr_reader :count
def initialize

end
end

class Two
def initialize

end
def someMethod
#access One.count here
end
end

obj1 = One.new
obj2 = Two.new

Is there a particular reason why you are approaching it like this? I do
believe this looks like the wrong approach, but I’m not positive what
purpose
you are going for.

-c

you do need an instance of One to access an instance variable
So you can pass obj1 as a parameter to obj2.someMethod
or maybe obj1 should be created inside someMethod

Thanks Craig, I’m trying to write a very simple game; I realise that
Ruby isn’t the right vehicle for writing games (!) but hopefully it will
help me learn Ruby and have some fun along the way. I’ve got a ‘world’
object and a ‘player’ object which have very different attributes and
methods but each may need to access the information contained in the
other, for example the player might only move in a certain direction
depending upon where he is in the world.

Chris, I think that may be way to go, i.e. creating the player object
within the world object (sounds logical too!) - I’ve never done anything
like that before but a quick search shows that such nested or inner
classes might be just right.

Thanks both,

Paul

EDIT Maybe not, according to
brpreiss.com inner methods may
not access outer variables :frowning:

Ruby actually isn’t that bad for games. Native ruby can use
http://www.libgosu.org/ and JRuby can use any of the Java frameworks.

And for your actual issue, organizing complex OO programs is quite hard.
What you want to do is not actually have nested/inner classes exactly.
You
just want to have instances of world contain instances of player. So
something kinda like:

class World
attr_accesor :player

def tick
    self.player.update() unless self.back_hole?
end

end

w = World.new
p = Player.new

w.player = p

An inner class would imply that the Player is tightly coupled with the
World. This would mean that say if you original had a Player that acted
like a fish, it would be much harder to add a second Player that acts
like
a jellyfish. If you will, think of the World class as god. It tells all
the
players simply to live and it is up to the players to figure out how to
do
it.

“Practical Object-Orientated Design in Ruby” by Sandi Metz explains this
much better than I can.

Take a look at Design Patterns:

or more specifically the “four horsemen” book:
http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?s=books&ie=UTF8&qid=1393447423&sr=1-1&keywords=design+patterns

Or more ruby specifc:
http://www.amazon.com/Practical-Object-Oriented-Design-Ruby-Addison-Wesley-ebook/dp/B0096BYG7C/ref=sr_1_2?s=books&ie=UTF8&qid=1393447456&sr=1-2&keywords=rails+design+patterns

Those have been super helpful to me, and many other code slingers. To
that
end, I think you are taking the wrong architectural approach, but it’s
great that you are giving it a go!

-c

Paul R. wrote in post #1138172:

Thanks Craig, I’m trying to write a very simple game; I realise that
Ruby isn’t the right vehicle for writing games (!)

the issue with game & ruby is performence,
object design of a game is easy in ruby (as any good object langage)
but object design of a game not so easy…

I’ve got a ‘world’
object and a ‘player’ object which have very different attributes and

good start,

like that before but a quick search shows that such nested or inner
classes might be just right.

Inner class are not necessary…
Here a example

===
class Actor
def collision(pt,r)

end
end
class Player < Actor; … end
class Robot < Actor; … end

====

class World
def initialize()
@player= Player.new(self)
@lrobots = (0…10).each.map { Robot.new(self) }
end
def tick
@player.tick
@lrobots.each { |r| r.tick }
end
def tst_collision_robot§
@lrobots.select { |r| r.collision_with(p.position,p.radius) }
end
. . .
end

see rrobots (many versions),

In gosu world ( with ruby 1.9 only, 2.x not yet supported),
ang : one player with obstacles and targets… (github / gem)

Let me put my 5 cents about game development in ruby. Someone says that
ruby isn’t suitable for gamedev, but it depends on your purposes. If
want to implement AAA crisis-like FPS, ruby is not your tool of choice.
But if you want to take a part in some game jams like Ludum Dare, ruby
is awesome tool. I hardly recommend you blog of this
guy http://spooner.github.io/ to make sure that ruby gamedev isn’t a
myth. My advice to use: gosu(graphics rendering, game loop, sound, etc),
chipmunk(game physics), ashton(shaders), releasy(packager).


Iurii Plugatariov
Sent with Airmail

On February 26, 2014 at 22:50:28, Craig M. ([email protected])
wrote:

Take a look at Design Patterns:

or more specifically the “four horsemen” book:
http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?s=books&ie=UTF8&qid=1393447423&sr=1-1&keywords=design+patterns

Or more ruby specifc:
http://www.amazon.com/Practical-Object-Oriented-Design-Ruby-Addison-Wesley-ebook/dp/B0096BYG7C/ref=sr_1_2?s=books&ie=UTF8&qid=1393447456&sr=1-2&keywords=rails+design+patterns

Those have been super helpful to me, and many other code slingers. To
that
end, I think you are taking the wrong architectural approach, but it’s
great that you are giving it a go!

-c

Thanks all, that’s all useful info and most welcome (I never expected
those game libraries for Ruby). Maybe I should say that I’ve written,
co-written or been involved in a fair few 2D/3D freeware games before
(C, Blitz Basic) and have a fair bit of experience with Java/Processing.

I’m not writing this for anyone except myself - it will be a tiny game,
text only (but maybe using curses) and just to help me learn more about
Ruby.

I’ve just modified the few hundred lines of code such that the player is
part of the world as per Ricky’s suggestion, let’s see where it goes…
:smiley:

Thanks again,

Paul

Thanks Robert, I’ve never had cause to try creating one object inside
another in Java (or any other language) but as I say, my Java experience
is not extensive.

I’ve just modified the few hundred lines of code such that the player is
part of the world as per Ricky’s suggestion, let’s see where it goes…
:smiley:
The fact that you needed to change so many lines of code may be
indicative of room for improvement of your abstraction. :slight_smile:

Haha, the program is a few hundred lines, I didn’t have to modify all
of them - if that were the case even I could tell that the program had a
serious design flaw.

Incorporating the player into the world class has certainly made things
a lot cleaner so far.

Thanks Regis, I need to think more about the example you’ve given!

Cheers

Paul

On Wed, Feb 26, 2014 at 10:20 PM, Paul R. [email protected]
wrote:

Thanks all, that’s all useful info and most welcome (I never expected
those game libraries for Ruby). Maybe I should say that I’ve written,
co-written or been involved in a fair few 2D/3D freeware games before
(C, Blitz Basic) and have a fair bit of experience with Java/Processing.

This slightly confuses me: you state that you have experience doing
Java coding but your question is about basic OO principles. These are
the same across OO languages: separations of concerns, information
hiding, internal state consistency… In what way is Ruby so different
that you feel you need to ask about this? Maybe there’s more to the
question that we haven’t grasped yet.

I’m not writing this for anyone except myself - it will be a tiny game,
text only (but maybe using curses) and just to help me learn more about
Ruby.

Nothing wrong with that. And Ruby is certainly suited for that type of
game development.

I’ve just modified the few hundred lines of code such that the player is
part of the world as per Ricky’s suggestion, let’s see where it goes…
:smiley:

The fact that you needed to change so many lines of code may be
indicative of room for improvement of your abstraction. :slight_smile:

Kind regards

robert

On Thu, Feb 27, 2014 at 2:46 PM, Paul R. [email protected]
wrote:

Thanks Robert, I’ve never had cause to try creating one object inside
another in Java (or any other language) but as I say, my Java experience
is not extensive.

Oh, I thought so from “have a fair bit of experience with
Java/Processing”.

I’ve just modified the few hundred lines of code such that the player is
part of the world as per Ricky’s suggestion, let’s see where it goes…
:smiley:
The fact that you needed to change so many lines of code may be
indicative of room for improvement of your abstraction. :slight_smile:

Haha, the program is a few hundred lines, I didn’t have to modify all
of them

Oh, then I misunderstood. It sounded to me as if you had modified a
few hundred lines of code.

  • if that were the case even I could tell that the program had a
    serious design flaw.

:slight_smile:

Incorporating the player into the world class has certainly made things
a lot cleaner so far.

Good!

Thanks Regis, I need to think more about the example you’ve given!

Reasoning about code is good and important - trying out things as well.
:wink:

Cheers

robert