Capitalize Every Word In String

Trying to capitalize each word in a fields value. I initially tried the titleize function of Ruby, but that didn't work...I guess because it's not part of Ruby internals or something like that?

  ruby {
    code => 'event.set("[Group]", event.get("[Group]").titleize)'
  }

Anybody got another way to do this?

There is a mutate filter for all operations like : uppercase ; capitalize ; lowercase

event.get will return a Ruby string if the field is a string. titleize is part of Ruby on Rails, not core Ruby.

None of those functions accomplish my needs.

Raw Value: jOhN dOe
Desired Value: John Doe

Uppercase: JOHN DOE
Lowercase: john doe
Capitalize: JOhN DOe

Does the Ruby filter implement Ruby on Rails or core Ruby? I guess core since the code doesn't work, or is my ruby code above incorrect?

The ruby filter does not implement Ruby on Rails, just core Ruby.

You can lowercase and then capitalize. A ruby filter does operations in the order uppercase, capitalize, lowercase. So you will need two filters

mutate { lowercase => [ "Group" ] }
mutate { capitalize=> [ "Group" ] }

This is what I tried first, but it just capitalized the first word.

Ah, yes. The filter is calling the ruby .capitalize method on the entire field, which lowercases everything and then capitalizes the first character (so the first mutate is not needed). What we need to do is chop the field up into words, capitalize each one, then put them back together.

ruby { code => 'event.set("Group", event.get("Group").split.map(&:capitalize).join(" "))' }
2 Likes

That did it. As always, thanks for the help @Badger

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.