diff options
author | NARUSE, Yui <naruse@airemix.jp> | 2019-11-17 23:24:59 +0900 |
---|---|---|
committer | NARUSE, Yui <naruse@airemix.jp> | 2019-11-28 23:49:28 +0900 |
commit | b5fbefbf2c14742f6d46ecdf3ce712062dfb1d0a (patch) | |
tree | d5367d0c20d2323ac8d030ded69efba7e6e76b8b | |
parent | 76871dea6b05ee441f25c6f694ac21525ed25f93 (diff) |
Added Symbol#start_with? and Symbol#end_with? method. [Feature #16348]
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | string.c | 43 | ||||
-rw-r--r-- | test/ruby/test_symbol.rb | 23 |
3 files changed, 70 insertions, 0 deletions
@@ -397,6 +397,10 @@ RubyVM:: String:: + New methods:: + + * Added Symbol#start_with? and Symbol#end_with? method. [Feature #16348] + Unicode:: * Update Unicode version and Emoji version from 11.0.0 to @@ -11076,6 +11076,46 @@ sym_swapcase(int argc, VALUE *argv, VALUE 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); } diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb index c47cadf4bb..75335f180d 100644 --- a/test/ruby/test_symbol.rb +++ b/test/ruby/test_symbol.rb @@ -560,4 +560,27 @@ class TestSymbol < Test::Unit::TestCase puts :a == :a RUBY end + + def test_start_with? + assert_equal(true, :hello.start_with?("hel")) + assert_equal(false, :hello.start_with?("el")) + assert_equal(true, :hello.start_with?("el", "he")) + + bug5536 = '[ruby-core:40623]' + assert_raise(TypeError, bug5536) {:str.start_with? :not_convertible_to_string} + + assert_equal(true, :hello.start_with?(/hel/)) + assert_equal("hel", $&) + assert_equal(false, :hello.start_with?(/el/)) + assert_nil($&) + end + + def test_end_with? + assert_equal(true, :hello.end_with?("llo")) + assert_equal(false, :hello.end_with?("ll")) + assert_equal(true, :hello.end_with?("el", "lo")) + + bug5536 = '[ruby-core:40623]' + assert_raise(TypeError, bug5536) {:str.end_with? :not_convertible_to_string} + end end |