Find source_location of a method when you have the Class - ruby

I am looking into catching every method that is defined on a base class, looking up what file it is defined in, and then doing some logic based on that.
I currently have:
# Defined in some file
class Subclass < Base
def foo
end
end
class Base
self.method_added(method)
# self is a given subclass (Subclass)
# This doesn't work. :(
self.method(method).source_location
end
end
What I'd like to be able to do is find out the source location of that method.
I could do something like:
self.new.method(source).source_location
But don't think I should be having to instantiate anything to get this to work.
Any ideas?

You can use method Module#instance_method to get instance method of your class:
instance_method(method).source_location # `self` is unnecessary, it is added implicitly
# => ["/home/alex/Projects/test/test.rb", 23]
instance_method(symbol) → unbound_method
Returns an UnboundMethod representing the given instance method in mod.

Related

Why can some classes and/or methods be called without instances of their parent class?

I'm near the finish of the Ruby track in Code Academy, and I'm curious about a peculiar thing: I was under the impression that a class is a repository of constants, methods, etc... and that in order to access most of them, you would first need to create an instance of that class or in some cases the methods of themselves can be invoked (as in they are all technically part of the global object). And then I saw something like this:
#Worked
Time.now
I understood as this as the method [now] of instance of class [Time] being invoked. I then tried to invoke the method on its own:
#Failed
now
and that failed, and I assumed that while a method can be created in the general scope [as part of the global object], if it relies on initialized variables of "parent" class, it cannot be called on its own, because it would not know which object to search for those initialized variables. Following that I created a test class:
class Clock
def initialize
#hours = 1
#minutes = 30
end
def showTime
puts "The time is: #{#hours}:#{#minutes}"
end
end
#this worked
watch = Clock.new
watch.showTime
#this failed
showTime
I then just created a basic method (assuming it's in the global level)
def mymethod
puts "The mighty METHOD!"
end
#Works
mymethod
and calling this method the way I did, without referencing the global object worked. So... the questions I have are as follows:
How can [Time.now] be called in this fashion? Shouldn't there be an instance of Time first created?
Why can't I call the method [now] on its own? Am I right that it relies on resources that it cannot find when called this way?
Why could I not call the method showTime on its own? But if I define any method on the "global" level I can access it without referencing the global object
First of all, your intuition is correct.
Every methods must be an instance method of some receiver.
Global methods are defined as private instance methods on Object class and hence seem to be globally available. Why? From any context Object is always in the class hierarchy of self and hence private methods on Object are always callable without receiver.
def fuuuuuuuuuuun
end
Object.private_methods.include?(:fuuuuuuuuuuun)
# => true
Class methods are defined as instance methods on the "singleton class" of their class instance. Every object in Ruby has two classes, a "singleton class" with instance methods just for that one single object and a "normal class" with method for all objects of that class. Classes are no different, they are objects of the Class class and may have singleton methods.
class A
class << self # the singleton class
def example
end
end
end
A.singleton_class.instance_methods.include?(:example)
# => true
Alternative ways of defining class methods are
class A
def self.example
end
end
# or
def A.example
end
Fun fact, you can define singleton methods on any object (not just on class objects) using the same syntax def (receiver).(method name) as follows
str = "hello"
def str.square_size
size * size
end
str.square_size
# => 25
"any other string".square_size
# => raises NoMethodError
Some programming language history — Singleton classes are taken from the Smalltalk language where they are called "metaclasses". Basically all object-oriented features in Ruby (as well as the functional-style enumerators on Enumerable) are taken from the Smalltalk language. Smalltalk was an early class-based object-oriented language created in the 70ies. It was also the language that invented graphical user interfaces like overlapping windows and menus et cetera. If you love Ruby maybe also take a look at Smalltalk, you might fall in love yet again.
This is known as a class method. If CodeAcademy didn't cover it, that's a shame. Here's some examples:
# basic way
class Foo
def self.bar; :ok; end
end
Foo.bar # => :ok
# alternate syntax
class Foo
class << self
def bar; :ok; end
end
end
# alternate syntax, if Foo class already exists
def Foo.bar; :ok; end
# alternate approach if Foo class already exists
Foo.class_exec do
def bar; :ok; end
end
# to define a class method on an anonymous 'class' for a single instance
# you won't need to use this often
Foo.new.singleton_class.class_exec do
def bar; :ok; end
end
# to define a class method on an instance's actual class
Foo.new.class.class_exec do
def bar; :ok; end
end
Another way to get class methods is to extend a module.
module FooMethods
def bar; :ok; end
end
module Foo
extend FooMethods
end
Foo.bar # => :ok
Note that with Modules, the methods are always defined as instance methods. This way they can be either extended into class scope or included into instance scope. Modules can also have class methods, using the exact same syntax / examples as shown above with classes. However there's not such as easy to load a module's class methods via include or extend.
How can [Time.now] be called in this fashion? Shouldn't there be an
instance of Time first created?
The Time.now method is a class method, not an instance method and therefore can be called directly on the Time class rather than an instance of it Time.new
Class methods are defined on the class themselves using the self keyword:
class Time
def self.now
# code
end
end
Time.now # works
Why can't I call the method [now] on its own? Am I right that it
relies on resources that it cannot find when called this way?
When you call a method "on its own" you're actually implicitly calling it on self:
self.now
The above is the same as just doing:
now
Why could I not call the method showTime on its own? But if I define
any method on the "global" level I can access it without referencing
the global object
You defined the showTime method on a specific class so you have to send that method to that class. When you define a method in the "global" scope you're implicitly defining it on self and the subsequent call to mymethod is actually self.mymethod so it will work.
Time.now is a class method.
To define a class method, you need to define the method with self. : def self.method_name
class Clock
#hours = 1
#minutes = 30
def self.showTime
puts "The time is: #{#hours}:#{#minutes}"
end
end
Clock.showTime
#=> The time is: 1:30
If you want to call now on its own, you can do so inside Time class :
class Time
puts now
#=> 2017-01-19 22:17:29 +0100
end

Can I use Forwardable to delegate a constant in Ruby?

Given a class like:
class Thing
CONSTANT = 10
# other code
end
And another like:
class Other
def initialize
#thing = Thing.new
end
def method
puts CONSTANT
end
end
Is it possible to extend Forwardable to have Other's CONSTANT delegate to Thing's constant?
I have tried extendingSingleForwardable (see docs here ) like so:
class Other
extend SingleForwardable
def initialize
#thing = Thing.new
end
def method
puts CONSTANT
end
def_single_delegator :#thing, :CONSTANT
end
and:
def_single_delegator :#thing, :constant
but neither worked, am I getting the syntax wrong? If not, is there another way?
This one is answered by the documentation (bold emphasis mine):
def_single_delegator(accessor, method, new_name=method)
Defines a method method which delegates to accessor (i.e. it calls the method of the same name in accessor). If new_name is provided, it is used as the name for the delegate method.
So, no, you can only delegate message sends, not constant lookup.
Forwardable (and SingleForwardable) let you add class functions to your current class via a module. When you write a delegator you're specifying which methods are being passed on to the receiving object from the original module.
In your example, def_single_delegator :#thing, :CONSTANT is being interpreted as the following: Defines a method :CONSTANT which delegates to accessor :#thing (i.e. it calls the method of the same name in accessor).
As you can see, this is not passing on the value of the constant, instead it's passing on a method with the same name as the constant. If you want to pass on the same value as the constant you can provide an accessor method that returns to you the value of the original constant so that you can assign it in this class.

Subclassing a class that has a "factory" method

I have a class that has a "factory" method which returns new instances given a file_name. Depending on the type of file given it needs to construct the object differently. It also happens to be a swig generated class wrapping a c++ class, I am not sure that matters, but I am including that detail just in case it does. So I have this class defined somewhere, which has among other things this new_from_file method
class Wv::WvWaveList
def self.new_from_file file_name
...
Wv::WaveList.new
end
....
end
I wanted to add a method copy_wave, so my first thought was to subclass and add it there so something like this.
class MyWaveList < Wv::WvWaveList
def copy_wave
...
end
end
The problem is that new_from_file still returns a Wv::WaveList not a MyWaveList so I can't call copy_wave on instances returned by new_from_file
One simple solution is to just open the class up here and add the method
class Wv::WvWave
def copy_wave
...
end
end
Another solution would be to have MyWaveList have an instance of a Wv::WaveList and delegate all the appropriate calls to that instance.
So I am just wondering what the inheritance solution might be? I just don't see it right now.
I believe this should work
class Wv::WvWaveList
def self.new_from_file file_name
...
self.new
end
....
end
Because self.new_from_file was always calling Wv::WaveList.new, it was always instantiating objects with that class, even from subclasses. But by using self, you'll be able to call new_from_file on any subclass, and the objects will be of the correct class:
>> a = MyWaveList.new_from_file "some_file"
=> #<MyWaveList:0x007fd473004318 #file_name="some_file">
>> a.class
=> MyWaveList

Ruby. How to know which class instance method is defined?

I want to know which class method_missing is defined.
It is defined in Object.
How can I figure out which class along the hierarchy overrides it?
You can use UnboundMethod#owner method to check where the method is implemented:
class A
def method_missing(*args)
# do something
end
end
method = A.instance_method(:method_missing)
method.owner
# => A
Note: If the method is implemented in module (which is later mixed into the class hierarchy somewhere), owner will return this module.

Why does defining a method at the top level affect other user-defined classes?

If I define a top level method like this:
def inspect(x)
# do something useful...
end
Then calling #inspect on a user-defined class stops working:
class Foo; end
p Foo.new # `inspect': wrong number of arguments (0 for 1) (ArgumentError)
However, it keeps working for classes like NilClass and String:
p nil # prints 'nil'
p "test" # prints '"test"'
I thought that one explanation for this behaviour could be that top-level execution may be in the Object class itself, but it turns out that it's in an instance of Object called main. Doesn't this mean that methods defined here shouldn't affect other classes?
main is a special place. Any methods defined there are defined as private instance methods of Object. This is so you can define pseudo-functions that can be called in any context without an explicit receiver.

Resources