summaryrefslogtreecommitdiffhomepage
path: root/tasks/ruby_ext.rake
blob: 7422e3fafa4343b44f5df1f2fcbbaf746a3ab247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Object
  class << self
    def attr_block(*syms)
      syms.flatten.each do |sym|
        class_eval "def #{sym}(&block);block.call(@#{sym}) if block_given?;@#{sym};end"
      end
    end
  end
end

class String
  def relative_path_from(dir)
    Pathname.new(File.expand_path(self)).relative_path_from(Pathname.new(File.expand_path(dir))).to_s
  end
  
  # Compatible with 1.9 on 1.8
  def %(params)
    if params.is_a?(Hash)
      str = self.clone
      params.each do |k, v|
        str.gsub!("%{#{k}}", v)
      end
      str
    else
      if params.is_a?(Array)
        sprintf(self, *params)
      else
        sprintf(self, params)
      end
    end
  end
end

class Symbol
  # Compatible with 1.9 on 1.8
  def to_proc
    proc { |obj, *args| obj.send(self, *args) }
  end
end