Feature #6697

[PATCH] Add Kernel#Symbol conversion method like String(), Array() etc.

Added by Mark Dodwell almost 2 years ago. Updated over 1 year ago.

[ruby-core:46172]
Status:Feedback
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:-
Target version:next minor

Description

For consistency, it seems to make sense to add a Kernel#Symbol helper.

This change would not break any existing code and it provides a more consistent interface.

Thoughts?

0001-Add-Kernel-Symbol-conversion-method-like-String-Arra.patch Magnifier (2.42 KB) Mark Dodwell, 07/04/2012 06:25 PM

History

#1 Updated by Shota Fukumori almost 2 years ago

I can't think any use cases of Kernel.#Symbol, could you tell me the use case?

(Why Kernel.#Symbol makes sense?)

#2 Updated by Mark Dodwell almost 2 years ago

You could make the same argument against String. Why would you use that over #to_s? I can't think of any use cases for that :)

To me, it's really about API consistency. I don't believe you would actually use it frequently, but including it is following the principle of least surprise.

Fwiw. I discovered Symbol did not exist by writing some code to call it, expecting it (since there is String, Array, Hash, etc.) I doubt I'm the only one who's tried calling Symbol, expecting it to exist.

#3 Updated by Yui NARUSE almost 2 years ago

  • Status changed from Open to Feedback

String(obj) calls obj.tostr first, then calls obj.tos.
So they are different.

Integer("foo") raises ArgumentError, but "foo".toi returns 0.
Array("foo") returns ["foo"], but "foo".to
a raises NoMethodError on 1.9.
Hash(nil) returns {}, but nil.to_hash raises NoMethodError.

So your point about consistency is not reasonable.

#4 Updated by Koichi Sasada over 1 year ago

  • Assignee set to Yusuke Endoh
  • Target version changed from 2.0.0 to next minor

No feedback there.

#5 Updated by Yusuke Endoh over 1 year ago

  • Assignee changed from Yusuke Endoh to Yukihiro Matsumoto

In Ruby, it is not a good idea to propose something because of consistency.

Yusuke Endoh mame@tsg.ne.jp

#6 Updated by Martin Dürst over 1 year ago

On 2012/10/27 20:12, mame (Yusuke Endoh) wrote:

Issue #6697 has been updated by mame (Yusuke Endoh).

Assignee changed from mame (Yusuke Endoh) to matz (Yukihiro Matsumoto)

In Ruby, it is not a good idea to propose something because of consistency.

I guess this should say:

In Ruby, it is not a good idea to propose something only because of
consistency.

Consistency is good, but (as possibly in this proposal) adding a method
just for consistency, without demonstrating an actual need, isn't Ruby
style. So please show some strong uses cases for this method.

Regards, Martin.

Also available in: Atom PDF