https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112013-10-24T00:29:31ZRuby Issue Tracking SystemRuby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=425742013-10-24T00:29:31Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>I'd prefer to reserve this syntax as a short hash syntax for string keyed hashes:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">{</span>
<span class="s1">'string'</span><span class="p">:</span> <span class="s1">'v'</span> <span class="c1"># equivalent to 'string' => 'v'</span>
<span class="p">}</span>
</code></pre>
<p>If your proposal is accepted, then it wouldn't be possible add the short syntax support for strings.</p>
<p>Do you have any real-world use case where a symbol would be useful but you need the :'xxx' constructor to generate it? I don't think this is a common thing to happen...</p> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=425752013-10-24T00:37:16Zjamonholmgren (Jamon Holmgren)jamon@clearsightstudio.com
<ul></ul><p>I would be okay with your idea, Rodrigo, although it's less consistent (if you look at my first code block in the description). It would result in symbols looking like this:</p>
<p>{<br>
:'symbol': 'v'<br>
}</p>
<p>Not the end of the world, though. Would you then also allow other literals to use the colon, not just strings / symbols?</p>
<p>{<br>
[ 1, 2, 3 ]: 'v'<br>
}</p>
<p>I realize these are edge cases, but it's worth considering.</p> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=425762013-10-24T00:40:23Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>yes, it makes sense to me to accept anything as a key. The only problem is that we can't use names in variables with this syntax :(</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">key</span> <span class="o">=</span> <span class="s1">'a'</span>
<span class="p">{</span><span class="ss">key: </span><span class="mi">1</span><span class="p">}</span> <span class="c1"># will be {:key => 1}, not {'a' => 1}</span>
</code></pre>
<p>Alternatively we could do:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">{</span><span class="s2">"</span><span class="si">#{</span><span class="n">key</span><span class="si">}</span><span class="s2">"</span><span class="p">:</span> <span class="mi">1</span><span class="p">}</span>
</code></pre>
<p>But it wouldn't be shorter in such case :P</p> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=425772013-10-24T00:54:58Zmohawkjohn (John Woods)john.o.woods@gmail.com
<ul></ul><p>This may or may not be related, but we here at NMatrix (part of SciRuby) would love to be able to index ranges in NMatrix using a 1:3 notation. This can be accomplished with a hash, but only if it will allow numeric (rather than symbolic keys) before the colon. Currently we have to type:</p>
<pre><code>n[1=>3, 2=>4]
</code></pre>
<p>or use the .../.. notation, but we would prefer</p>
<pre><code>n[1:3,2:4]
</code></pre> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=425792013-10-24T02:03:24Zjamonholmgren (Jamon Holmgren)jamon@clearsightstudio.com
<ul></ul><p>I still think my original suggestion is more consistent and has fewer implications, but would like further input.</p> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=425862013-10-24T11:21:02Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li><li><strong>Target version</strong> set to <i>2.6</i></li></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/7546">@mohawkjohn (John Woods)</a> let us separate the issue. There may be a chance to introduce num:num literals for your purpose (just maybe).</p>
<p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/7474">@jamonholmgren (Jamon Holmgren)</a> having Symbol GC, it is realistic to have that kind of notation to cope well with JSON. But I am still not sure how much useful this is. Any use-case?</p>
<p>Matz.</p> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=425892013-10-24T11:56:14Zmohawkjohn (John Woods)john.o.woods@gmail.com
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/13">@matz (Yukihiro Matsumoto)</a> Very well, and thank you for the consideration. I opened a new issue on that topic: <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Shorthands (a:b, *) for inclusive indexing (Open)" href="https://bugs.ruby-lang.org/issues/9049">#9049</a>.</p> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=430992013-11-23T15:53:49Zjamonholmgren (Jamon Holmgren)jamon@clearsightstudio.com
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/13">@matz (Yukihiro Matsumoto)</a> -- sorry, I didn't receive an email notification, so I didn't realize you had responded.</p>
<p>This isn't MRI, I realize, but in RubyMotion this notation would come in handy.</p>
<p><a href="https://github.com/clearsightstudio/ProMotion/issues/345#issuecomment-29115788" class="external">https://github.com/clearsightstudio/ProMotion/issues/345#issuecomment-29115788</a></p>
<pre><code>add UIButton.new, {
"setTitle:forState:" => ['Register', UIControlStateNormal],
"addTarget:action:forControlEvents:" => [self, 'register', UIControlEventTouchUpInside]
}
# becomes:
add UIButton.new, {
"setTitle:forState:": ['Register', UIControlStateNormal],
"addTarget:action:forControlEvents:": [self, 'register', UIControlEventTouchUpInside]
}
</code></pre>
<p>Another situation is when you want to map strings:</p>
<pre><code>def map_string(source_string)
{
"jamon-holmgren": "Jamon A. Holmgren",
"matzumoto-yukihiro": "Yukihiro Matsumoto"
}[source_string]
end
</code></pre>
<p>Perhaps somewhat contrived, but you get the point. The first (RubyMotion) example I do deal with on a regular basis, being the creator of ProMotion.</p> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=441952014-01-10T00:58:18Zjamonholmgren (Jamon Holmgren)jamon@clearsightstudio.com
<ul></ul><p>I should also mention this allows for similar syntax between JavaScript, Python, and Ruby. In this case, all three languages could translate the same dictionary/hash in a more or less compatible way.</p> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=457892014-03-14T23:02:01Zjamonholmgren (Jamon Holmgren)jamon@clearsightstudio.com
<ul></ul><p>Looks like there is a patch already for this:</p>
<p><a href="https://bugs.ruby-lang.org/issues/4276" class="external">https://bugs.ruby-lang.org/issues/4276</a></p> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=457952014-03-15T02:21:39Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Is duplicate of</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/4276">Feature #4276</a>: Allow use of quotes in symbol syntactic sugar for hashes</i> added</li></ul> Ruby master - Feature #9047: Alternate hash key syntax for symbolshttps://bugs.ruby-lang.org/issues/9047?journal_id=489902014-09-20T01:48:53Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset r47649.</p>
<hr>
<p>parse.y: quoted ID key</p>
<ul>
<li>parse.y (assoc): allow quoted ID as a key of a hash literal.<br>
<a href="/issues/4276">[ruby-core:34453]</a> [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Allow use of quotes in symbol syntactic sugar for hashes (Closed)" href="https://bugs.ruby-lang.org/issues/4276">#4276</a>]</li>
</ul>