0

Is it (in any way, no matter how weird or ugly) possible to call an object like a method. I'm not talking about method objects or calling an object's method, I mean this:

class Klass
  def callMeMethod *args
    p args
  end
end

myObject = Klass.new

myObject *args  #=> [arg0, arg1, ...]
myObject(*args) #=> [arg0, arg1, ...]

I thought about defining a new global method each time an object is created (never mind how terrible that is) but I ran into problems, also I would ideally like to call it even if it's returned from some expression such as:

someArray[index_of_my_object] *args  #=> [arg0, arg1, ...]
someArray[index_of_my_object](*args) #=> [arg0, arg1, ...]

So basically Python's __call__ method.

thanks

Asone Tuhid
  • 539
  • 4
  • 13
  • thinks link it's of your interest http://stackoverflow.com/questions/35400337/ruby-send-vs-call-method – catch22 Apr 15 '17 at 17:35
  • no, I'm talking about calling the object as if it were a method (`object variable # => result`) – Asone Tuhid Apr 15 '17 at 17:47
  • Implement `#call` and `#[]` methods just like [`Proc`](http://ruby-doc.org/core-2.4.1/Proc.html) and [`Method`](http://ruby-doc.org/core-2.4.1/Method.html). I think that would be more a natural interface in Ruby. – mu is too short Apr 15 '17 at 17:47
  • Yeah, yeah, more natural is good and I know I can define methods, I'm talking about a very specific thing that, as far as I understand, is completely against the concept of ruby methods and yet I'm sure there's a way to hack it together – Asone Tuhid Apr 15 '17 at 17:49
  • I think you're taking the wrong approach here. Write Ruby when you're working with Ruby, don't try to make it behave like some other language. Why do you need to do it this way? – mu is too short Apr 15 '17 at 17:53
  • Partly just because I should be able to and partly because doing useless and ridiculous things like this helps me understand the language better – Asone Tuhid Apr 15 '17 at 17:56
  • I don't think you can, otherwise you probably wouldn't have methods like [`#Array` in `Kernel`](http://ruby-doc.org/core-2.4.1/Kernel.html#method-i-Array). `.()` is sugar for `.call` though so you can `proc.(6)` and `proc.call(6)` are the same thing. IMO the way to understand Ruby is to program in Ruby, not try to program Python in Ruby. – mu is too short Apr 15 '17 at 18:02
  • That's just conformist. You're right for formal programming but finding weird hacks is a nice way to appreciate how the language works like infix operators, it might be trivial but it's a good way to learn overloading – Asone Tuhid Apr 15 '17 at 18:20
  • I think you could do it with [binding_of_caller](https://github.com/banister/binding_of_caller) and `method_missing`. I don't think it's possible with pure Ruby, though. – Jordan Running Apr 15 '17 at 20:30
  • Thanks but that's not really the point, as you said, the idea is completely pointless, I only really want to know if and how this can be done. `.()` is so close but that dot is bugging me. – Asone Tuhid Apr 15 '17 at 20:46

1 Answers1

2

No.

This has been asked many, many times over many, many years.

Yehuda Katz wrote a blog post about it, titled Ruby is NOT a Callable Oriented Language (It's Object Oriented), and the title says pretty much what the Ruby community thinks about this. Ruby is centered around objects, not functions. Message sending is the fundamental operation, not function calls.

There is an ambiguity between a message send with no explicit receiver and a function call. Scala solves this ambiguity with static typing, but Ruby can't do that, so the ambiguity is simply resolved by not having function calls, or rather by using a different syntax for function calls (foo.(bar) instead of foo(bar)).

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653