Project

General

Profile

Feature #13676

to_s method is not overriden for Set

Added by razor (Marat Chardymov) over 1 year ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:81753]

Description

When I call

s1 = Set.new
s1<<'tic'<<'tac'
s1.to_s

I'd expect ['tic', 'tac'] values being printed, not "#Set:0x0055f331076348"

Associated revisions

Revision d893c123
Added by knu (Akinori MUSHA) over 1 year ago

Alias Set#to_s to #inspect [ruby-core:81753] [Feature #13676]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59332 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 59332
Added by knu (Akinori MUSHA) over 1 year ago

Alias Set#to_s to #inspect [ruby-core:81753] [Feature #13676]

Revision 59332
Added by knu (Akinori MUSHA) over 1 year ago

Alias Set#to_s to #inspect [ruby-core:81753] [Feature #13676]

Revision 59332
Added by knu (Akinori MUSHA) over 1 year ago

Alias Set#to_s to #inspect [ruby-core:81753] [Feature #13676]

History

Updated by nobu (Nobuyoshi Nakada) over 1 year ago

  • Status changed from Open to Feedback
  • Description updated (diff)

What do you want to do?
If you want a list of the elements, use to_a.
If you want to see what elements it has, use inspect.

Updated by razor (Marat Chardymov) over 1 year ago

nobu (Nobuyoshi Nakada) wrote:

What do you want to do?
If you want a list of the elements, use to_a.
If you want to see what elements it has, use inspect.

I'm printing several sets in my erb. It would be nice have such to_s behaviour by default, eliminating the need to call to_a on each of them before print

Updated by zverok (Victor Shepelev) over 1 year ago

What do you want to do?
If you want a list of the elements, use to_a.
If you want to see what elements it has, use inspect.

Isn't it just a decent behavior for any value object to have a readable to_s representation?.. Say, for cases like puts-debugging (puts "Comparing #{set1} to #{set2}...") or developer-friendly error messages (raise "Input was expected to have 4 elements, #{set} received").

Updated by shevegen (Robert A. Heiler) over 1 year ago

I have no pro or con opinion. I did however had want to compare Set to Array and the two behave differently.

    require 'pp'
    require 'set'

    s1 = Set.new
    s1<<'tic'<<'tac'
    puts s1.to_s
    pp s1

    array = Array.new
    array << 'tic' << 'tac'
    puts array.to_s
    pp array

    # Output:
    #
    # <Set:0x810460c4>
    # <Set: {"tic", "tac"}>
    # ["tic", "tac"]
    # ["tic", "tac"]

I have no idea why Set behaves that way, perhaps there is a clear reason.

I can however had understand razor too - without knowing the context or
really having a lot of experience with Set mayself, to me the behaviour
of Array seems "more useful" by default. But again, I have no real idea
about this so neither can I say good or bad if it would be changed - I
really don't know. I only use Arrays, barely ever Set myself. :)

Updated by knu (Akinori MUSHA) over 1 year ago

  • Assignee set to knu (Akinori MUSHA)
  • Status changed from Feedback to Assigned

Makes sense. I'll add to_s as an alias to inspect.

#6

Updated by knu (Akinori MUSHA) over 1 year ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r59332.


Alias Set#to_s to #inspect [ruby-core:81753] [Feature #13676]

Also available in: Atom PDF