Project

General

Profile

Actions

Feature #13676

closed

to_s method is not overriden for Set

Added by razor (Marat Chardymov) over 7 years ago. Updated over 7 years ago.

Status:
Closed
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"

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

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

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 7 years 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 7 years 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 7 years 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 7 years ago

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

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

Actions #6

Updated by knu (Akinori MUSHA) over 7 years ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r59332.


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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0