pub(crate) fn replace_all_block(
        vm: &mut VM,
        re_val: Value,
        given: &str,
        block: &Block,
    ) -> Result<(String, bool), RubyError> {
        fn replace_(
            vm: &mut VM,
            re: &RegexpInfo,
            given: &str,
            block: &Block,
        ) -> Result<(String, bool), RubyError> {
            let mut range = vec![];
            let mut i = 0;
            let f = vm.eval_block_map1(block);
            loop {
                let (start, end, matched_str) = match re.captures_from_pos(given, i) {
                    Ok(None) => break,
                    Ok(Some(captures)) => {
                        let m = captures.get(0).unwrap();
                        i = m.end() + if m.start() == m.end() { 1 } else { 0 };
                        vm.get_captures(&captures, given);
                        (m.start(), m.end(), m.as_str())
                    }
                    Err(err) => {
                        return Err(RubyError::internal(format!("Capture failed. {:?}", err)))
                    }
                };
                let matched = Value::string(matched_str);
                let result = f(vm, matched)?;
                let replace = result.val_to_s(vm)?.into_owned();
                range.push((start, end, replace));
            }

            let mut res = given.to_string();
            for (start, end, replace) in range.iter().rev() {
                res.replace_range(start..end, replace);
            }
            Ok((res, range.len() != 0))
        }

        if let Some(s) = re_val.as_string() {
            let re = vm.regexp_from_escaped_string(&s)?;
            return replace_(vm, &re, given, block);
        } else if let Some(re) = re_val.as_regexp() {
            return replace_(vm, &re, given, block);
        } else {
            return Err(RubyError::argument("1st arg must be RegExp or String."));
        };
    }
Replaces all non-overlapping matches in `given` string with `replace`.
ruruby/src/builtin/regexp.rs