Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
String#capitalize!
() -> self?
(:ascii | :lithuanian | :turkic) -> self?
(:lithuanian, :turkic) -> self?
(:turkic, :lithuanian) -> self?
capitalize!(*options) → self or nil
string.c
View on GitHub ↗
static VALUE
rb_str_capitalize_bang(int argc, VALUE *argv, VALUE str)
{
rb_encoding *enc;
OnigCaseFoldType flags = ONIGENC_CASE_UPCASE | ONIGENC_CASE_TITLECASE;
flags = check_case_options(argc, argv, flags);
str_modify_keep_cr(str);
enc = str_true_enc(str);
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
if (flags&ONIGENC_CASE_ASCII_ONLY)
rb_str_ascii_casemap(str, str, &flags, enc);
else
str_shared_replace(str, rb_str_casemap(str, &flags, enc));
if (ONIGENC_CASE_MODIFIED&flags) return str;
return Qnil;
}
Upcases the first character in self; downcases the remaining characters; returns self if any changes were made, nil otherwise:
Example
s = 'hello World!' # => "hello World!" s.capitalize! # => "Hello world!" s # => "Hello world!" s.capitalize! # => nil
The casing may be affected by the given options; see Case Mapping.
Related: String#capitalize.