send
is the way to go in Ruby. If you don't like it because it allows to call private methods, use public_send
instead:
Math.public_send(condition ? :sin : :cos, 0.5)
質問
In C++ I can do this:
(condition ? sin : cos)(0.5);
or
typedef std::deque<int> T;
(T().*(condition ? &T::push_back : &T::push_front))(1);
What would be an equivalent of this in Ruby?
I know I can use send
or method
, but they allow me to call private
methods.
# String#puts and String#print are private
("".method condition ? :puts : :print).call
解決
send
is the way to go in Ruby. If you don't like it because it allows to call private methods, use public_send
instead:
Math.public_send(condition ? :sin : :cos, 0.5)