Project

General

Profile

Actions

Bug #10024

closed

Array injection of ternary operators injects boolean rather than ternary result

Added by blainekasten@gmail.com (Blaine Kasten) almost 10 years ago. Updated almost 10 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
2.1.2
[ruby-core:63635]

Description

The following code:

a = []
a << ''.blank? ? 'blank' : 'not blank'

puts a

renders [true].

I would expect it to render ['blank']

If I wrap it in parenthesis it does work.

a = []
a << ( ''.blank? ? 'blank' : 'not blank' )
puts a # ['blank']

it seems like the logic internally is perceiving this function as:

(a << ''.blank?) ? 'blank' : 'not blank'

Which seems illogical.

Updated by pducks32 (Patrick Metcalfe) almost 10 years ago

The #<< has a higher precedence than the ternery operator. Using #<<= seems to make it work just fine.

Updated by nobu (Nobuyoshi Nakada) almost 10 years ago

  • Description updated (diff)
  • Status changed from Open to Rejected

<< and >> are shift operators primarily, and have higher precedence.

Actions

Also available in: Atom PDF

Like0
Like0Like0