Harry
Harry

Reputation: 4835

Activesupport titlecase() with bang (.titlecase!)?

I currently have a string with customer names that I am titlecasing with:

@customer_name = @customer_name.titlecase

But this seems a bit long-winded. When I try to do:

@customer_name.titlecase!

I get a no method error. Does titlecase! exist? It seems odd that there wouldn't be a way to do this, since there is a downcase!, for example.

Upvotes: 0

Views: 417

Answers (2)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

You can see on http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000381 that all titlecase does is this (also notice there are no bang methods)

def titleize
  Inflector.titleize(self)
end

So, if you wanted to implement this is

class String
  def titleize!
    replace titleize
  end
end

Then:

>> the_string = "oh hai"
=> "oh hai"
>> the_string.titleize!
=> "Oh Hai"
>> the_string
=> "Oh Hai"

Upvotes: 1

sawa
sawa

Reputation: 168101

downcase with or without bang are Ruby methods. titlecase is not. Perhaps, that is the reason that it does not have the bang version. Rails developers probably did not bother to define the bang version.

Upvotes: 0

Related Questions