Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > CORE - Object Instantiation and Location

Reply
Thread Tools

CORE - Object Instantiation and Location

 
 
Ilias Lazaridis
Guest
Posts: n/a
 
      05-26-2011
#ruby 1.9
class Person
def initialize(name)
@name = name
@where = "don't know" #
end

def report
print "\n", @name, " instantiated in: ", @where, "\n"
end
end

p = Person.new("Nancy")
p.report

-

What I'm looking for:

how can I detect WHERE the object was instantiated?

* module
* class
* file name
* line number
*
something like

def initialize(name)
@name = name
@where = self.execution.called_from # Object
@where = self.execution.file_name # String with filename

..

--
http://lazaridis.com
 
Reply With Quote
 
 
 
 
Roger Braun
Guest
Posts: n/a
 
      05-26-2011
2011/5/26 Ilias Lazaridis <(E-Mail Removed)>:
>
> What I'm looking for:
>
> how can I detect WHERE the object was instantiated?


This seems to do what you need.

http://snippets.dzone.com/posts/show/2787

See also here:

http://www.ruby-doc.org/core/classes...l.html#M001397

--
Roger Braun
rbraun.net | humoralpathologie.de

 
Reply With Quote
 
 
 
 
Bala TS
Guest
Posts: n/a
 
      05-26-2011
I think you can use class variable


like this
@@name = name
@@where = "don't know"

if you initialization then you can get like this way
=> Nancy ................ Don't knonw

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      05-26-2011
On 26 Μάϊος, 13:31, Roger Braun <(E-Mail Removed)> wrote:
> 2011/5/26 Ilias Lazaridis <(E-Mail Removed)>:
>
>
>
> > What I'm looking for:

>
> > how can I detect WHERE the object was instantiated?

>
> This seems to do what you need.
>
> http://snippets.dzone.com/posts/show/2787


Yes, this seems to do it for file/line/method

> See also here:
>
> http://www.ruby-doc.org/core/classes...l.html#M001397


That seems to contain the solution for the function name.

And this is the right documentation, but I could not find (within
Object and Kernel) the construct I'm looking for (accessing calling
Module or Object).

#ruby 1.9
class Person
def initialize(name)
@name = name
@where = self.invoked_from # returns Object
end
def report
print "\n", @name, " instantiated in: ", @where, "\n"
end
end

p = Person.new("Nancy")
p.report #=> Nancy instantiated in: main

Is there something like "self.invocation_from", or how could this be
implemented?

Note that this should work on object model, returning a "living"
object.

..

--
http://lazaridis.com
 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      05-26-2011

On May 26, 2011, at 12:55 , Ilias Lazaridis wrote:

> Is there something like "self.invocation_from", or how could this be
> implemented?
>
> Note that this should work on object model, returning a "living"
> object.


BARRIER - Bad Design Smell


 
Reply With Quote
 
James Gray
Guest
Posts: n/a
 
      05-26-2011
[Note: parts of this message were removed to make it a legal post.]

On Thu, May 26, 2011 at 3:53 PM, Ryan Davis <(E-Mail Removed)>wrote:

>
> On May 26, 2011, at 12:55 , Ilias Lazaridis wrote:
>
> > Is there something like "self.invocation_from", or how could this be
> > implemented?
> >
> > Note that this should work on object model, returning a "living"
> > object.

>
> BARRIER - Bad Design Smell
>


I laughed so hard!

Just to make it clear for those following along: this feature would violate
multiple principals of object oriented programming and just good design,
which is probably why there's not a big push for it.

James Edward Gray II

 
Reply With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      05-27-2011
On 26 Μάϊος, 13:31, Roger Braun <(E-Mail Removed)> wrote:
> 2011/5/26 Ilias Lazaridis <(E-Mail Removed)>:
>
>
>
> > What I'm looking for:

>
> > how can I detect WHERE the object was instantiated?

>
> This seems to do what you need.
>
> http://snippets.dzone.com/posts/show/2787
>
> See also here:
>
> http://www.ruby-doc.org/core/classes...l.html#M001397


This one could lead to a solution, at least a temporal one.

I can retrieve from the Kernel.caller the call stack, and from there
the information <main>.

How can I user the string "<main>" or "<class:MyClass>" to retrieve
the actual object of main or MyClass?

..

--
http://lazaridis.com
 
Reply With Quote
 
Christopher Dicely
Guest
Posts: n/a
 
      05-27-2011
On Thu, May 26, 2011 at 2:35 PM, James Gray <(E-Mail Removed)> wrote:
> On Thu, May 26, 2011 at 3:53 PM, Ryan Davis <(E-Mail Removed)>wro=

te:
>
>>
>> On May 26, 2011, at 12:55 , Ilias Lazaridis wrote:
>>
>> > Is there something like "self.invocation_from", or how could this be
>> > implemented?
>> >
>> > Note that this should work on object model, returning a "living"
>> > object.

>>
>> BARRIER - Bad Design Smell
>>

>
> I laughed so hard!
>
> Just to make it clear for those following along: =C2=A0this feature would=

violate
> multiple principals of object oriented programming and just good design,
> which is probably why there's not a big push for it.


While that's true, lots of Ruby has lots of features that, by default,
expose functionality whose use would violate principals of object
oriented programming and good design. This is generally a good thing,
IMO, because such design principals are generalities and, as such,
usually have specific instances in practice where excessive devotion
to them is counterproductive. The power to ignore them is often a good
thing for a language to have.

And, really, a frequently requested feature by Ruby users (which I
think was implemented in at least one Gem for pre-1.9 ruby, don't know
if there is anything that provides it for 1.9) has been something to
the effect of Binding.of_caller that would return the binding of the
point from which the current method was called; if such a method
existed, Ilias request would seem to just be solved by:

Binding.of_caller.eval { self }

 
Reply With Quote
 
Christopher Dicely
Guest
Posts: n/a
 
      05-27-2011
On Fri, May 27, 2011 at 7:46 AM, Christopher Dicely <(E-Mail Removed)> wr=
ote:
> On Thu, May 26, 2011 at 2:35 PM, James Gray <(E-Mail Removed)> wrote=

:
>> On Thu, May 26, 2011 at 3:53 PM, Ryan Davis <(E-Mail Removed)>wr=

ote:
>>
>>>
>>> On May 26, 2011, at 12:55 , Ilias Lazaridis wrote:
>>>
>>> > Is there something like "self.invocation_from", or how could this be
>>> > implemented?
>>> >
>>> > Note that this should work on object model, returning a "living"
>>> > object.
>>>
>>> BARRIER - Bad Design Smell
>>>

>>
>> I laughed so hard!
>>
>> Just to make it clear for those following along: =C2=A0this feature woul=

d violate
>> multiple principals of object oriented programming and just good design,
>> which is probably why there's not a big push for it.

>
> While that's true, lots of Ruby has lots of features that, by default,
> expose functionality whose use would violate principals of object
> oriented programming and good design. This is generally a good thing,
> IMO, because such design principals are generalities and, as such,
> usually have specific instances in practice where excessive devotion
> to them is counterproductive. The power to ignore them is often a good
> thing for a language to have.
>
> And, really, a frequently requested feature by Ruby users (which I
> think was implemented in at least one Gem for pre-1.9 ruby, don't know
> if there is anything that provides it for 1.9) has been something to
> the effect of Binding.of_caller that would return the binding of the
> point from which the current method was called; if such a method
> existed, Ilias request would seem to just be solved by:
>
> Binding.of_caller.eval { self }


Or, more likely:

Binding.of_caller.eval "self"

 
Reply With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      05-29-2011
On 27 Μάϊος, 17:54, Christopher Dicely <(E-Mail Removed)> wrote:
> On Fri, May 27, 2011 at 7:46 AM, Christopher Dicely <(E-Mail Removed)> [...]


> > And, really, a frequently requested feature by Ruby users (which I
> > think was implemented in at least one Gem for pre-1.9 ruby, don't know
> > if there is anything that provides it for 1.9) has been something to
> > the effect of Binding.of_caller that would return the binding of the
> > point from which the current method was called; if such a method
> > existed, Ilias request would seem to just be solved by:

>
> > Binding.of_caller.eval { self }

>
> Or, more likely:
>
> *Binding.of_caller.eval "self"



http://extensions.rubyforge.org/rdoc...s/Binding.html
http://extensions.rubyforge.org/rdoc/

Yes, this should work, but it seems to have some problems, too:

http://www.ruby-forum.com/topic/175402

-

There is another solution, which I like more:

https://github.com/Asher-/sender

found within the answer to this question:

http://stackoverflow.com/questions/2...caller-in-ruby

-

But I try to achieve it without C-level extensions, only with the
build in (1.9) object-model / reflection / *documented* call-stack
tracing etc.

The question is:

How to achieve this in a "clean" way, without going to C-level?

..

--
http://lazaridis.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CORE - Literal Instantiation breaks Object Model Ilias Lazaridis Ruby 4 06-21-2011 02:31 AM
Xilinx multiplier core instantiation for Virtex4 D Stanford VHDL 8 05-10-2007 07:37 PM
Location, location, location =?Utf-8?B?VHJhY2V5?= Wireless Networking 2 02-17-2007 08:37 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
Core Solo & Core Duo are not Core microarchitecture; 65nm Pentium M chips bigal Hardware 0 03-22-2006 11:24 AM



Advertisments