From cbe73e93f7623e4f93ad0085a616cb7764043ef0 Mon Sep 17 00:00:00 2001 From: Jun Hiroe Date: Mon, 2 Jun 2014 22:00:05 +0900 Subject: Add comments in String#lstrip, rstrip, strip, lstring_bang, rstrip_bang and strip_bang --- mrbgems/mruby-string-ext/mrblib/string.rb | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'mrbgems') diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index 45c631b94..18a7a62d3 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -1,4 +1,14 @@ class String + ## + # call-seq: + # str.lstrip -> new_str + # + # Returns a copy of str with leading whitespace removed. See also + # String#rstrip and String#strip. + # + # " hello ".lstrip #=> "hello " + # "hello".lstrip #=> "hello" + # def lstrip a = 0 z = self.size - 1 @@ -6,6 +16,16 @@ class String (z >= 0) ? self[a..z] : "" end + ## + # call-seq: + # str.rstrip -> new_str + # + # Returns a copy of str with trailing whitespace removed. See also + # String#lstrip and String#strip. + # + # " hello ".rstrip #=> " hello" + # "hello".rstrip #=> "hello" + # def rstrip a = 0 z = self.size - 1 @@ -13,6 +33,15 @@ class String (z >= 0) ? self[a..z] : "" end + ## + # call-seq: + # str.strip -> new_str + # + # Returns a copy of str with leading and trailing whitespace removed. + # + # " hello ".strip #=> "hello" + # "\tgoodbye\r\n".strip #=> "goodbye" + # def strip a = 0 z = self.size - 1 @@ -21,16 +50,45 @@ class String (z >= 0) ? self[a..z] : "" end + ## + # call-seq: + # str.lstrip! -> self or nil + # + # Removes leading whitespace from str, returning nil if no + # change was made. See also String#rstrip! and + # String#strip!. + # + # " hello ".lstrip #=> "hello " + # "hello".lstrip! #=> nil + # def lstrip! s = self.lstrip (s == self) ? nil : self.replace(s) end + ## + # call-seq: + # str.rstrip! -> self or nil + # + # Removes trailing whitespace from str, returning nil if + # no change was made. See also String#lstrip! and + # String#strip!. + # + # " hello ".rstrip #=> " hello" + # "hello".rstrip! #=> nil + # def rstrip! s = self.rstrip (s == self) ? nil : self.replace(s) end + ## + # call-seq: + # str.strip! -> str or nil + # + # Removes leading and trailing whitespace from str. Returns + # nil if str was not altered. + # def strip! s = self.strip (s == self) ? nil : self.replace(s) -- cgit v1.2.3 From 85ed19ca4da194d48f27bb06d1eacccf9f877d58 Mon Sep 17 00:00:00 2001 From: Jun Hiroe Date: Mon, 2 Jun 2014 22:03:29 +0900 Subject: Fix indent in String#casecmp comments --- mrbgems/mruby-string-ext/mrblib/string.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'mrbgems') diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index 45c631b94..3066ea698 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -36,16 +36,17 @@ class String (s == self) ? nil : self.replace(s) end -# call-seq: -# str.casecmp(other_str) -> -1, 0, +1 or nil -# -# Case-insensitive version of String#<=>. -# -# "abcdef".casecmp("abcde") #=> 1 -# "aBcDeF".casecmp("abcdef") #=> 0 -# "abcdef".casecmp("abcdefg") #=> -1 -# "abcdef".casecmp("ABCDEF") #=> 0 -# + ## + # call-seq: + # str.casecmp(other_str) -> -1, 0, +1 or nil + # + # Case-insensitive version of String#<=>. + # + # "abcdef".casecmp("abcde") #=> 1 + # "aBcDeF".casecmp("abcdef") #=> 0 + # "abcdef".casecmp("abcdefg") #=> -1 + # "abcdef".casecmp("ABCDEF") #=> 0 + # def casecmp(str) self.downcase <=> str.to_str.downcase rescue NoMethodError -- cgit v1.2.3 From a0b12bf6b4ed5e148d33562142d8e89271669953 Mon Sep 17 00:00:00 2001 From: Mitsutaka Mimura Date: Mon, 2 Jun 2014 23:06:02 +0900 Subject: Add Array#to_h --- mrbgems/mruby-array-ext/src/array.c | 44 +++++++++++++++++++++++++++++++++++ mrbgems/mruby-array-ext/test/array.rb | 8 +++++++ 2 files changed, 52 insertions(+) (limited to 'mrbgems') diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c index 014137e99..d69f0ac44 100644 --- a/mrbgems/mruby-array-ext/src/array.c +++ b/mrbgems/mruby-array-ext/src/array.c @@ -2,6 +2,7 @@ #include "mruby/value.h" #include "mruby/array.h" #include "mruby/range.h" +#include "mruby/hash.h" /* * call-seq: @@ -105,6 +106,48 @@ mrb_ary_values_at(mrb_state *mrb, mrb_value self) return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, mrb_ary_ref); } +/* + * call-seq: + * ary.to_h -> Hash + * + * Returns the result of interpreting aray as an array of + * [key, value] paris. + * + * [[:foo, :bar], [1, 2]].to_h + * # => {:foo => :bar, 1 => 2} + */ + +static mrb_value +mrb_ary_to_h(mrb_state *mrb, mrb_value ary) +{ + mrb_int i; + mrb_value v, hash; + + hash = mrb_hash_new_capa(mrb, 0); + + for (i = 0; i < RARRAY_LEN(ary); ++i) { + v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]); + + if (mrb_nil_p(v)) { + mrb_raisef(mrb, E_TYPE_ERROR, "wrong element type %S at %S (expected array)", + mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, RARRAY_PTR(ary)[i])), + mrb_fixnum_value(i) + ); + } + + if (RARRAY_LEN(v) != 2) { + mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong array length at %S (expected 2, was %S)", + mrb_fixnum_value(i), + mrb_fixnum_value(RARRAY_LEN(v)) + ); + } + + mrb_hash_set(mrb, hash, RARRAY_PTR(v)[0], RARRAY_PTR(v)[1]); + } + + return hash; +} + void mrb_mruby_array_ext_gem_init(mrb_state* mrb) { @@ -114,6 +157,7 @@ mrb_mruby_array_ext_gem_init(mrb_state* mrb) mrb_define_method(mrb, a, "at", mrb_ary_at, MRB_ARGS_REQ(1)); mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1)); mrb_define_method(mrb, a, "values_at", mrb_ary_values_at, MRB_ARGS_ANY()); + mrb_define_method(mrb, a, "to_h", mrb_ary_to_h, MRB_ARGS_REQ(0)); } void diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index cb5652dde..8c919f7e0 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -285,3 +285,11 @@ assert('Array#values_at') do assert_equal ['none', nil, nil, 'red', 'green', 'purple'], a.values_at(4..6, 0...3) assert_raise(TypeError) { a.values_at 'tt' } end + +assert('Array#to_h') do + assert_equal({}, [].to_h) + assert_equal({a: 1, b:2}, [[:a, 1], [:b, 2]].to_h) + + assert_raise(TypeError) { [1].to_h } + assert_raise(ArgumentError) { [[1]].to_h } +end -- cgit v1.2.3