diff options
| author | Robert Mosolgo <[email protected]> | 2014-11-24 09:30:45 -0800 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-11-26 01:03:38 +0900 |
| commit | 0133d9ac70fd8d3ebe873e29c12cabfd3ff3f1a1 (patch) | |
| tree | 8f293f7de5f4d6ed3151952469bf85626c1aad81 | |
| parent | 28bd33297ad42c2eed1cca45345166a072b3bbf2 (diff) | |
| download | mruby-0133d9ac70fd8d3ebe873e29c12cabfd3ff3f1a1.tar.gz mruby-0133d9ac70fd8d3ebe873e29c12cabfd3ff3f1a1.zip | |
fix(String) String#[] accepts float; close #2650 #2651
| -rw-r--r-- | src/string.c | 4 | ||||
| -rw-r--r-- | test/t/string.rb | 2 |
2 files changed, 6 insertions, 0 deletions
diff --git a/src/string.c b/src/string.c index 5eaed533a..20f371aa2 100644 --- a/src/string.c +++ b/src/string.c @@ -15,6 +15,7 @@ #include "mruby/class.h" #include "mruby/range.h" #include "mruby/string.h" +#include "mruby/numeric.h" #include "mruby/re.h" const char mrb_digitmap[] = "0123456789abcdefghijklmnopqrstuvwxyz"; @@ -750,6 +751,8 @@ mrb_str_aref(mrb_state *mrb, mrb_value str, mrb_value indx) mrb_regexp_check(mrb, indx); switch (mrb_type(indx)) { + case MRB_TT_FLOAT: + indx = mrb_flo_to_fixnum(mrb, indx); case MRB_TT_FIXNUM: idx = mrb_fixnum(indx); @@ -813,6 +816,7 @@ num_index: * * a = "hello there" * a[1] #=> 101(1.8.7) "e"(1.9.2) + * a[1.1] #=> "e"(1.9.2) * a[1,3] #=> "ell" * a[1..3] #=> "ell" * a[-3,2] #=> "er" diff --git a/test/t/string.rb b/test/t/string.rb index c0e545e87..a56862fe2 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -44,6 +44,7 @@ assert('String#[]', '15.2.10.5.6') do b = 'abc'[-1] c = 'abc'[10] d = 'abc'[-10] + e = 'abc'[1.1] # length of args is 2 a1 = 'abc'[0, -1] @@ -63,6 +64,7 @@ assert('String#[]', '15.2.10.5.6') do assert_equal 'c', b assert_nil c assert_nil d + assert_equal 'b', e assert_nil a1 assert_nil b1 assert_nil c1 |
