Project

General

Profile

Actions

Bug #11802

closed

Hash.new and {} have different behaviour

Added by aufi (Marek Aufart) over 8 years ago. Updated over 8 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
[ruby-core:72032]

Description

It looks that {} is not recognized as a variable sometimes.

irb(main):001:0> p Hash.new
{}
=> {}
irb(main):002:0> p {}
=> nil

Similar in rspec test:

expect(subject.something).to eq {} # fails on not given argument for eq
expect(subject.something).to eq({}) # works

Updated by aufi (Marek Aufart) over 8 years ago

It looks like a block, but:

irb(main):001:0> {}.class
=> Hash
irb(main):002:0> {}
=> {}

Updated by Hanmac (Hans Mackowiak) over 8 years ago

p {} is parsed as p() { }

you see it when you try:

p { :key => "value"} #which does raise a Syntax Error

you need ()

p({ :key => "value"}) # => {:key=>"value"}

Updated by marcandre (Marc-Andre Lafortune) over 8 years ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0