0

在 C++ 中,我可以这样做:

(condition ? sin : cos)(0.5);

或者

typedef std::deque<int> T;
(T().*(condition ? &T::push_back : &T::push_front))(1);

在 Ruby 中,这相当于什么?

我知道我可以使用sendor method,但它们允许我调用private方法。

# String#puts and String#print are private
("".method condition ? :puts : :print).call
4

1 回答 1

2

send是使用 Ruby 的方式。如果您不喜欢它,因为它允许调用私有方法,请public_send改用:

Math.public_send(condition ? :sin : :cos, 0.5)
于 2012-11-10T14:10:22.007 回答