Project

General

Profile

Feature #6697

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

Added by Mark Dodwell over 3 years ago. Updated over 3 years ago.

Status:
Feedback
Priority:
Normal
[ruby-core:46172]

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 [ruby-core:46234] Updated by sorah Shota Fukumori over 3 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 [ruby-core:46235] Updated by Mark Dodwell over 3 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 [ruby-core:46261] Updated by Yui NARUSE over 3 years ago

  • Status changed from Open to Feedback

String(obj) calls obj.to_str first, then calls obj.to_s.
So they are different.

Integer("foo") raises ArgumentError, but "foo".to_i 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 [ruby-core:48385] Updated by Koichi Sasada over 3 years ago

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

No feedback there.

#5 [ruby-core:48469] Updated by Yusuke Endoh over 3 years 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 [ruby-core:48535] Updated by Martin Dürst over 3 years 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