diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-12-12 01:01:29 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-12-12 01:01:29 +0000 |
commit | 81be0398848dba163cf9f04263c8f3753133ab32 (patch) | |
tree | 9d1306498e517952b7b796f1fedc23728f7b0a02 | |
parent | 4d467a08650273f3e20dbc4dc78592a2237362a9 (diff) | |
download | ruby-81be0398848dba163cf9f04263c8f3753133ab32.tar.gz |
* range.c (range_cover): new method Range#cover? added. the
method name might be changed. thanks to takano32 at
http://www.rubyist.net/~matz/20051210.html#c08 for name
suggestion. [ruby-talk:167182]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9674 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | range.c | 33 |
2 files changed, 40 insertions, 0 deletions
@@ -1,3 +1,10 @@ +Mon Dec 12 09:58:09 2005 Yukihiro Matsumoto <matz@ruby-lang.org> + + * range.c (range_cover): new method Range#cover? added. the + method name might be changed. thanks to takano32 at + http://www.rubyist.net/~matz/20051210.html#c08 for name + suggestion. [ruby-talk:167182] + Mon Dec 12 00:33:56 2005 Yukihiro Matsumoto <matz@ruby-lang.org> * ext/digest/digest.c (rb_digest_base_s_digest): add volatile to @@ -641,6 +641,38 @@ range_include(VALUE range, VALUE val) } +/* + * call-seq: + * rng.cover?(val) => true or false + * + * Returns <code>true</code> if <i>obj</i> is between beg and end, + * i.e <code>beg <= obj <= end</code> (or <i>end</i> exclusive when + * <code>exclude_end?</code> is true). + * + * ("a".."z").cover?("c") #=> true + * ("a".."z").cover?("5") #=> false + */ + +static VALUE +range_cover(range, val) + VALUE range, val; +{ + VALUE beg, end; + + beg = rb_ivar_get(range, id_beg); + end = rb_ivar_get(range, id_end); + if (r_le(beg, val)) { + if (EXCL(range)) { + if (r_lt(val, end)) return Qtrue; + } + else { + if (r_le(val, end)) return Qtrue; + } + } + return Qfalse; +} + + /* A <code>Range</code> represents an interval---a set of values with a * start and an end. Ranges may be constructed using the * <em>s</em><code>..</code><em>e</em> and @@ -718,6 +750,7 @@ Init_Range(void) rb_define_method(rb_cRange, "member?", range_include, 1); rb_define_method(rb_cRange, "include?", range_include, 1); + rb_define_method(rb_cRange, "cover?", range_cover, 1); id_cmp = rb_intern("<=>"); id_succ = rb_intern("succ"); |