Permalink
Browse files

add stringify_items! and stringify_items methods for Array

  • Loading branch information...
1 parent 9c3acd7 commit e8aafe6e9cd7012e429fbc402b112f1430f5fd1c @ZhangHanDong committed Mar 19, 2011
Showing with 25 additions and 0 deletions.
  1. +16 −0 lib/ruby_extendsions/array_extendsions.rb
  2. +9 −0 spec/array_spec.rb
@@ -44,6 +44,22 @@ def is_blank_all?
def random
return self[rand(self.size)]
end
+
+ def stringify_items
+
+ end
+
+ # 把数组元素全部转换为字符串,只是一层。
+ def stringify_items!
+ self.each_with_index do |item, i|
+ item.is_a?(Array) ? item.stringify_items! : self[i] = item.to_s
+ end
+ end
+
+ def stringify_items
+ Marshal.load(Marshal.dump(self)).stringify_items!
+ end
+
# 二分查找返回数组index, 只限于可排序的元素
def binary_search_index(e, l = 0, u = length - 1)
return if l>u
View
@@ -53,4 +53,13 @@
(arr == brr ).should be_false
end
+ it "should be change to string" do
+ arr, brr = [:a, [1,2,:cddd], :d, 1], [:b, [3,4,false], :b,2]
+ arr.stringify_items!
+ arr.should_not be == [:a, [1,2,:cddd], :d, 1]
+ arr.should be == ['a', ['1','2','cddd'], 'd', '1']
+ brr.stringify_items.should be == ["b", ["3", "4", "false"], "b", "2"]
+ brr.should be == [:b, [3,4,false], :b,2]
+ end
+
end

0 comments on commit e8aafe6

Please sign in to comment.