Project

General

Profile

Bug #14086

Interpreter seems to sneak in a nil value when a comma is missing on a nested array wrapped in parenthesis

Added by mariogintili (Mario Gintili) over 1 year ago. Updated over 1 year ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:83686]

Description

I believe the title is self explanatory. But code is always louder than words:

$ ruby --version
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin16]
$ irb
2.2.4 :001 > ([[2,2], [3,3], [4,4], [5,5], [6,6]])
 => [[2, 2], [3, 3], [4, 4], [5, 5], [6, 6]] 
2.2.4 :002 > ([[2,2] [3,3], [4,4], [5,5], [6,6]])
 => [nil, [4, 4], [5, 5], [6, 6]] 

the only difference between lines 1 and 2 in my REPL is a coma right after the first nested array. is this the expected behaviour?

History

Updated by zverok (Victor Shepelev) over 1 year ago

is this the expected behaviour?

Yes.

Try this:

[2, 2][3, 3] # => nil

Why?
Because it is read as "Array of [2,2], call operator [] with arguments 3, 3 (which means fetch 3 elements starting from 3rd)". As this array doesn't have a 3rd element, the result of the operation is nil. So the first item of your larger array is the result of this calculation, e.g. nil.

#2

Updated by marcandre (Marc-Andre Lafortune) over 1 year ago

  • Status changed from Open to Rejected

Also available in: Atom PDF