Feature #13676
closedto_s method is not overriden for Set
Added by razor (Marat Chardymov) over 8 years ago. Updated over 8 years ago.
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 8 years ago
          
          
        
        
          
            Actions
          
          #1
            [ruby-core:81754]
          Updated by nobu (Nobuyoshi Nakada) over 8 years ago
          
          
        
        
          
            Actions
          
          #1
            [ruby-core:81754]
        
      
      - 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 8 years ago
          
          
        
        
          
            Actions
          
          #2
            [ruby-core:81755]
          Updated by razor (Marat Chardymov) over 8 years ago
          
          
        
        
          
            Actions
          
          #2
            [ruby-core:81755]
        
      
      nobu (Nobuyoshi Nakada) wrote:
What do you want to do?
If you want a list of the elements, useto_a.
If you want to see what elements it has, useinspect.
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 8 years ago
          
          
        
        
          
            Actions
          
          #3
            [ruby-core:81756]
          Updated by zverok (Victor Shepelev) over 8 years ago
          
          
        
        
          
            Actions
          
          #3
            [ruby-core:81756]
        
      
      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 8 years ago
          
          
        
        
          
            Actions
          
          #4
            [ruby-core:81757]
          Updated by shevegen (Robert A. Heiler) over 8 years ago
          
          
        
        
          
            Actions
          
          #4
            [ruby-core:81757]
        
      
      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 8 years ago
          
          
        
        
          
            Actions
          
          #5
            [ruby-core:82052]
          Updated by knu (Akinori MUSHA) over 8 years ago
          
          
        
        
          
            Actions
          
          #5
            [ruby-core:82052]
        
      
      - Status changed from Feedback to Assigned
- Assignee set to knu (Akinori MUSHA)
Makes sense. I'll add to_s as an alias to inspect.
        
           Updated by knu (Akinori MUSHA) over 8 years ago
          
          
        
        
          
            Actions
          
          #6
          Updated by knu (Akinori MUSHA) over 8 years ago
          
          
        
        
          
            Actions
          
          #6
        
      
      - Status changed from Assigned to Closed