Simple Question About Deleting Instances

I have a case where I’d like to delete an instance of a class I made
myself. How do I do that?

The problem I’m having is that while iterating through a loop, I’m
constantly trying to reassign the same variable name to a different
instance, but Ruby is just adding the new data to the old, so I’m
mounting piles and piles of unwanted data on an instance.

I googled, but couldn’t find a solution. Odd!

Some code would help. Saying: …

require ‘my_stuff’ # Includes the Report class

file_list = Dir.new(“reports”)
yes_list = []
no_list = []
file_list.each do |file|
if file_name =~ /yes/
report = Report.new(file_name)
yes_list.push(report.ID_list)
elsif file_name =~ /no/
report = Report.new(file_name)
no_list.push(report.ID_list)
end
end

puts yes_list.inspect
puts no_list.inspect

Both yes and no lists gather way more data than they’re supposed to and
based on the excess data I’m seeing, it looks like it just isn’t
deleting old data from the old instance of report.

I’ve (feebly) tried report.delete, report.replace, and report = nil at
the end of each if block, if that helps convey my goal.

I think you problems is in the report class. Each time you do report =
Report.new, you won’t be getting any data carried over from the last
report
object. Is report.ID_list a method on the class? or on the instance? The
only way I can see it keeping data around is if you have some type of
data
structure as a class variable, when you meant it to be an instance
variable.

mark

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nathan O. wrote:

I have a case where I’d like to delete an instance of a class I made
myself. How do I do that?

The problem I’m having is that while iterating through a loop, I’m
constantly trying to reassign the same variable name to a different
instance, but Ruby is just adding the new data to the old, so I’m
mounting piles and piles of unwanted data on an instance.

Some code would help. Saying:

a = 1
a = 2
a = 3

Will always reassign variable ‘a’ to something new. My guess is you
aren’t really reassigning your reference to a new value. But
it’s hard to say without any code cause no one can tell what you are
doing.

Zach
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFENo3JMyx0fW1d8G0RAqN4AJ4yQ+netLhX3nqNAKM4k8fj/kYSAQCfT899
zFfsDvypayWzWW4sLBdJQS4=
=DELC
-----END PGP SIGNATURE-----

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nathan O. wrote:

Currently, all my methods for my classes are defined as:

def method_name()

end

This is an instance method. It works on the instance of Report.

And my variables are all defined as:

@@var = …

This is a class variable. All Reports will share this variable. You
probably want to make it an instance variable with one @ symbols.

@var = …

I couldn’t figure out any other way to make variables available to
methods of the class they belonged to, but doing it this way worked
.

For more information you may want to check out the Learn To Program book
by Chris P.s. Learn to Program, by Chris Pine

Specfically Classes - Learn to Program nd scroll down to
Instance Variables.

Zach

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFENpa7Myx0fW1d8G0RAtW+AJ9OK1RnQnuud/rbdbsUl7FU4fdroQCfeO9k
O6F5VO92CGpw/QSuX1sFB7U=
=wii5
-----END PGP SIGNATURE-----

zdennis wrote:

def method_name()

end

This is an instance method. It works on the instance of Report.

And my variables are all defined as:

@@var = …

This is a class variable. All Reports will share this variable. You
probably want to make it an instance variable with one @ symbols.

@var = …

I replaced all instances of “@@” with “@” in my script since I don’t
need any class variables. Now, Ruby generates errors telling me that all
of these variables are unknown when referenced from instance methods.

Do I need to make any special kinds of accessors or anything?

And on that note, how do I do something like attr_reader :@@var? Ruby
doesn’t seem to know what to do when I put those @s there.

Mark Van H. wrote:

I think you problems is in the report class. Each time you do report =
Report.new, you won’t be getting any data carried over from the last
report
object. Is report.ID_list a method on the class? or on the instance? The
only way I can see it keeping data around is if you have some type of
data
structure as a class variable, when you meant it to be an instance
variable.

mark

Ah, this is a question I’ve had for a while. Last time I looked in to
it, I didn’t find a real answer, but my code started working, so I ran
with it.

How do I define the difference between class variables and instance
variables? Class methods and instance methods?

Currently, all my methods for my classes are defined as:

def method_name()

end

And my variables are all defined as:

@@var = …

I couldn’t figure out any other way to make variables available to
methods of the class they belonged to, but doing it this way worked
.

Nathan O. wrote:

probably want to make it an instance variable with one @ symbols.

@var = …

I replaced all instances of “@@” with “@” in my script since I don’t
need any class variables. Now, Ruby generates errors telling me that all
of these variables are unknown when referenced from instance methods.

Can you post code along with your questions ? It makes it so much easier
to help.

Do I need to make any special kinds of accessors or anything?

And on that note, how do I do something like attr_reader :@@var? Ruby
doesn’t seem to know what to do when I put those @s there.

When using attr_reader, attr_writer, attr_accessor you dont need to
include the @ symbols.

class A
attr_accessor :var # this will create an instance variable @var
# plus the get/set methods for var on an instance
# of A

class << self
attr_accessor :foo # this will create a class method var for you
# plus the get/set methods for var on the class
# A. The var will be created as an instance
#variable @var on the class A
end
end

a = A.new
a.var = 5
puts a.var #outputs 5

b = A.new
b.var = 10
puts b.var #outputs 10

A.foo = 100
puts A.foo #outputs 100

Please do read the Chris P.s Learn To Program from my last post. It
seems you are guessing at what goes where and you need little a
guidance, Chris P.s Learn to Program will help you get started in the
right direction.

Zach

Nathan O. wrote:

end
end

You are creating an instance variable @b on your class A. The class A is
an instance of the class Class, so it to can have instance variables.

You want to define your instance variable @b inside of your initialize
method, or you want to use attr_* methods to do this for you:

class A
@@a = 1

def initialize
  @b = 2
end

def say_stuff
   puts @@a
   puts @b
end

end

a = A.new
a.say_stuff

Or:

class A
attr_accessor :b

@@a = 1

def initialize
  @b = 2
end

def say_stuff
   puts @@a
   puts @b
end

end

a = A.new
a.b = 10
a.say_stuff

You should really check this stuff out in IRB as well. Open a shell or
cmd.exe and type ‘irb’.

Zach

zdennis wrote:

You are creating an instance variable @b on your class A. The class A is
an instance of the class Class, so it to can have instance variables.

Not to be a whiner, but is there a reason we can’t just do attr_accessor
:@@var? What you’re saying is logical, but it seems like we’re getting
from point A to point B via point F.

class A
@@a = 1

def initialize
  @b = 2
end

def say_stuff
   puts @@a
   puts @b
end

end

a = A.new
a.say_stuff

Or:

You should really check this stuff out in IRB as well. Open a shell or
cmd.exe and type ‘irb’.

I do use irb (well, I write little test scripts and then run them
through regular ruby, but same thing). I’ve done something else wrong:
when I try this specifically the way you’ve outlined in a small test
script, it works. Doing it that way in the script I’m working on
generates errors. I guess I’ve got something else to look in to.

zdennis wrote:

Can you post code along with your questions ? It makes it so much easier
to help.

class A
@@a = 1
@b = 2
def say_stuff()
puts @@a # Works
puts @b # Doesn’t work
end
end

When using attr_reader, attr_writer, attr_accessor you dont need to
include the @ symbols.

class A
attr_accessor :var # this will create an instance variable @var
# plus the get/set methods for var on an instance
# of A

class << self
attr_accessor :foo # this will create a class method var for you
# plus the get/set methods for var on the class
# A. The var will be created as an instance
#variable @var on the class A
end
end

a = A.new
a.var = 5
puts a.var #outputs 5

b = A.new
b.var = 10
puts b.var #outputs 10

A.foo = 100
puts A.foo #outputs 100

That seems way more convoluted than just:

class A
@@var = “”
attr_accessor :@@var
end

Class << self creates class variables by the same name as instance
variables? So you’d have both @@var and @var? Is there a specific reason
for this? It seems HIGHLY unpredictable.

Please do read the Chris P.s Learn To Program from my last post. It
seems you are guessing at what goes where and you need little a
guidance, Chris P.s Learn to Program will help you get started in the
right direction.

I’ll at least give it a glance at lunch time. Sounds promising. Thanks!

Nathan O. wrote:

… this is going to take a while to debug. Does anyone have any idea
why using only class variables would work but instance variables
wouldn’t? I suspect tainting, but how is any of this tainted?

Now I just feel like I’m spamming the forum with my newbieness…

For some reason, having methods for accessors (def fileName;return
@fileName;end) works on the command line but not through a browser.
Using attr_accessor instead, it works.

That part of my code was a vestige from previous habits, so I wont miss
it, but does anyone have any idea why it wouldn’t have worked to write
accessor methods?

Nathan O. wrote:

I do use irb (well, I write little test scripts and then run them
through regular ruby, but same thing). I’ve done something else wrong:
when I try this specifically the way you’ve outlined in a small test
script, it works. Doing it that way in the script I’m working on
generates errors. I guess I’ve got something else to look in to.

Hmmm!!! My code actually is working! The problem: it’s a CGI script.
On the command line, it runs fine. From a browser, it breaks down!

… this is going to take a while to debug. Does anyone have any idea
why using only class variables would work but instance variables
wouldn’t? I suspect tainting, but how is any of this tainted?

Nathan O. wrote:

zdennis wrote:

You are creating an instance variable @b on your class A. The class A is
an instance of the class Class, so it to can have instance variables.

Not to be a whiner, but is there a reason we can’t just do attr_accessor
:@@var? What you’re saying is logical, but it seems like we’re getting
from point A to point B via point F.

No. You just need to understand how classes and instances of a class
work.

attr_* methods set instance variables and get/set methods on an instance
of a class. It does this using symbols.

class A
attr_accessor :b
end

:b will point to instance variable @b, on an instance of A.

If you want to use attr_* to set class variables you need to work on the
instance of the class itself. That is why we do:

class A
class << self
attr_accessor :class_var
end
end

Because “class << self” opens up the instance of class A. And :class_var
will map to a ‘@class_var’ instance variable on your class itself (and
not on an instance of your class).

If you want further shortcuts check out the facets or traits libraries
on rubyforge. They may have what you are looking for, although I would
ask you to understand how ruby works first before trying to shortcut
this.

Zach

Nathan O. wrote:

Nathan O. wrote:

… this is going to take a while to debug. Does anyone have any idea
why using only class variables would work but instance variables
wouldn’t? I suspect tainting, but how is any of this tainted?

Now I just feel like I’m spamming the forum with my newbieness…

For some reason, having methods for accessors (def fileName;return
@fileName;end) works on the command line but not through a browser.
Using attr_accessor instead, it works.

The ruby-esque way to write methods is lowercase with underscores
separating words:

def file_name; end

Scratch the lowerCamelCase and save the UpperCamelCase for class and
module definitions.

Zach

zdennis wrote:

attr_* methods set instance variables and get/set methods on an instance

class A
class << self
attr_accessor :class_var
end
end

Because “class << self” opens up the instance of class A. And :class_var
will map to a ‘@class_var’ instance variable on your class itself (and
not on an instance of your class).

class A
end

this creates an instance of your class, which is of class A

a = A.new

this opens the instance of class A itself, which is of class Class

class A
class << self
end
end

Zach

zdennis wrote:

Nathan O. wrote:
No. You just need to understand how classes and instances of a class
work.

I understand how it works. In Ruby, (almost) everything is an object.
Therefor, when I’m defining a class and I want a class variable to be
accessible, I need to say so in defining the class, not in defining how
instances of the class work.

Again, I understand how it works. I’m just trying to point out that
sure, doing it this way proves that the coder knows how Ruby works, but
it also makes the code reader think more than maybe they should need to.
This works in direct contrast with the perception that Ruby lets you
focus more on solving problems and less on writing code.

Just my perception as someone who just spent half a day trying to work
around language particularities.

Nathan O. wrote:

Again, I understand how it works. I’m just trying to point out that
sure, doing it this way proves that the coder knows how Ruby works, but
it also makes the code reader think more than maybe they should need to.
This works in direct contrast with the perception that Ruby lets you
focus more on solving problems and less on writing code.

Just my perception as someone who just spent half a day trying to work
around language particularities.

If you want you can extend ruby to do what you want. Ruby gives you
this flexibility…

class Class
def attr_class_accessor arg
instance_eval “class << self ; attr_accessor :#{arg} ; end”
end
end

class A
attr_class_accessor :b
end

Zach

zdennis wrote:

If you want you can extend ruby to do what you want. Ruby gives you
this flexibility…

class Class
def attr_class_accessor arg
instance_eval “class << self ; attr_accessor :#{arg} ; end”
end
end

class A
attr_class_accessor :b
end

Yes, but… that still means that someone has to think about more
implementation details than should (in my opinion) be necessary :slight_smile: You
know what I mean?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nathan O. wrote:

class A
attr_class_accessor :b
end

Yes, but… that still means that someone has to think about more
implementation details than should (in my opinion) be necessary :slight_smile: You
know what I mean?

I had to think about half a second, and then type for about 8 seconds.
Just copy and paste the code and put it in a file that you
require on your projects, and you won’t have to think. I will think for
you. Matz is busy solving bigger problems and conquering
better obstacles. So he doesn’t have to think about this, I will put my
brain to work for him.

class Class
def attr_class_accessor arg
instance_eval “class << self ; attr_accessor :#{arg} ; end”
end

def attr_class_reader arg
  instance_eval "class << self ; attr_reader :#{arg} ; end"
end

def attr_class_writer arg
  instance_eval "class << self ; attr_writer :#{arg} ; end"
end

end

If you feel this stronlgy about your desire to simplify this, perhaps
you should submit it as an RCR, http://www.rcrchive.net. I
am not saying I think your idea to simplify this into a builtin part of
ruby is a bad thing, but I think that you should not
abolish your ability to think. Ruby gives you power, use it. And if you
think it can be made better, submit a patch or submit an RCR.

Matz gives you the power to influence the langauge, I challenge you to
do so.

Zach
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFENrBSMyx0fW1d8G0RAgMYAJ9vFyS/H5Jn/jYdxIZgr57y+i6N5gCfThBe
zpwrWvyW5OX6BC2oJxVAo1Q=
=4Jlf
-----END PGP SIGNATURE-----