Forum: Ruby Accessing instance variables in different classes

399a991530e4bf70b979784f898fbaef?d=identicon&s=25 Paul Robinson (paulr)
on 2014-02-26 15:17
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
C47a7525de78ba5f3769aca123f4c1cc?d=identicon&s=25 Craig Monson (Guest)
on 2014-02-26 15:42
(Received via mailing list)
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
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 Chris Hulan (Guest)
on 2014-02-26 15:46
(Received via mailing list)
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
399a991530e4bf70b979784f898fbaef?d=identicon&s=25 Paul Robinson (paulr)
on 2014-02-26 21:20
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
http://www.brpreiss.com/books/opus8/html/page597.html inner methods may
not access outer variables :(
20753db11b9ffec13351e7fc2ddfbf7e?d=identicon&s=25 Ricky N. (ricky_n)
on 2014-02-26 21:44
(Received via mailing list)
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.
C47a7525de78ba5f3769aca123f4c1cc?d=identicon&s=25 Craig Monson (Guest)
on 2014-02-26 21:50
(Received via mailing list)
Take a look at Design Patterns:
http://en.wikipedia.org/wiki/Design_Patterns
or more specifically the "four horsemen" book:
http://www.amazon.com/Design-Patterns-Elements-Reu...

Or more ruby specifc:
http://www.amazon.com/Practical-Object-Oriented-De...

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
Df15cecd056f7d4764d3c0e76f46083f?d=identicon&s=25 Iurii Plugatariov (Guest)
on 2014-02-26 21:58
(Received via mailing list)
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 Monson (craig@malachiarts.com)
wrote:

Take a look at Design Patterns:
http://en.wikipedia.org/wiki/Design_Patterns
or more specifically the "four horsemen" book:
http://www.amazon.com/Design-Patterns-Elements-Reu...

Or more ruby specifc:
http://www.amazon.com/Practical-Object-Oriented-De...

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
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-02-26 22:16
Paul Robinson 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(p)
      @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)
399a991530e4bf70b979784f898fbaef?d=identicon&s=25 Paul Robinson (paulr)
on 2014-02-26 22:20
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...
:D

Thanks again,

Paul
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-02-27 11:23
(Received via mailing list)
On Wed, Feb 26, 2014 at 10:20 PM, Paul Robinson <lists@ruby-forum.com>
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...
> :D

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

Kind regards

robert
399a991530e4bf70b979784f898fbaef?d=identicon&s=25 Paul Robinson (paulr)
on 2014-02-27 14:46
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...
>> :D
>The fact that you needed to change so many lines of code may be
>indicative of room for improvement of your abstraction. :-)

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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-02-27 16:54
(Received via mailing list)
On Thu, Feb 27, 2014 at 2:46 PM, Paul Robinson <lists@ruby-forum.com>
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...
>>> :D
>>The fact that you needed to change so many lines of code may be
>>indicative of room for improvement of your abstraction. :-)
>
> 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.

:-)

> 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.
;-)

Cheers

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.