From b5fbefbf2c14742f6d46ecdf3ce712062dfb1d0a Mon Sep 17 00:00:00 2001 From: "NARUSE, Yui" Date: Sun, 17 Nov 2019 23:24:59 +0900 Subject: Added Symbol#start_with? and Symbol#end_with? method. [Feature #16348] --- string.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'string.c') diff --git a/string.c b/string.c index e58be68621..22ced326b8 100644 --- a/string.c +++ b/string.c @@ -11075,6 +11075,46 @@ sym_swapcase(int argc, VALUE *argv, VALUE sym) return rb_str_intern(rb_str_swapcase(argc, argv, rb_sym2str(sym))); } +/* + * call-seq: + * sym.start_with?([prefixes]+) -> true or false + * + * Returns true if +sym+ starts with one of the +prefixes+ given. + * Each of the +prefixes+ should be a String or a Regexp. + * + * :hello.start_with?("hell") #=> true + * :hello.start_with?(/H/i) #=> true + * + * # returns true if one of the prefixes matches. + * :hello.start_with?("heaven", "hell") #=> true + * :hello.start_with?("heaven", "paradise") #=> false + */ + +static VALUE +sym_start_with(int argc, VALUE *argv, VALUE sym) +{ + return rb_str_start_with(argc, argv, rb_sym2str(sym)); +} + +/* + * call-seq: + * sym.end_with?([suffixes]+) -> true or false + * + * Returns true if +sym+ ends with one of the +suffixes+ given. + * + * :hello.end_with?("ello") #=> true + * + * # returns true if one of the +suffixes+ matches. + * :hello.end_with?("heaven", "ello") #=> true + * :hello.end_with?("heaven", "paradise") #=> false + */ + +static VALUE +sym_end_with(int argc, VALUE *argv, VALUE sym) +{ + return rb_str_end_with(argc, argv, rb_sym2str(sym)); +} + /* * call-seq: * sym.encoding -> encoding @@ -11361,5 +11401,8 @@ Init_String(void) rb_define_method(rb_cSymbol, "capitalize", sym_capitalize, -1); rb_define_method(rb_cSymbol, "swapcase", sym_swapcase, -1); + rb_define_method(rb_cSymbol, "start_with?", sym_start_with, -1); + rb_define_method(rb_cSymbol, "end_with?", sym_end_with, -1); + rb_define_method(rb_cSymbol, "encoding", sym_encoding, 0); } -- cgit v1.2.3