# \[dry-inflector\] Question about underscore and camelize behavior with dashes

**URL:** <https://discourse.dry-rb.org/t/dry-inflector-question-about-underscore-and-camelize-behavior-with-dashes/1325>\
**Category:** Support\
**Created:** [August 22, 2021, 11:17am UTC](https://discourse.dry-rb.org/t/dry-inflector-question-about-underscore-and-camelize-behavior-with-dashes/1325 "2021-08-22T11:17:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [August 22, 2021, 11:17am UTC](https://discourse.dry-rb.org/t/dry-inflector-question-about-underscore-and-camelize-behavior-with-dashes/1325/1 "2021-08-22T11:17:30Z")

</div>

Hello, I recently released [command\_kit](https://github.com/postmodern/command_kit.rb), which is a zero-dependency all-in-one kitchen-sink-included kit for building CLI apps. I borrowed some of the functions from dry-inflector since I only needed a very basic inflector for converting file names / command names to Class names. My next goal was to port command\_kit to Crystal. However, Crystal does not support `Regexp.last_match` (which it shouldn’t, because global state), so I [refactored](https://github.com/postmodern/command_kit.rb/blob/38f79fa512bb27af59f2ed2f5f0b248cb2c1fe80/lib/command_kit/inflector.rb) the inflector methods to use `StringScanner`; which improved the performance of `underscore` but slowed down `camelize`. Eventually I want to submit a PR back to dry-inflector, however my algorithm deviates slightly from dry-inflector’s original algorithm:

- `underscore` reduces multiple `_` and `-`s to a single `_` (ex: `Foo---Bar___Baz` → `foo_bar_baz`)
- `camelize` omits any `-`s (ex: `foo-bar` → `FooBar`)

This raised the question about what should the ideal behavior be? Should `-` be preserved, reduced, or entirely omitted when converting a CamelCase string to under\_scored or vice versa?

---

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [August 22, 2021, 11:29pm UTC](https://discourse.dry-rb.org/t/dry-inflector-question-about-underscore-and-camelize-behavior-with-dashes/1325/2 "2021-08-22T23:29:17Z")

</div>

Here is an example script to illustrate differences in behavior.

```ruby
#!/usr/bin/env ruby

begin
  require 'bundler/setup'
rescue LoadError => error
  abort error.message
end

require 'dry-inflector'
require 'command_kit/inflector'

dry_inflector = Dry::Inflector.new
command_kit = CommandKit::Inflector

camelcase = "Foo__BAR---Baz"
underscore = "foo___bar---baz"

puts "#{camelcase}\t-> DRY::Inflector#underscore -> #{dry_inflector.underscore(camelcase)}"
puts "#{camelcase}\t-> CommandKit::Inflector#underscore -> #{command_kit.underscore(camelcase)}"

puts

puts "#{underscore}\t-> DRY::Inflector#camelize -> #{dry_inflector.camelize(underscore)}"
puts "#{underscore}\t-> CommandKit::Inflector#camelize -> #{command_kit.camelize(underscore)}"

```

```auto
Foo __BAR---Baz	-> DRY::Inflector#underscore -> foo__ bar___baz
Foo __BAR---Baz	-> CommandKit::Inflector#underscore -> foo__ bar___baz

foo___bar---baz	-> DRY::Inflector#camelize -> FooBar---baz
foo___bar---baz	-> CommandKit::Inflector#camelize -> FooBarBaz

```

Personally, I think any underscores or dashes should be stripped when converting from under\_scored case to CamelCase. Not sure whether `-` should be preserved or omitted when converting CamelCase to under\_scored case?

---

<div class="post-metadata">

**Author:** ![swilgosz](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/swilgosz/32/682_2.png) [@swilgosz](https://discourse.dry-rb.org/u/swilgosz)\
**Post date:** [August 23, 2021, 12:05pm UTC](https://discourse.dry-rb.org/t/dry-inflector-question-about-underscore-and-camelize-behavior-with-dashes/1325/3 "2021-08-23T12:05:11Z")

</div>

I think repeated dashes should be reduced to single one and be treated as one.

However, the underscore method seems to work fine without reducing the number of characters when replacing dashes with underscores.

---

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [August 25, 2021, 4:27am UTC](https://discourse.dry-rb.org/t/dry-inflector-question-about-underscore-and-camelize-behavior-with-dashes/1325/4 "2021-08-25T04:27:05Z")

</div>

Update: I’ve fixed the regression introduced by my StringScanner algorithm where the number of `_` or `-` characters were not being preserved when converting to under\_scored. Lost some of the performance gains, but is still ~15% faster than `DRY::Inflector#underscore`.

> <https://github.com/postmodern/command_kit.rb/commit/39bc739879b8eb5f70de62c2df5b6cb60ff9b96b>
