# How to downcase an Enum member name?

**URL:** https://forum.crystal-lang.org/t/how-to-downcase-an-enum-member-name/6313
**Category:** Help & Support
**Created:** [January 12, 2024, 5:23pm UTC](https://forum.crystal-lang.org/t/how-to-downcase-an-enum-member-name/6313 "2024-01-12T17:23:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![aboulafia](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@aboulafia](https://forum.crystal-lang.org/u/aboulafia)
#### Post date: [January 12, 2024, 5:23pm UTC](https://forum.crystal-lang.org/t/how-to-downcase-an-enum-member-name/6313/1 "2024-01-12T17:23:05Z")

</div>

Me, again ;-)

```auto
enum Foo
  I8
end

```

I can’t rename I8 to i8, this is forbiden  
I can’t either

```auto
enum Foo
  I8
  def to_s
   "#{self}".downcase
  end
end

```

That leads to an infinite loop

So we have Enum.names but no .name() on each member exepted the stock to\_s ?

---

<div class="post-metadata">

### Author: ![mdwagner](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/mdwagner/32/1077_2.png) [@mdwagner](https://forum.crystal-lang.org/u/mdwagner)
#### Post date: [January 12, 2024, 6:10pm UTC](https://forum.crystal-lang.org/t/how-to-downcase-an-enum-member-name/6313/2 "2024-01-12T18:10:51Z")

</div>

I’m pretty sure Enum members have to start with an uppercase letter. You shouldn’t override `to_s`, but instead use `to_s(io : IO)` and append to the io. You can also do this for `inspect(io : IO)`, if that fits your bill better. Or you can just define another method that returns `self.to_s.downcase` (I used this pattern myself).

---

<div class="post-metadata">

### Author: ![HertzDevil](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hertzdevil/32/1023_2.png) [@HertzDevil](https://forum.crystal-lang.org/u/HertzDevil)
#### Post date: [January 12, 2024, 6:23pm UTC](https://forum.crystal-lang.org/t/how-to-downcase-an-enum-member-name/6313/3 "2024-01-12T18:23:46Z")

</div>

Except you _have_ to override `Enum#to_s`, because it is the `IO` overload that forwards to it, not the other way round.

Personally I wish there is a way to override specific members’ names without having to override either.

---

<div class="post-metadata">

### Author: ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)
#### Post date: [January 12, 2024, 7:16pm UTC](https://forum.crystal-lang.org/t/how-to-downcase-an-enum-member-name/6313/4 "2024-01-12T19:16:00Z")

</div>

The simplest way to do it:

```crystal
enum Numbers
  One
  Two
  Three

  def to_s
    super.downcase
  end
end

```

The most runtime-efficient way, avoids the heap allocation:

```crystal
enum Numbers
  One
  Two
  Three

  def to_s
    {% begin %}
      case self
      {% for member in @type.constants %}
        in .{{ member.id.downcase }}?
          "{{ member.id.downcase }}"
      {% end %}
      end
    {% end %}
  end
end

```

---

<div class="post-metadata">

### Author: ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)
#### Post date: [January 12, 2024, 7:57pm UTC](https://forum.crystal-lang.org/t/how-to-downcase-an-enum-member-name/6313/5 "2024-01-12T19:57:07Z")

</div>

Would work out well if you could annotate constants, then could do something like `JSON::Field`.

---

<div class="post-metadata">

### Author: ![lautan](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@lautan](https://forum.crystal-lang.org/u/lautan)
#### Post date: [January 13, 2024, 8:06am UTC](https://forum.crystal-lang.org/t/how-to-downcase-an-enum-member-name/6313/6 "2024-01-13T08:06:29Z")

</div>

Great line. Thanks
