Feature #13676
closed
- 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
.
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
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"
).
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. :)
- Status changed from Feedback to Assigned
- Assignee set to knu (Akinori MUSHA)
Makes sense. I'll add to_s as an alias to inspect.
- Status changed from Assigned to Closed
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0