Project

General

Profile

Actions

Feature #5478

open

Add syntax to import Set into core

Added by rkh (Konstantin Haase) about 13 years ago. Updated about 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:40311]

Description

Set is a central data structure. However, a lot of Ruby developers use arrays for situations where it would be more reasonable to use a set. One reason for that is that it is way easier to use Array than Set at the moment. Another one is that developers are simply not aware that it exists.

I propose to move Set from stdlib to core and possibly add a literal syntax or a method on array for creating a Set.

First class syntax suggestions:

<1, 2, 3>  # might be tricky to parse
#[1, 2, 3] # would collide with comments
$[1, 2, 3]
${1, 2, 3}

Method suggestions:

~[1, 2, 3]
+[1, 2, 3]

Whitespace separated String Sets could look like this:

%w<foo bar blah> # creates an array at the moment 
#w[foo bar blah] # would collide with comments
$w[foo bar blah] # would collide with sending :[] to $w
$w{foo bar blah}

~%w[foo bar blah] # not really shorter than using an array with strings
+%w[foo bar balh] # not really shorter than using an array with strings

Maybe it's ok to not have a whitespace separated syntax. I'm just brainstorming here.

An issue with the method approach is that it would create an Array first to which the message would be sent.

I favor the <1, 2, 3> syntax, possibly without the ability to create a whitespace separated version.

I'd be willing to work on a patch not only for MRI but also for JRuby and Rubinius if you would consider this to be useful although I would need help with the parser.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0