ljnissen
ljnissen

Reputation: 139

The #call function on my own classes

I know I can use the #call function on Proc objects, like so:

def you_called(object)
     object.call
end

a_proc = lambda {puts "I'm a Proc"}
you_called a_proc

But is there also a way to use the #call function on my own classes and if so how do I implement it?

My thinking goes along the lines like this:

class My_own_class
   def some_method
      puts "Hi from My_own_class#some_method"
   end
end

object = My_own_class.new

Upvotes: 1

Views: 34

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

Yes you can, and here is how:

class Callable
  def self.class_method
    puts "I am a class method"
  end
  def instance_method
    puts "I am an instance method"
  end
end

Callable.method(:class_method).call
Callable.new.method(:instance_method).call

Upvotes: 2

Related Questions