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
Related
I have task here: I must create a class which will be like class Struct. I have just started to learn Ruby and don't know how to create it. In my attempts, I was able to create a method that takes arguments and creates an array. Then I have to go through the array and all of the arguments, make the argument class, in which I can bring some value. But I don't know how to create methods that can create classes in Ruby.
I am asking for help if you have an example or know where to find it, I will be grateful!
my attempts:
class Hobbit
def new(*params)
"#{params.inspect}"
end
end
So I assume all you want is
And what i want it is an example of a method that can create classes
Check out this question and the accepted answer which shows you how to create a class dynamically given a class name. Let me know if that's what you're looking for.
If I want to call a class method (mailer method in rails), providing its name in a variable. How can I do that? For objects, we can use send, or we can use read_attribute to read some values
my_object.send("#{self.action_type(self)}")
my_object.read_attribute("#{self.action_type}_email")
But for class names, nothing is working as send is defined as instance method in object class. I want something like this, which will not work as send can't be applied on class:
Notifier.send("#{self.action_type(self)}").deliver
Use eval
eval("Notifier.#{self.action_type}(self).deliver")
Not safe but it should work.
Classes are objects. There is no difference in how you apply send.
Notifier.send(action_type, self).deliver
You can also do:
method = Notifier.method(action_type)
method.call(self).deliver
you can also use
self.class.send(:your_class_method)
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
I know about controller_name returning a string containing the controller's name but how can I retrieve the controller class (or object) from within a helper?
EDIT: The solution should also work when the controller is namespaced (eg. Admin::PostsController)
You can use the constantize method, like:
controller_name.constantize
Though I'm not sure how it will behave if you have a namespaced controller.
Update:
That one won't work for all controller names and/or namespaces. Though one can use the #controller method in combination with #class:
controller.class
A view probably shouldn't need to do this. Ideally whatever you're trying to do in the view that expects this, you would instead do in the controller.
Trying to think about why you'd want to do this, the best answer I can think of is that you want to invoke a helper method you've defined in the controller. There already exists a construct to do this, use helper_method.
For pretty much anything else, the controller should provide that data to the view. Not the view pulling it out of the controller. (e.g. even though you shouldn't need the class, the controller could provide it with #controller_class = self.class, which would then be available to the view)
In pure Ruby, because class names are constants, you can do this to get the class from a string:
classname = 'Posts'
p Kernel.const_get(classname).methods
There is a nice shortcut in Rails, constantize for just this:
p 'Posts'.constantize.methods
If the classname is eg 'editable_file', first call the camelize method:
p 'editable_file'.camelize.constantize # EditableFile
p 'extensions/editable_file'.camelize.constantize # Extensions::EditableFile
EDIT: If you really want to get the controller name un-demodulized, then this code in config/initializers/controller_name.rb should ensure it:
class ActionController::Metal
def self.controller_name
# #controller_name ||= self.name.demodulize.sub(/Controller$/, '').underscore
#controller_name ||= self.name.sub(/Controller$/, '').underscore
end
end
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)