Hi I’ve been doing rails for a while, which was really my first
introduction to ruby. I’ve found my self using ruby more and more and
keep amazing my self about how cool it is, although I’ve got a few
questions, or rather holes in my ruby knowledge, from rails doing a lot
of stuff for you I think.
What does a double colon do in terms of class inheritance. ie.
GameWindow < Gosu::Window ?
Why would you want to use nested classes?
Is it bad practice to define constance out of classes?
What does a double colon do in terms of class inheritance.
Nothing.
ie. GameWindow < Gosu::Window ?
This means that GameWindow inherits from the class Gosu::Window, which
is the
class Window in the namespace Gosu.
Why would you want to use nested classes?
To avoid name collision or just to group things together that belong
together
or keep classes that won’t be directly used by the user out of the main
namespace. Like if you have a class Card that uses the class Rank, but
the
user won’t ever create an instance of class Rank himself (he just will
get
Rank object when he does my_card.rank), you’d nest Rank inside Card (and
Card
probably inside MyGame, for the previous reasons).
Is it bad practice to define constance out of classes?
Not as such, but it’s generally a good idea - at least for libraries
that will
be used by other ruby files, to have all your stuff in a namespace to
avoid
name collision as in point 2.
How/Can you delete instance variables
Just set them to nil or use remove_instance_variable (the former won’t
actually delete the variable, but why do you need to do that anyway?)
or objects?
An objects gets GCed when it’s no longer referenced by any variables.
Hi I’ve been doing rails for a while, which was really my first
introduction to ruby. I’ve found my self using ruby more and more and
keep amazing my self about how cool it is, although I’ve got a few
questions, or rather holes in my ruby knowledge, from rails doing a lot
of stuff for you I think.
What does a double colon do in terms of class inheritance. ie.
GameWindow < Gosu::Window ?
Nothing special. As always, it’s used to access a constant defined in
another
class or module. In your specific example, it is used to access the
Window
class in the Gosu module so that the GameWindow class can inherit it. It
is
exactly the same as:
window = Gosu::Window
class GameWindow < window
Why would you want to use nested classes?
Personally, I use nested classes when the inner class is closey coupled
to the
outer and isn’t meant to be used by itself(for example, it can be a
helper
class used by the implementation of the outer class and which isn’t
useful if
used in other ways). They don’t provide additional benefits, but make
clearer
the programmer’s intent
Is it bad practice to define constance out of classes?
Constants defined outside classes are top level constants, so the answer
depends on the meaning of the constant. If it is related to the
application or
library as a whole, it may make sense to define it globally (for
example, a
constant containing the application version). If it is specific of a
class,
put it there (for example, take the constants defined in class Regexp.
They
are meant to be used only in methods of that class, so it wouldn’t make
sense
to define it globally).
How/Can you delete instance variables or objects?
It depends on what you exactly mean, but I’d say the answer is no. I
don’t
think you can force an object to be garbage-collected, and you cant
undefine
an instance variable. What you can do is to make possible for the object
to be
garbage-collected by setting all variables which point to it to
something else
and set an instance variable to nil.
Sorry, I misread the documentation. You can indeed undefine an instance
variable, using the remove_instance_variable method. Since it is
private,
though, if you need to call it from outside the instance, you can either
use
send
What does a double colon do in terms of class inheritance. ie.
GameWindow < Gosu::Window ?
Nothing. It’s a namespace thing.
Why would you want to use nested classes?
In order not to pollute the global namespace.
Is it bad practice to define constance out of classes?
It depends on whether you care about polluting the global namespace.
IMHO, yes.
How/Can you delete instance variables or objects?
By removing references to it (unsetting variables pointing to the
object) and letting the garbage collector do its work. I’m not sure
what you mean with “delete instance variables”.
I hope the answers are about right. If not, please somebody correct
me.
the Gosu::Window thing would that be an example of nested then? as in
Gosu module Window class?
Yes.
the deleting instance var object was something that I was trying to do
in regards to Gosu Game lib.
it tries to update itself 60 times a second, so if you press a key it
gets regstered by the lib several times. I was trying to create a method
that it would once the key was pressed it remained inactive for a set
time. I was using the Gosu::milliseconds for this. it seemed easier to
create a class that handled this, and I was getting worried that it
would create loads of objects that only had a short life span then
sucked up perfomance as the game continued. but if they delete them self
if there is no pointer to them that works.
In your key handling method, wrap the logic in something like
if not @pause_end or Gosu::milliseconds > @pause_end
…
end
After you handle whatever key was pressed, just add a line like:
@pause_end = Gosu::milliseconds + 500 # or however long you want it to
be inactive
In regards to this post, a more relevant example of an instance variable
reference problem is the following:
In a Game Object, there is an Area object.
An area object has Player A and Player B, which are themselves objects.
When Player A interacts (such as attacks) Player B, both objects store a
reference to each other in order to interact.
So in this sense, the Game Object, Area Object and both players have
reference to player objects.
Now, if Player A quits the game, the Game Object might make the
reference to the Player A = nil.
It would be very helpful to be able to destroy the object, without
having to find all objects referencing this object to finally get Player
A to become nil.
I would appreciate help on this matter, on how to make a single object
nil and have that propagate to everything referencing it.
the Gosu::Window thing would that be an example of nested then? as in
Gosu module Window class?
the deleting instance var object was something that I was trying to do
in regards to Gosu Game lib.
it tries to update itself 60 times a second, so if you press a key it
gets regstered by the lib several times. I was trying to create a method
that it would once the key was pressed it remained inactive for a set
time. I was using the Gosu::milliseconds for this. it seemed easier to
create a class that handled this, and I was getting worried that it
would create loads of objects that only had a short life span then
sucked up perfomance as the game continued. but if they delete them self
if there is no pointer to them that works.
Now, if Player A quits the game, the Game Object might make the
reference to the Player A = nil.
It would be very helpful to be able to destroy the object, without
having to find all objects referencing this object to finally get Player
A to become nil.
I would appreciate help on this matter, on how to make a single object
nil and have that propagate to everything referencing it.
Two approaches come to mind. The first is make the Game Object
observable (see observable.rb). When it is destroyed it notifies the
objects that refer to it.
The second is to pass around an indirect reference to the Game Object.
For example, make an array that contains only the Game Objecct and give
the players a reference to the array. When the Game Object is destroyed
it sets the array element to nil. The players never refer to the game
object directly, only via the array and only after making sure it hasn’t
become nil.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.