Courtland Caldwell
Courtland Caldwell

Reputation: 578

Invoke `?:` using `send` in Ruby

Can I / how would I go about calling the ternary operator using the send :name, *args, &blk syntax construct? I don't have a practical reason to do this, but it came up.

Here you can see my woeful attempts:

i = 7

send "?:", proc { i > 2 }, true, false
# => NoMethodError: undefined method `?:' for main:Object

proc { i > 2 }.send "?:", true, false
# => NoMethodError: undefined method `?:' for #<Proc:0x00000001ef77b0@(irb):3>

proc { i > 2 }.call.send "?:", true, false
# => NoMethodError: undefined method `?:' for true:TrueClass

Upvotes: 0

Views: 46

Answers (1)

sawa
sawa

Reputation: 168111

There is no way to do that. The ternary operator construction, or the ? part of it, is not a method; as you noticed, it is a syntactic construct. Meanwhile, send can only handle methods.

Upvotes: 1

Related Questions