PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Determine "owning" class?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Determine "owning" class?

Réponse
 
LinkBack Outils de la discussion
Vieux 09/03/2008, 16h58   #1
dkmd_nielsen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Determine "owning" class?

class A
end

class C
def initialize
x = [A.new,A.new]
end

def template
x
end
end

Is it possible for an instance of A to determine the instance of C
that instantiated it?

The application I'm working on has a class named Template that
contains an array of Blocks. What I would like to do is have an
instance of Block reference back to Template's array so that it can
search for a particular sibling block in the array. Is the structure
built incorrectly? (It's too late to rebuild at this time.)

Thanks,
dvn
  Réponse avec citation
Vieux 09/03/2008, 17h58   #2
Ken Bloom
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Determine "owning" class?

On Sun, 09 Mar 2008 08:58:06 -0700, dkmd_nielsen wrote:

> class A
> end
>
> class C
> def initialize
> x = [A.new,A.new]
> end
>
> def template
> x
> end
> end
>
> Is it possible for an instance of A to determine the instance of C that
> instantiated it?
>
> The application I'm working on has a class named Template that contains
> an array of Blocks. What I would like to do is have an instance of
> Block reference back to Template's array so that it can search for a
> particular sibling block in the array. Is the structure built
> incorrectly? (It's too late to rebuild at this time.)
>
> Thanks,
> dvn


class A
def initialize owner
@owner=owner
end
end

class C
def initialize
x = [A.new(self),A.new(self)]
end

def template
x
end
end

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
  Réponse avec citation
Vieux 10/03/2008, 23h44   #3
Pit Capitain
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Determine "owning" class?

2008/3/9, dkmd_nielsen <donn@cmscms.com>:
> The application I'm working on has a class named Template that
> contains an array of Blocks. What I would like to do is have an
> instance of Block reference back to Template's array so that it can
> search for a particular sibling block in the array.


Where are the blocks created? If they are created inside an instance
of the Template class, then you should be able to get at it from the
block:

class Template
def initialize
@blocks = [lambda{}, lambda{}]
end
attr_reader :blocks
end

t1 = Template.new
b = t1.blocks.first
t2 = eval("self", b)
t1 == t2 # => true

Regards,
Pit

  Réponse avec citation
Vieux 11/03/2008, 16h46   #4
Ken Bloom
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Determine "owning" class?

On Mon, 10 Mar 2008 17:44:41 -0500, Pit Capitain wrote:

> 2008/3/9, dkmd_nielsen <donn@cmscms.com>:
>> The application I'm working on has a class named Template that
>> contains an array of Blocks. What I would like to do is have an
>> instance of Block reference back to Template's array so that it can
>> search for a particular sibling block in the array.

>
> Where are the blocks created? If they are created inside an instance of
> the Template class, then you should be able to get at it from the block:
>
> class Template
> def initialize
> @blocks = [lambda{}, lambda{}]
> end
> attr_reader :blocks
> end
>
> t1 = Template.new
> b = t1.blocks.first
> t2 = eval("self", b)
> t1 == t2 # => true
>
> Regards,
> Pit


A block in his question isn't the same as a ruby syntactic block. I think
he means a block of content of a web page.

--Ken



--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
  Réponse avec citation
Vieux 11/03/2008, 19h56   #5
Pit Capitain
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Determine "owning" class?

2008/3/11, Ken Bloom <kbloom@gmail.com>:
> A block in his question isn't the same as a ruby syntactic block. I think
> he means a block of content of a web page.


Ken, how do you create (quoting the OP) "an array of Blocks" with
syntactic blocks?

Regards,
Pit

  Réponse avec citation
Vieux 12/03/2008, 14h32   #6
Ken Bloom
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Determine "owning" class?

On Tue, 11 Mar 2008 13:56:49 -0500, Pit Capitain wrote:

> 2008/3/11, Ken Bloom <kbloom@gmail.com>:
>> A block in his question isn't the same as a ruby syntactic block. I
>> think
>> he means a block of content of a web page.

>
> Ken, how do you create (quoting the OP) "an array of Blocks" with
> syntactic blocks?


That's what you just did, creating an array of empty lambdas.

He defined (somewhere)
class Block
...
end

you instead, filled the array with objects of class Proc, which carry
bindings and you can identify self from a Proc. You can't do the same for
Block, the way I have defined it above.

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
  Réponse avec citation
Vieux 12/03/2008, 17h21   #7
Pit Capitain
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Determine "owning" class?

2008/3/12, Ken Bloom <kbloom@gmail.com>:
> He defined (somewhere)
> class Block
> ...
> end


Ah, finally I see what you mean. Yes, you could be right that the OP
was doing something like this. Sorry, I had problems to dissociate the
word "block" from Ruby's blocks here on ruby-talk...

Thanks,
Pit

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h51.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15544 seconds with 15 queries