Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns a string containing the characters in self, with cases reversed; each uppercase character is downcased; each lowercase character is upcased:
s = 'Hello World!' # => "Hello World!"
s.swapcase         # => "hELLO wORLD!"
The casing may be affected by the given options; see Case Mapping.
               static VALUE
rb_str_swapcase(int argc, VALUE *argv, VALUE str)
    rb_encoding *enc;
    VALUE ret;

    flags = check_case_options(argc, argv, flags);
    enc = str_true_enc(str);
    if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return str_duplicate(rb_cString, str);
    if (flags&ONIGENC_CASE_ASCII_ONLY) {
        ret = rb_str_new(0, RSTRING_LEN(str));
        rb_str_ascii_casemap(str, ret, &flags, enc);
    else {
        ret = rb_str_casemap(str, &flags, enc);
    return ret;

Was this page useful?