Discussion:
Proposing new warning
Miguel A Salinas II
2018-10-23 11:46:55 UTC
Permalink
Hi all,
I’m wondering where to pitch an idea and implementation of a new warning. I found that new ruby programmers get confused between strings and symbols especially when using them as keys in hashes. I made a small change that warns when there is a value in the hash with a symbol as its key but you are trying to access it with the equivalent string. E.g.
hash = {this: “that”}
puts hash[“this”]

Warning: no value stored for key “this”. Value found for equivalent symbol key :this. Did you mean to use a symbol?

Any pointers for how to propose this would be greatly appreciated. Thanks!

- miguel

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/opt
Victor Shepelev
2018-10-23 12:58:17 UTC
Permalink
You can use an official bug/proposal tracker: https://bugs.ruby-lang.org/
(it is open for anybody).

About your proposal: I like the friendliness, but I am afraid there would
be performance implications, probably.
Post by Miguel A Salinas II
Hi all,
I’m wondering where to pitch an idea and implementation of a new warning.
I found that new ruby programmers get confused between strings and symbols
especially when using them as keys in hashes. I made a small change that
warns when there is a value in the hash with a symbol as its key but you
are trying to access it with the equivalent string. E.g.
hash = {this: “that”}
puts hash[“this”]
Warning: no value stored for key “this”. Value found for equivalent symbol
key :this. Did you mean to use a symbol?
Any pointers for how to propose this would be greatly appreciated. Thanks!
- miguel
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Greg Navis
2018-10-23 13:18:10 UTC
Permalink
Also, existing apps may start generating lots of warnings. What if I store
both string and symbols in a hash?

Best
Greg
Walter Lee Davis
2018-10-23 14:50:16 UTC
Permalink
I would trigger the warning only after a key miss, so the overhead would only happen in case of a completely missing key. So if you had { foo: 'bar', 'foo' => 'baz', 'boo' => 'blarg' } and you asked for test['foo'] and test[:foo], you would get bar and baz, but if you asked for test[:boo], you would get the warning.

Walter
Also, existing apps may start generating lots of warnings. What if I store both string and symbols in a hash?
Best
Greg
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Ale Miralles
2018-10-23 15:43:04 UTC
Permalink
"What if I store both string and symbols in a hash?"
If I were doing that, I would appreciate a warning.

~ Ale Miralles.
https://medium/amiralles
http://amiralles.com.ar
Post by Greg Navis
Also, existing apps may start generating lots of warnings. What if I store
both string and symbols in a hash?
Best
Greg
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Hassan Schroeder
2018-10-23 17:07:33 UTC
Permalink
Post by Ale Miralles
"What if I store both string and symbols in a hash?"
If I were doing that, I would appreciate a warning.
I'm not sure I understand why -- *any* object can be a Hash key, not
only strings or symbols.

If I had a use case for requiring keys to be the same type I would
probably either subclass Hash or rely on a linter like RuboCop.
--
Hassan Schroeder ------------------------ ***@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Dan Fitzpatrick
2018-10-23 18:27:39 UTC
Permalink
I prefer the monkey patch to Hash in this case. String/Symbol keys on
hashes is one of the only thinks I don't like about Ruby. So I "divide"
my hashes by key with the "/" function:

Add this:

class Hash
def /(k)
self[k.to_sym] || self[k.to_s]
end
end

Then you can do this:

h1 = {a: 1, b: 2}
h2 = {"c" => 1, "d" => 2}

The following returns 1:

h1/:a # 1
h2/:c # 1

x = 'a'
h1/x # 1

One issue is if you append another function you have to use parenthesis:

h = {now: Time.now}
h/:now.strftime('%Y%m%d') #<= ERROR
(h/:now).strftime('%Y%m%d') # "20181023"

This is my solution but it is considered hackery by many. The thing I
like about it is that it is easy and I know that I am getting values by
symbol or key and you can still do stuff like:

h = {a: 1, "a" => 2} and access the values as you normally would with
h[:a] or h["a"]


Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Andy Jones
2018-10-24 07:23:13 UTC
Permalink
Post by Hassan Schroeder
If I had a use case for requiring keys to be the same type I would
probably either subclass Hash or rely on a linter like RuboCop.
I thought that subclassing the basic collection classes like Hash or Array was a bad idea? Because under the hood, their implementation was optimised, and it could lead to unforeseen effects?

Of course, you can always use composition instead of inheritance.


Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Greg Navis
2018-10-24 08:28:11 UTC
Permalink
I personally wouldn't like to see a warning like that as there are simple
methods of addressing such confusion.

Loading...