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.
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.
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:
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:
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.
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)
.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.