Project

General

Profile

Bug #10870

Hash Literal Declined As First Argument

Added by NorthernLights (Imran "") about 5 years ago. Updated about 5 years ago.

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

Description

Hi,

When a hash literal is passed as first argument to a method, Ruby throws a syntax error.
Example code:

def foo *args
  p args
end

foo 'Hello'
foo 123
foo ['Hello', 123]
foo {:hello => 123}   # Syntax Error: Unexpected =>  Expecting }

However, shifting hash literal over to second place, somehow makes it all legal.

foo 'Unnecessary 1st Argument', {:hello => 123}     # Error gone

Cheers.

Updated by jacknagel (Jack Nagel) about 5 years ago

foo {} without parentheses is equivalent to foo() {}, (i.e. passing a block to foo) not foo({}).

Updated by nobu (Nobuyoshi Nakada) about 5 years ago

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

It is parsed as a block.
You don't need braces in that case.

def foo *args
  p args
end
foo :hello => 123

Or put parentheses.

foo(:hello => 123)
foo({:hello => 123})

Also available in: Atom PDF