Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/main.rb

Instance Method Summary collapse

Instance Method Details

#append_word(word) ⇒ Object

Returns a new string with word appended to self.



3
4
5
6
7
8
# File 'lib/main.rb', line 3

def append_word(word)
  copy = self
  copy.concat(' ') unless copy.end_with?(' ')
  copy.concat(word)
  return copy
end

#append_word!(word) ⇒ Object

Appends the word to self.



10
11
12
13
# File 'lib/main.rb', line 10

def append_word!(word)
  self.concat(' ') unless self.end_with?(' ')
  self.concat(word)
end

#highest_frequency_charsObject

Returns the most common character or characters.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/main.rb', line 69

def highest_frequency_chars()
  chars = {}
  self.chars().each() do |c|
    if not chars.key?(c)
      chars[c] = 1 
    else
      chars[c] += 1
    end
  end
  greatest_value = -1
  greatest_value_chars = []
  for char in chars.keys()
    if chars[char] >= greatest_value
      greatest_value = chars[char] 
      greatest_value_chars << char
    end
  end
  return greatest_value_chars
end

#longest_wordObject

Returns the longest word in self.



15
16
17
18
19
20
21
22
23
# File 'lib/main.rb', line 15

def longest_word()
  longest = ""
  for word in self.words()
    if word.length() > longest.length()
      longest = word
    end
  end
  return longest
end

#occurrences(match_character) ⇒ Object

Returns the number of times match_character occurs.



35
36
37
38
39
40
41
# File 'lib/main.rb', line 35

def occurrences(match_character)
  number_occurrences = 0
  self.chars().each() do |c|
    number_occurrences += 1 if c == match_character
  end
  return number_occurrences
end

#shortest_wordObject

Returns the shortest word in self.



25
26
27
28
29
30
31
32
33
# File 'lib/main.rb', line 25

def shortest_word()
  shortest = self.words()[0]
  for word in self.words()
    if word.length() < shortest.length()
      shortest = word
    end
  end
  return shortest
end

#swap(index1, index2) ⇒ Object

Swaps the values at index1 and index2 and returns the results in a new string.



43
44
45
46
47
48
49
# File 'lib/main.rb', line 43

def swap(index1, index2)
  return_string = self
  tempvar = return_string[index1]
  return_string[index1] = return_string[index2]
  return_string[index2] = tempvar
  return return_string
end

#swap!(index1, index2) ⇒ Object

Swaps the values at index1 and index2.



51
52
53
54
55
# File 'lib/main.rb', line 51

def swap!(index1, index2)
  tempvar = self[index1]
  self[index1] = self[index2]
  self[index2] = tempvar
end

#word_occurrences(word) ⇒ Object

Returns the number of times the word occurs.



57
58
59
60
61
62
63
# File 'lib/main.rb', line 57

def word_occurrences(word)
  number_occurrences = 0
  self.words().each() do |w|
    number_occurrences += 1 if w == word
  end
  return number_occurrences
end

#wordsObject

Returns all the words in the string.



65
66
67
# File 'lib/main.rb', line 65

def words()
  return self.split(' ')
end