Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/main.rb
Instance Method Summary collapse
-
#append_word(word) ⇒ Object
Returns a new string with word appended to self.
-
#append_word!(word) ⇒ Object
Appends the word to self.
-
#longest_word ⇒ Object
Returns the longest word in self.
-
#occurrences(match_character) ⇒ Object
Returns the number of times match_character occurs.
-
#shortest_word ⇒ Object
Returns the shortest word in self.
-
#swap(index1, index2) ⇒ Object
Swaps the values at index1 and index2 and returns the results in a new string.
-
#swap!(index1, index2) ⇒ Object
Swaps the values at index1 and index2.
-
#word_occurrences(word) ⇒ Object
Returns the number of times the word occurs.
-
#words ⇒ Object
Returns all the words in the string.
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 |
#longest_word ⇒ Object
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_word ⇒ Object
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 |
#words ⇒ Object
Returns all the words in the string.
65 66 67 |
# File 'lib/main.rb', line 65 def words() return self.split(' ') end |