Checking for an instance method - ruby

Is there a class method to check if there is a certain instance method for that class? Something like respond_to? that is a class method.

Yes, you can use
method_defined?
,which is class method to check whether particular class has instance method defined or not.
Following link will explain you more
Given a class, see if instance has method (Ruby)

Use method_defined? on Class
Ex: Array.method_defined? :sort

Related

In the context of ruby modules, what is the difference between a self.method and a regular method?

So, from what I understand, in a class, a self.method is like a class method, whereas a regular method is like an instance method. But, as you can't instantiate a module, why would I use one over the other? What is the difference between these two in a module?
So, from what I understand, in a class, a self.method is like a class method, whereas a regular method is like an instance method.
That is not true. Ruby only has one single kind of method: instance methods. So, what you call a "regular method" is not "like" an instance method, it is an instance method. And there are no class methods in Ruby, every method is an instance method.
There are two ways to define a method in Ruby: with an explicit definee (def foo.bar) and without (def bar, which is then using the default definee).
In the first case, it is easy to know where a method will be defined: if you say def foo.bar, the method will become an instance method of foo's singleton class, always.
If you say def bar, the method becomes an instance method of the default definee, which usually, but not always, is the closest lexically enclosing module definition body. The most notable exception is a method that is defined directly in a script body, in this case, there is no lexically enclosing module definition, and the default definee is Object instead. In fact, there is even another twist: the method will be implicitly defined as private as well.
So, in your case, the first method will become an instance method of the singleton class of self (and inside a module definition body, self is the module itself), the second method will become an instance method of the module.
But, as you can't instantiate a module,
Ruby has a feature called inheritance. While it is true that you can directly instantiate a module, you can instantiate a class that inherits from a module. In fact, since the Object class inherits from the Kernel module (which has instance methods like Kernel#puts or Kernel#require, which may have used once or twice), and almost every class directly or indirectly inherits from Object, it is highly likely that you will have in your Ruby programming already have instantiated an indirect instance of a module and used a module's instance methods.
Enumerable is another important module in Ruby that contains methods like map, select, etc. that both Array and Hash inherit from.
why would I use one over the other?
There's really not much to it: you'd use an instance method, if you want to … well use an instance method, i.e. want to have a method that has privileged access to the internal representation of self across many different instances of the same module.
You would use a method of the singleton class, if you want the method to be only available for that single object.
There is one and only one reason for having module methods: to invoke those methods on the module in which they defined (using self: def self.my_method...). An example of this is Ruby's built-in Math module. All methods in this module are module methods. These methods are all invoked with Math as their explicit receiver (e.g., Math.sqrt(2.5)). In effect, they are used like functions in non-OO languages.
When you include, prepend or extend a module from a class, any module methods are simply ignored.
You might find this (timely) article helpful in understanding the Ruby object model and how methods are “looked up”: https://www.honeybadger.io/blog/ruby-method-lookup/

How to test IsKindOfClass in Xamarin.iOS?

Seems simple but I can't find the answer anywhere?
I just want to test whether an object is an instance of a certain class.
There's the IsKindOfClass method, but what argument can I pass it?
I wanted to write it like in Objective-C :
if (view.IsKindOfClass(AdvicePageView.Class)) {
but the Class method / attribute doesn't exist.
How can I pass my class to IsKindOfClass?
this is basic C#
if (MyView is MyTypeOfView) then
where MyView is an instance and MyTypeOfView is a class

Can I access a private class method from a public instance method without using the class name?

I have a class method, self.calculate, that I want to invoke from both another class method and from an instance method.
If I write
def green
calculate(x,y,z)
end
though, I get a NoMethodError: undefined method 'calculate' exception. Invoking self.calculate gets the same problem.
What does work is calling MyClassName.calculate, but that seems somehow wrong and messy. What is the correct practice in this situation?
You can use
self.class.calculate
to call a class methods from an instance method.

If I define a method in Ruby, does it belong to any class?

I have feeling, that if one defines a method
def test
puts 'Hi'
end
then there is a class to which this method belongs to (i.e. Unknown#test). So one probably has a possibility to list all methods defined "outside" of other classes. Or there is another way to do such listing?
If you define a method outside of any class, it will become a private method of the Object class.
A top-level method is a private method of Object.
Check out this question.
In future, to find what object a method belongs to, do this:
method(:test).owner
Output, for your example is Object
And you can then list all the methods of Object with
Object.send(:methods)
or
Object.send(:private_methods)

Ruby: How do I get the methods of a class without having an object of it?

"abc".respond_to?(:sub) returns true, but String.respond_to?(:sub) returns false.
The second returns false, because it asks whether objects of the class Class have a method sub, as String is an Object of Class. It is the same for methods()…
How do I do these things and especialy respond_to?() without creating an Object of that class.
You can use method_defined? method, declared in Module class.
If you are trying to confirm whether a method exists, String.method_defined? :sub will work. If you are specifically interested in instance methods, use something like:
String.instance_methods.index 'sub'
Note that you have to use a string, not a symbol.

Resources