Project

General

Profile

Feature #7748

Contextual send

Added by trans (Thomas Sawyer) over 7 years ago. Updated over 6 years ago.

Status:
Open
Priority:
Normal
Target version:
[ruby-core:51719]

Description

=begin
If I write a method that uses #send vs. #public_send, I am making an assumption about how that method is invoked. For example, take the simplest form of such a method:

class String
def send_out(op, *a, &b)
send(op, *a, &b)
end
end

This code has a bug in it, in the sense that it can be used to call private string methods. The solution is to use #public_send. In most cases that will be fine. But if anyone tries to reuse the method while extending String themselves, e.g.

class String
def send_out(op, *a, &b)
public_send(op, *a, &b)
end

def some_public_method
  send_out(:some_private_method)
end

private
def some_private_method
end

end

Then it will be a problem b/c it cannot be used on a private supporting method.

So it seems like there should be something like a ((contextual send)) which invokes a send with the same visibility as the parent method is invoked. e.g.

class String
def send_out(op, *a, &b)
contextual_send(op, *a, &b)
end
end

And then all cases will work as expected.
=end

Also available in: Atom PDF