Project

General

Profile

Bug #11604

Ruby's interpreting hash symbol keys as parts of Ruby syntax

Added by maciej.mensfeld (Maciej Mensfeld) about 4 years ago. Updated about 4 years ago.

Status:
Rejected
Priority:
Normal
Target version:
-
ruby -v:
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
Backport:
[ruby-core:71114]

Description

Cannot initialize hashes with some Ruby reserved keywords like: while, until, if, unless, do. Can with other like class, end, elsif, super. Don't really get why

{ while: 1 } # Will fail
# error.rb:3: syntax error, unexpected ':'
# print { while: 1 }

However when assigned, works perfectly

hash = { while: 1 }

But when used as a method argument - fails

run(for: 1)
run { for: 1 }

Works with old syntax

{ :if => 1 }

And with new but with quotation marks

{ 'if': 1 }

Example file included. Please just execute like this:

ruby error.rb

Files

error.rb (278 Bytes) error.rb maciej.mensfeld (Maciej Mensfeld), 10/19/2015 03:18 PM

History

Updated by shugo (Shugo Maeda) about 4 years ago

  • Status changed from Open to Feedback

Maciej Mensfeld wrote:

Cannot initialize hashes with some Ruby reserved keywords like: while, until, if, unless, do. Can with other like class, end, elsif, super. Don't really get why

These syntax errors are not caused by key names, but by the use of { just after the method names.
The following code also causes a syntax error without the new style syntax of hash literals:

print { :while => 1 }

In this case, { is not interpreted as the beginning of a hash literal, but the beginning of a block, and thus a syntax error occurs.

Updated by maciej.mensfeld (Maciej Mensfeld) about 4 years ago

That is true but IMHO it should still be considered as a bug.

Updated by nobu (Nobuyoshi Nakada) about 4 years ago

  • Status changed from Feedback to Rejected

Yes, it's your bug.

Also available in: Atom PDF