-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
46 lines (28 loc) · 1019 Bytes
/
README
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
40
41
42
43
44
45
46
StringFormatter is a method for defining custom strf-style methods for your object.
* string_formatter require Ruby 1.9 or higher *
Example:
class PersonFormatter < StringFormatter
f { |p| p.first_name }
F { |p| p.first_name.upcase }
l { |p| p.last_name }
punctuation
pipe { |p| 'PIPE' }
end
class UpcaseFormatter < StringFormatter
f { |p| p.first_name.upcase }
l { |p| p.last_name.upcase }
end
class Person
attr_accessor :first_name, :last_name
define_format_string :strf, :with => PersonFormatter
define_format_string :strfup, :with => UpcaseFormatter
def initialize(*names)
@first_name, @last_name = names
end
end
p = Person.new("Bob", "Smith")
p.strf('%l, %f %|')
# => "Smith, Bob PIPE"
p.strfup('%l, %f')
# => "SMITH, BOB"
You can define definitions for lower case characters, upper case characters, and punctuation marks. The method for lower and upper case characters is the character itself, and the punctuation marks have specific names for each.