CORE - Object Instantiation and Location

#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

.

2011/5/26 Ilias L. [email protected]:

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/Kernel.html#M001397

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

On 26 Μάϊος, 13:31, Roger B. [email protected] wrote:

http://snippets.dzone.com/posts/show/2787
Yes, this seems to do it for file/line/method

See also here:

module Kernel - RDoc Documentation

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.

.

On Thu, May 26, 2011 at 3:53 PM, Ryan D.
[email protected]wrote:

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 G. II

On May 26, 2011, at 12:55 , Ilias L. 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

On 26 Μάϊος, 13:31, Roger B. [email protected] wrote:

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

See also here:

module Kernel - RDoc Documentation

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 .

How can I user the string “” or “class:MyClass” to retrieve
the actual object of main or MyClass?

.

On Fri, May 27, 2011 at 7:46 AM, Christopher D. [email protected]
wrote:

object.
While that’s true, lots of Ruby has lots of features that, by default,
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”

On Thu, May 26, 2011 at 2:35 PM, James G. [email protected]
wrote:

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.

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 }

On 27 Μάϊος, 17:54, Christopher D. [email protected] wrote:

On Fri, May 27, 2011 at 7:46 AM, Christopher D. [email protected] […]

Binding.of_caller.eval “self”

http://extensions.rubyforge.org/rdoc/classes/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:

found within the answer to this question:

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?

.

On 27 Μάϊος, 07:51, Ilias L. [email protected] wrote:

http://snippets.dzone.com/posts/show/2787
How can I user the string “” or “class:MyClass” to retrieve
the actual object of main or MyClass?

for "class:MyClass’

obj = Kernel.const_get(“MyClass”) #=> returns object which represents
MyClass

for “main”?

How can I retrieve the “main” object?

.

On Mon, May 30, 2011 at 9:55 AM, Ilias L. [email protected]
wrote:

How can I retrieve the “main” object?
If you are in the top level, then self is that object:

puts self
puts self.class

If not, here is one way:

class Test
def give_me_main
eval ‘self’, TOPLEVEL_BINDING
end
end

o = Test.new.give_me_main
puts o
puts o.class

Jesus.

On Mon, May 30, 2011 at 11:05 AM, Ilias L. [email protected]
wrote:

for “main”?

How can I retrieve the “main” object?
[…]
eval ‘self’, TOPLEVEL_BINDING
[…]

Very nice, this works fine!

The approach is broken because you will never properly track anonymous
objects. E.g.

class Bar; end

class Factory
def create
Bar.new
end
end

bar = Factory.new.create

now our creator is gone

If you want to make this traceable you will keep significantly more
objects alive than without creation tracking.

I suggest you settle with file and line number which should be
sufficient in the general case to identify the source location. With
a bit of coding you can even make this very memory savvy so the
overhead is far less than if you keep hold on the creator instance.

Cheers

robert

On 30 Μάϊος, 18:00, Robert K. [email protected] wrote:
[…] - (setting requirement “anonymous objects”, suggesting
processing)

No need to suggest me requirements and processing.

My use-case is solved (I have no requirement for “anonymous objects”).

Within this thread, there is a link to solution on C-level (sender),
but for now I don’t need it.

If you know other existent libraries which address this issue,
please mention it.

.

On Tue, May 31, 2011 at 3:40 AM, Ilias L. [email protected]
wrote:

On 30 , 18:00, Robert K. [email protected] wrote:
[…] - (setting requirement “anonymous objects”, suggesting
processing)

No need to suggest me requirements and processing.

Since you didn’t post a requirement yourself (you asked for a
particular solution) I took the liberty to suggest one commonly seen
during debugging.

My use-case is solved (I have no requirement for “anonymous objects”).

Glad to see your problem has been solved (whatever it is).

Cheers

robert

On 30 Μάϊος, 11:06, Jesús Gabriel y Galán [email protected]
wrote:

On Mon, May 30, 2011 at 9:55 AM, Ilias L. [email protected] wrote:
[…]

for "class:MyClass’

obj = Kernel.const_get(“MyClass”) #=> returns object which represents
MyClass

for “main”?

How can I retrieve the “main” object?
[…]
eval ‘self’, TOPLEVEL_BINDING
[…]

Very nice, this works fine!

.

On 31 Μάϊος, 11:28, Robert K. [email protected] wrote:

On Tue, May 31, 2011 at 3:40 AM, Ilias L. [email protected] wrote:

On 30 ÌÜúïò, 18:00, Robert K. [email protected] wrote:
[…] - (setting requirement “anonymous objects”, suggesting
processing)

No need to suggest me requirements and processing.

Since you didn’t post a requirement yourself (you asked for a
particular solution) I took the liberty to suggest one commonly seen
during debugging.
[…]

You are right (subjecting the requirement).

I apologize!

Note to readers:

I’ve not verified the quality of this solution, but it seems to be the
cleanest implementation (for 1.9)

.