"Benny" <linux / marcrenearns.de> schrieb im Newsbeitrag
news:2nphthF3bt4sU1 / uni-berlin.de...
> Robert Klemme wrote:
>
> >
> > "Benny" <linux / marcrenearns.de> schrieb im Newsbeitrag
> > news:2npdjuF3728iU1 / uni-berlin.de...
> >> ok after rethinking I have to correct me:
> >> substitute Object#name with Object#names
> >> in my first posting
> >
> > No good.  Benny, I seriously think you should get rid of the idea that
> > variable identifiers are an object's names.
> >
> ok, I'll have to clear my programming vocabulary :)
>
> > It's a one way street.  You can only walk it in the direction of varible
> > to instance, not the other way.  It's not needed and it would be a total
> > overhead to manage this at runtime.
> >
> ok, thats a satisfactory answer, thanks :)

You're welcome!

> >> (I like to iterate over all objects of a class and often I need
> >> the name of
> >> the object in the iteration to identify the current thing.
>
> > No, you don't need the var name for that.  You can always do
> >
> > foo = Foo.new
> >
> > ObjectSpace.each_object( Foo ) do |obj|
> >   if obj.equal? foo
> >     puts "found it"
> >   else
> >     puts "other instance"
> >   end
> > end
>
> no, I wanted to do some like this:
>
> foo = Foo.new
>
> ObjectSpace.each_object( Foo ) do |obj|
>    puts obj.names.to_s # was: obj.name
> end
>
> I need to output the name somewhere (in a console,
> in a log file of the like)
>
>
> >> in the iteration I like to invoke certain methods, so in the end they
> >> are  processed on every object of that class)
> >
> > You're making me curios why you need that and what you do.
> it quite comfortable for lots of things.
>
> Currently I use it for a simple backup-script, where each
> configuration item has a name and attributes for src, dest, exclusion,
> accessing programs (e.g. mysql) etc.
>
> there are 2 iterations over the backup-objects:
> the first one to check the configuration if its complete and the syntax
> is ok. the second one processes the backup stops accessing programms and
> restarts some of them etc.
>
> but I really needed the ObjectSpace iteration when I made did parsing of
> syntactical items (call it "tags") who are interdependend and
> have different behavior. they all have some "stages" (the degree of the
> completness) and could embed other tags. for the resolution of the
> dependancies to the other "tags" their methods are called and one after
> another resolv its dependencies mark itself as being in the next stage
> and gives the requested properties to the calling "tag" back and so on.
>
> there are some "meta-stages" in each of the an ObjectSpace iteration over
> the tags is done with calling a specific method initializing the
dependency
> resolving worm.
>
> while programing I find it quite useful to let the tags give me some
output
> about what they are doing in the ObjectSpace. and therefor I needed some
> kind of "names" to identify them (I ended up in giving them numbers in a
> gobal hash where the key was the number and the value was the instance of
> the "tag"-object and additionally the number was saved as attribut of this
> object.

That's exactly what I was going to suggest.  It's a better solution anyway
since the number of instances is most likely not known beforehand.  So you
need some kind of collection anyway.  As you figured, a hash comes in very
handy here and you can use any attribute of the instance you like as key.
You can even wrap the hash with little logic so as to retrieve the key on
insertion:

class Index < Hash
  def initialize(key)
    super()
    @key = key
  end

  def index(obj)
    self[ obj.send @key ]= obj
  end
end

>> class Foo; attr_accessor :name; end
=> nil
>> f=Foo.new
=> #<Foo:0x1019da08>
>> f.name = "hallo"
=> "hallo"
>> idx = Index.new :name
=> {}
>> idx.index f
=> #<Foo:0x1019da08 @name="hallo">
>> idx
=> {"hallo"=>#<Foo:0x1019da08 @name="hallo">}
>>

Of course you can add logic so the objects know their index automatically
etc.

Kind regards

    robert