@SuppressWarnings("unchecked")
    public static <T> T deep(final T input) {
        if (input instanceof Map<?, ?>) {
            return (T) deepMap((Map<?, ?>) input);
        } else if (input instanceof List<?>) {
            return (T) deepList((List<?>) input);
        } else if (input instanceof RubyString) {
            // new instance but sharing ByteList (until either String is modified)
            return (T) ((RubyString) input).dup();
        } else if (input instanceof Collection<?>) {
            throw new ClassCastException("unexpected Collection type " + input.getClass());
        }

        return input;
    }
@Test
    public void testRubyStringCloningAndChangeOriginal() {
        String javaString = "fooBar";
        RubyString original = RubyString.newString(RubyUtil.RUBY, javaString);

        RubyString result = Cloner.deep(original);

        ThreadContext context = RubyUtil.RUBY.getCurrentContext();
        IRubyObject index = RubyUtil.RUBY.newFixnum(5);
        original.op_aset(context, index, RubyUtil.RUBY.newString("z")); // original[5] = 'z'

        assertNotEquals(result, original);

        assertTrue(result.op_equal(context, RubyString.newString(RubyUtil.RUBY, javaString)).isTrue());
        assertEquals(javaString, result.asJavaString());
        assertEquals("fooBaz", original.asJavaString());
    }