Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/pcr-ruby.rb

Overview

Add some useful String methods

Instance Method Summary collapse

Instance Method Details

#compareSemester(s) ⇒ Object

Method to compare semesters. Returns true if self is later, false if self is before, 0 if same s should be a string like “2009A”



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pcr-ruby.rb', line 55

def compareSemester(s)
	year = self[0..3]
	season = self[4]
	compYear = s[0..3]
	compSeason = s[4]
	
	if year.to_i > compYear.to_i #Later year
		return true
	elsif year.to_i < compYear.to_i #Earlier year
		return false
	elsif year.to_i == compYear.to_i #Same year, so test season
		if season > compSeason #Season is later
			return true
		elsif season = compSeason #Exact same time
			return 0
		elsif season < compSeason #compSeason is later
			return false
		end
	end
end

#isValidCourseCode?Boolean

Checks if String is valid Penn course code format

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
# File 'lib/pcr-ruby.rb', line 11

def isValidCourseCode?
	test = self.split('-')
	if test[0].length == 4 and test[1].length == 3
		true
	else
		false
	end
end

#smart_capitalizeObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/pcr-ruby.rb', line 38

def smart_capitalize
	# ignore any leading crazy characters and capitalize the first real character
	if self =~ /^['"\(\[']*([a-z])/
	  i = index($1)
	  x = self[i,self.length]
	  # word with capitals and periods mid-word are left alone
	  self[i,1] = self[i,1].upcase unless x =~ /[A-Z]/ or x =~ /\.\w+/
	end
	self
end

#smart_capitalize!Object



49
50
51
# File 'lib/pcr-ruby.rb', line 49

def smart_capitalize!
	replace(smart_capitalize)
end

#titlecaseObject

Methods to convert strings to titlecase. Thanks github.com/samsouder/titlecase



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pcr-ruby.rb', line 22

def titlecase
	small_words = %w(a an and as at but by en for if in of on or the to v v. via vs vs.)

	x = split(" ").map do |word|
	  # note: word could contain non-word characters!
	  # downcase all small_words, capitalize the rest
	  small_words.include?(word.gsub(/\W/, "").downcase) ? word.downcase! : word.smart_capitalize!
	  word
	end
	# capitalize first and last words
	x.first.smart_capitalize!
	x.last.smart_capitalize!
	# small words after colons are capitalized
	x.join(" ").gsub(/:\s?(\W*#{small_words.join("|")}\W*)\s/) { ": #{$1.smart_capitalize} " }
end