Class: String

Inherits:
Object show all
Defined in:
lib/innate/roman.rb,
lib/innate/string.rb

Instance Method Summary collapse

Instance Method Details

#prefix_to?(*s) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
# File 'lib/innate/string.rb', line 39

def prefix_to?(*s)
  v = strip
  return false if v.length == 0
  s.flatten.each do |t|
    return t if t[0...v.length].strip == v
  end
  nil
end

#prefix_to_i?(*s) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
# File 'lib/innate/string.rb', line 48

def prefix_to_i?(*s)
  v = strip
  return false if v.length == 0
  s.flatten.each do |t|
    return t if t[0...v.length].upcase.strip == v.upcase
  end
  nil
end

#roman_numeral?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/innate/roman.rb', line 22

def roman_numeral?
  self =~ Regexp.ROMAN ? self : nil
end

#roman_to_iObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/innate/roman.rb', line 34

def roman_to_i
  raise "#{self} is not a valid roman numeral." unless roman_numeral?
  s = upcase
  values = { ?M => 1000, ?D => 500, ?C => 100, ?L => 50, ?X => 10, ?V => 5, ?I => 1 }
  value = result = 0
  upcase.each_byte do |b|
    result -= value *2 if values[b] > value
    result += (value = values[b])
  end
  result
end

#split_with_separator(pattern) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/innate/string.rb', line 2

def split_with_separator(pattern)
  pos = 0
  result = []
  separator = nil
  t = self
  loop do
    t = t[pos...length]
    m = pattern.match(t)
    unless m
      return result << yield(separator, t) if block_given?
      return result << [separator, t]
    end
    if block_given?
      result << yield(separator, m.pre_match)
    else
      result << [separator, m.pre_match]
    end
    separator = m[0]
    pos = m.offset(0).last
  end
  result
end

#starts_with?(*s) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/innate/string.rb', line 25

def starts_with?(*s)
  s.flatten.each do |t|
    return t if self[0...t.length].strip == t.strip
  end
  nil
end

#starts_with_i?(*s) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/innate/string.rb', line 32

def starts_with_i?(*s)
  s.flatten.each do |t|
    return t if self[0...t.length].upcase.strip == t.upcase.strip
  end
  nil
end

#toggle_romanObject



26
27
28
29
30
31
32
# File 'lib/innate/roman.rb', line 26

def toggle_roman
  if Regexp.ROMAN =~ self
    roman_to_i.to_s
  else
    to_i.roman
  end
end