[ create a new paste ] login | about

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

Ruby, pasted on Jun 15:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if RUBY_VERSION < '1.9.2'
  class String
    old_format = instance_method(:%)

    define_method(:%) do |arg|
      if arg.is_a?(Hash)
        self.gsub(/%\{(.*?)\}/) { arg[$1.to_sym] }
      else
        old_format.bind(self).call(arg)
      end
    end
  end
end

p RUBY_VERSION
p "%05d" % 123 
p "%-5s: %08x" % [ "ID", 123 ]
template = "%{name} is the best %{occupation} in %{city}."
vals = {:name => "Joe Smith", :occupation => "birthday clown", :city => "Las Vegas"}
p template % vals


Output:
1
2
3
4
"1.8.6"
"00123"
"ID   : 0000007b"
"Joe Smith is the best birthday clown in Las Vegas."


Create a new paste based on this one


Comments: