RFC Future Ruby hash literal syntax

M

Michael Kaelbling

REQUEST FOR COMMENTS: Change to future Ruby hash literal syntax

In 1.9, if you prefer to omit parentheses and you write this:

h = { a: Array.new 5, 'A', b: 0, c: 1 } # Syntax error

you will get a syntax error. The following works:

h = { a: Array.new(5, 'A'), b: 0, c: 1 }

because the parser is not confused by the argument-separating comma
and the element-separating comma.

I propose that the owners consider changing the syntax.

ALTERNATIVE 1: Same input, different interpretation

If the scanner does lookahead and returns tASSOC_SEP for a comma
that is followed by a label, then there is no ambiguity.

ALTERNATIVE 2: Use ';' to separate elements, use ',' for arguments

h = { a: Array.new 5, 'A'; b: 0; c: 1 } # Proposal 2

This avoids shift-reduce conflicts by a simple change to the assocs
production in parse.y, perhaps. Unfortunately the semicolon looks
strange, especially in array literals, when we address those, too.

ALTERNATIVE 3: Allow ';' as an element separator, but keep ','

h = { a: Array.new 5, 'A'; b: 0, c: 1 } # Proposal 3

This does not break working code. It is like 'or' versus '||',
and 'do' vs. '{', i.e., same role, different precedence.
 
R

Ryan Davis

I never understand ppl's obsession with Array.new and Hash.new, not =
including hash's block initializer. Hash.new {...} is brilliant and I =
love it. I think all variants or Array.new are essentially useless and =
stick to [] for everything.

And, of course:

BenString.new("my string is better with BenString")
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

And, of course:

BenString.new("my string is better with BenString")

Who uses string literals anymore? I switched to using BenStrings
exclusively.
 
Y

Yossef Mendelssohn

I think all variants or Array.new are essentially useless and stick to []=
for everything.

What's your [] equivalent of `Array.new(3) { Thing.new }`?
 
Y

Yossef Mendelssohn

[] for everything.
What's your [] equivalent of `Array.new(3) { Thing.new }`?

BenArray.new(3, Thing.new) works great!

I've yet to use BenArray, but assuming it follows BenString
conventions:
=3D> [#<Object:0x5136b4>, #<Object:0x5136a0>, #<Object:0x51363c>]

My question still stands.
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

I think all variants or Array.new are essentially useless and stick to []
for everything.

What's your [] equivalent of `Array.new(3) { Thing.new }`?

Using an inline block like that mandates parens around the argument(s)
anyway so it's moot in this context.
 
S

Sam Duncan

ruby-1.9.2-p0 > ([1]*3).map! {|x| Object.new}
=> [#<Object:0x00000000a12778>, #<Object:0x00000000a12750>,
#<Object:0x00000000a12728>]

??


I think all variants or Array.new are essentially useless and stick to [] for everything.
What's your [] equivalent of `Array.new(3) { Thing.new }`?
BenArray.new(3, Thing.new) works great!
I've yet to use BenArray, but assuming it follows BenString
conventions:
=> [#<Object:0x5136b4>, #<Object:0x5136a0>, #<Object:0x51363c>]

My question still stands.
 
S

Sam Duncan

Oops, just realised I didn't need the x

ruby-1.9.2-p0 > ([1]*3).map! {Object.new}
=> [#<Object:0x00000000a4b848>, #<Object:0x00000000a4b820>,
#<Object:0x00000000a4b7f8>]

Sam


ruby-1.9.2-p0 > ([1]*3).map! {|x| Object.new}
=> [#<Object:0x00000000a12778>, #<Object:0x00000000a12750>,
#<Object:0x00000000a12728>]

??


On Nov 29, 2010, at 19:40 , Yossef Mendelssohn wrote:

I think all variants or Array.new are essentially useless and
stick to [] for everything.
What's your [] equivalent of `Array.new(3) { Thing.new }`?
BenArray.new(3, Thing.new) works great!
I've yet to use BenArray, but assuming it follows BenString
conventions:
Array.new(3, Object.new)
Array.new(3) { Object.new }
=> [#<Object:0x5136b4>, #<Object:0x5136a0>, #<Object:0x51363c>]

My question still stands.
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

Oops, just realised I didn't need the x

ruby-1.9.2-p0 > ([1]*3).map! {Object.new}
=> [#<Object:0x00000000a4b848>, #<Object:0x00000000a4b820>,
#<Object:0x00000000a4b7f8>]

You don't need the map! either, and while I'm at it...

(1..3).map { Object.new }
 
B

botp

=A0 =A0(1..3).map { Object.new }

i'd do,

3.times.map{Object.new}
#=3D> [#<Object:0x94df234>, #<Object:0x94df220>, #<Object:0x94df20c>]

since when read, i feel it would be closer to Array.new(3){Object.new}
but i have no qualms about Array.new notations though and i used them
all the time :)

best regards -botp
 
Y

Yossef Mendelssohn

i'd do,

3.times.map{Object.new}
#=3D> [#<Object:0x94df234>, #<Object:0x94df220>, #<Object:0x94df20c>]

I wasn't aware that was possible, but it makes sense since `3.times`
returns an enumerator.

It's also the only example that doesn't feel like an overt attempt to
avoid using the right tool for the job.
 
B

Brian Candler

Tony Arcieri wrote in post #965007:
What's your [] equivalent of `Array.new(3) { Thing.new }`?

Using an inline block like that mandates parens around the argument(s)
anyway so it's moot in this context.

Parens not needed with do/end:
=> [#<Thing:0x7f69a1a5e210>, #<Thing:0x7f69a1a5e1e8>,
#<Thing:0x7f69a1a5e198>]
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

I think all variants or Array.new are essentially useless and stick to []
for everything.

What's your [] equivalent of `Array.new(3) { Thing.new }`?
Don't even need to go that far, even the String version has this issue.

a = ['A'] * 5
a.first << 'B'
a # => ["AB", "AB", "AB", "AB", "AB"]
 
R

Ryan Davis

I've yet to use BenArray, but assuming it follows BenString
conventions:
=> [#<Object:0x5136b4>, #<Object:0x5136a0>, #<Object:0x51363c>]

My question still stands.

No! BenArray performs like the latter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top