[ create a new paste ] login | about

Link: http://codepad.org/cjIIODi2    [ raw code | output | fork ]

kabhwan - Ruby, pasted on Sep 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class String
  def digit_sum
    match_data_arr = self.scan(/[0-9]/)

    return 0 if !match_data_arr

    sum = 0

    match_data_arr.each do |data|
      sum += data.to_i
    end

    sum
  end
end

print "ab1c2defh34ijklmn".digit_sum


Output:
1
10


Create a new paste based on this one


Comments: