Restore game actions.
[kaya.git] / lib / utils.rb
blobe245259006d7e56ec7e25781c81ce037502f1626
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 class Object
9   def tap
10     yield self
11     self
12   end
13   
14   def alter(property)
15     value = yield(send(property))
16     send("#{property}=", value)
17   end
19   def metaclass
20     class << self
21       self
22     end
23   end
24   
25   def metaclass_eval(&blk)
26     metaclass.instance_eval(&blk)
27   end
28   
29   def map
30     yield self unless nil?
31   end
32 end
35 module Enumerable
36   def detect_index
37     i = 0
38     each do |item|
39       return i if yield item
40       i += 1
41     end
42     
43     nil
44   end
45 end
47 class Hash
48   def maph
49     { }.tap do |result|
50       each do |key, value|
51         key, value = yield key, value
52         result[key] = value
53       end
54     end
55   end
56 end
58 class String
59   def underscore
60     self.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
61          gsub(/([a-z\d])([A-Z])/,'\1_\2').
62          downcase
63   end
64   
65   def camelize
66     gsub(/_(.)/) {|m| $1.upcase }
67   end
68 end