Skip to content

Instantly share code, notes, and snippets.

@ChrisBr
Created February 19, 2018 17:53
Show Gist options
  • Save ChrisBr/1856b76b25d3663c2819bfbd37a3dc49 to your computer and use it in GitHub Desktop.
Save ChrisBr/1856b76b25d3663c2819bfbd37a3dc49 to your computer and use it in GitHub Desktop.
@JRubyMethod(name = "delete_prefix")
public IRubyObject delete_prefix(ThreadContext context, IRubyObject arg) {
IRubyObject tmp = arg.checkStringType();
if (tmp.isNil()) throw context.runtime.newTypeError("no implicit conversion of " + arg.getMetaClass().getName() + " into String");
RubyString prefix = (RubyString)tmp;
if (this.start_with_p(context, prefix).isTrue()) {
RubyString result = (RubyString) substr19(context.runtime, prefix.strLength(), this.strLength() - prefix.strLength());
return result.isEmpty() ? this : result;
} else {
return this;
}
}
@JRubyMethod(name = "delete_suffix")
public IRubyObject delete_suffix(ThreadContext context, IRubyObject arg) {
IRubyObject tmp = arg.checkStringType();
if (tmp.isNil()) throw context.runtime.newTypeError("no implicit conversion of " + arg.getMetaClass().getName() + " into String");
RubyString suffix = (RubyString)tmp;
if (this.end_with_p(context, suffix).isTrue()) {
RubyString result = (RubyString) substr19(context.runtime, 0, this.strLength() - suffix.strLength());
return result.isEmpty() ? this : result;
} else {
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment