Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Enum#to_s to wrap flag enums with parens #3961

Conversation

makenowjust
Copy link
Contributor

This fixes is to display multiple enum flags in collection. For example:

@[Flags]
enum Foo
  A
  B
end

p [Foo::A, Foo::B, Foo::A | Foo::B]

It outpus:

[A, B, A, B]

This output is ambigious, so this commit fixes it to wrap multiple flag enums with parens. Then, it outputs:

[A, B, (A, B)]

It looks good. Thanks.

For example:

    @[Flags]
    enum Foo
      A
      B
    end

    p [Foo::A, Foo::B, Foo::A | Foo::B]

It outpus:

    [A, B, A, B]

This output is ambigious, so this commit fixes it to wrap multiple flag
enums with parens:

    [A, B, (A, B)]
@bcardiff
Copy link
Member

WDYT about adding a skip_parens (default: false) named argument to the to_s?
If I want to print only one enum flag value I would not want those extra parens.

@makenowjust
Copy link
Contributor Author

#to_s for usual enum (not flag) is useful sometime, but I think #to_s for flag enum is useful only for debugging
in many case. If you want another manipulation, you can use #each method.

@bew
Copy link
Contributor

bew commented Feb 5, 2017

I would think about piping possibilities, instead of surrounding them with parens:

[A, B, A | B] 

I find it more consistent with how you write it in code

@ysbaddaden
Copy link
Contributor

I agree for a pipe, it better represents the underlying value, and avoids the issue altogether.

@makenowjust
Copy link
Contributor Author

Honestly, I thought same thing at first, but also I need to respect current implementation, so I fixed as this way.

makenowjust added a commit to makenowjust/crystal that referenced this pull request Feb 6, 2017
@bcardiff
Copy link
Member

bcardiff commented Feb 9, 2017

Closed in favor of #4005

@bcardiff bcardiff closed this Feb 9, 2017
bcardiff pushed a commit that referenced this pull request Feb 9, 2017
@makenowjust makenowjust deleted the fix/enum/to_s_wrap_multiple_flags branch February 9, 2017 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants