|
7 | 7 | "description": "Searches for an element in a sorted array using binary search.", |
8 | 8 | "author": "ACR1209", |
9 | 9 | "tags": [ |
10 | | - "ruby", |
11 | 10 | "array", |
12 | 11 | "binary-search", |
13 | 12 | "search" |
|
20 | 19 | "description": "Splits an array into chunks of a specified size.", |
21 | 20 | "author": "ACR1209", |
22 | 21 | "tags": [ |
23 | | - "ruby", |
24 | 22 | "array", |
25 | | - "chunk", |
26 | | - "utility" |
| 23 | + "chunk" |
27 | 24 | ], |
28 | 25 | "contributors": [], |
29 | 26 | "code": "def chunk_array(array, size)\n array.each_slice(size).to_a\nend\n\n# Example usage:\narr = [1, 2, 3, 4, 5, 6, 7, 8, 9]\nchunked_arr = chunk_array(arr, 2)\nputs chunked_arr.inspect # Output: [[1, 2], [3, 4], [5, 6], [7, 8], [9]]\n" |
|
33 | 30 | "description": "Transposes a 2D matrix.", |
34 | 31 | "author": "ACR1209", |
35 | 32 | "tags": [ |
36 | | - "ruby", |
37 | 33 | "array", |
38 | 34 | "matrix", |
39 | 35 | "transpose" |
|
51 | 47 | "description": "Prints Hello, World! to the terminal.", |
52 | 48 | "author": "ACR1209", |
53 | 49 | "tags": [ |
54 | | - "ruby", |
55 | 50 | "printing", |
56 | 51 | "hello-world", |
57 | 52 | "utility" |
|
69 | 64 | "description": "Implements a basic binary tree with in-order traversal.", |
70 | 65 | "author": "ACR1209", |
71 | 66 | "tags": [ |
72 | | - "ruby", |
73 | 67 | "data structures", |
74 | 68 | "binary tree" |
75 | 69 | ], |
|
81 | 75 | "description": "Implements a doubly linked list with node insertion and traversal.", |
82 | 76 | "author": "ACR1209", |
83 | 77 | "tags": [ |
84 | | - "ruby", |
85 | 78 | "data structures", |
86 | 79 | "linked list", |
87 | 80 | "doubly linked list" |
|
94 | 87 | "description": "Implements a basic singly linked list with node insertion and traversal.", |
95 | 88 | "author": "ACR1209", |
96 | 89 | "tags": [ |
97 | | - "ruby", |
98 | 90 | "data structures", |
99 | 91 | "linked list" |
100 | 92 | ], |
|
111 | 103 | "description": "Defines and raises a custom error class in Ruby.", |
112 | 104 | "author": "ACR1209", |
113 | 105 | "tags": [ |
114 | | - "ruby", |
115 | 106 | "error handling", |
116 | 107 | "custom error" |
117 | 108 | ], |
|
128 | 119 | "description": "Calculates compound interest for a given principal amount, rate, and time period.", |
129 | 120 | "author": "ACR1209", |
130 | 121 | "tags": [ |
131 | | - "ruby", |
132 | 122 | "math", |
133 | 123 | "compound interest", |
134 | 124 | "finance" |
|
143 | 133 | "description": "Computes the factorial of a given integer.", |
144 | 134 | "author": "ACR1209", |
145 | 135 | "tags": [ |
146 | | - "ruby", |
147 | 136 | "math", |
148 | 137 | "factorial" |
149 | 138 | ], |
|
155 | 144 | "description": "Checks if a number is a prime number.", |
156 | 145 | "author": "ACR1209", |
157 | 146 | "tags": [ |
158 | | - "ruby", |
159 | 147 | "math", |
160 | 148 | "prime", |
161 | 149 | "check" |
|
170 | 158 | "description": "Finds all the prime numbers up to a specific integer.", |
171 | 159 | "author": "ACR1209", |
172 | 160 | "tags": [ |
173 | | - "ruby", |
174 | 161 | "math", |
175 | 162 | "prime numbers" |
176 | 163 | ], |
|
182 | 169 | { |
183 | 170 | "categoryName": "String Manipulation", |
184 | 171 | "snippets": [ |
185 | | - { |
186 | | - "title": "Transform Camel Case to Snake Case", |
187 | | - "description": "Converts a Camel Case string to Snake case.", |
188 | | - "author": "ACR1209", |
189 | | - "tags": [ |
190 | | - "ruby", |
191 | | - "string", |
192 | | - "convert", |
193 | | - "camel-case", |
194 | | - "snake-case", |
195 | | - "utility" |
196 | | - ], |
197 | | - "contributors": [], |
198 | | - "code": "def camel_to_snake(str)\n str.gsub(/([A-Z])/, '_\\1').downcase\nend\n\ncamel_case = \"camelCaseToSnakeCase\"\nputs camel_to_snake(camel_case) # Output: \"camel_case_to_snake_case\"\n" |
199 | | - }, |
200 | 172 | { |
201 | 173 | "title": "Capitalize Words", |
202 | 174 | "description": "Capitalizes the first letter of each word in a string.", |
203 | 175 | "author": "ACR1209", |
204 | 176 | "tags": [ |
205 | | - "ruby", |
206 | 177 | "string", |
207 | 178 | "capitalize", |
208 | 179 | "words" |
209 | 180 | ], |
210 | 181 | "contributors": [], |
211 | | - "code": "def capitalize_words(str)\n str.split.map(&:capitalize).join(' ')\nend\n\nsentence = \"ruby is awesome\"\nputs capitalize_words(sentence) # Output: \"Ruby Is Awesome\"\n" |
| 182 | + "code": "def capitalize_words(str)\n str.split.map(&:capitalize).join(' ')\nend\n\n# Usage\nsentence = \"ruby is awesome\"\nputs capitalize_words(sentence) # Output: \"Ruby Is Awesome\"\n" |
212 | 183 | }, |
213 | 184 | { |
214 | 185 | "title": "Count Word Occurrences in String", |
215 | 186 | "description": "Counts the occurrences of each word in a given string.", |
216 | 187 | "author": "ACR1209", |
217 | 188 | "tags": [ |
218 | | - "ruby", |
219 | 189 | "string", |
220 | 190 | "occurrences", |
221 | 191 | "word-count" |
222 | 192 | ], |
223 | 193 | "contributors": [], |
224 | | - "code": "def count_word_occurrences(text)\n words = text.downcase.scan(/\\w+/)\n occurrences = Hash.new(0)\n words.each { |word| occurrences[word] += 1 }\n occurrences\nend\n\ntext = \"ruby is awesome and Ruby is fun\"\nputs count_word_occurrences(text) # Output: {\"ruby\"=>2, \"is\"=>2, \"awesome\"=>1, \"and\"=>1, \"fun\"=>1}\n" |
| 194 | + "code": "def count_word_occurrences(text)\n words = text.downcase.scan(/\\w+/)\n occurrences = Hash.new(0)\n words.each { |word| occurrences[word] += 1 }\n occurrences\nend\n\n# Usage\ntext = \"ruby is awesome and Ruby is fun\"\nputs count_word_occurrences(text) # Output: {\"ruby\"=>2, \"is\"=>2, \"awesome\"=>1, \"and\"=>1, \"fun\"=>1}\n" |
225 | 195 | }, |
226 | 196 | { |
227 | 197 | "title": "Remove Punctuation", |
228 | 198 | "description": "Removes all punctuation from a given string.", |
229 | 199 | "author": "ACR1209", |
230 | 200 | "tags": [ |
231 | | - "ruby", |
232 | 201 | "string", |
233 | 202 | "punctuation", |
234 | 203 | "remove" |
235 | 204 | ], |
236 | 205 | "contributors": [], |
237 | | - "code": "def remove_punctuation(str)\n str.gsub(/[[:punct:]]/, '')\nend\n\ntext = \"Hello, Ruby! How's it going?\"\nputs remove_punctuation(text) # Output: \"Hello Ruby Hows it going\"\n" |
| 206 | + "code": "def remove_punctuation(str)\n str.gsub(/[[:punct:]]/, '')\nend\n\n# Usage\ntext = \"Hello, Ruby! How's it going?\"\nputs remove_punctuation(text) # Output: \"Hello Ruby Hows it going\"\n" |
| 207 | + }, |
| 208 | + { |
| 209 | + "title": "Transform Camel Case to Snake Case", |
| 210 | + "description": "Converts a Camel Case string to Snake case.", |
| 211 | + "author": "ACR1209", |
| 212 | + "tags": [ |
| 213 | + "string", |
| 214 | + "convert", |
| 215 | + "camel-case", |
| 216 | + "snake-case" |
| 217 | + ], |
| 218 | + "contributors": [], |
| 219 | + "code": "def camel_to_snake(str)\n str.gsub(/([A-Z])/, '_\\1').downcase\nend\n\n# Usage\ncamel_case = \"camelCaseToSnakeCase\"\nputs camel_to_snake(camel_case) # Output: \"camel_case_to_snake_case\"\n" |
238 | 220 | }, |
239 | 221 | { |
240 | 222 | "title": "Transform from Snake Case to Camel Case", |
241 | 223 | "description": "Converts a Snake Case string to Camel Case.", |
242 | 224 | "author": "ACR1209", |
243 | 225 | "tags": [ |
244 | | - "ruby", |
245 | 226 | "string", |
246 | 227 | "convert", |
247 | 228 | "snake-case", |
248 | | - "camel-case", |
249 | | - "utility" |
| 229 | + "camel-case" |
250 | 230 | ], |
251 | 231 | "contributors": [], |
252 | | - "code": "def snake_to_camel(str)\n str.split('_').map.with_index { |word, index| \n index == 0 ? word : word.capitalize \n }.join\nend\n\nsnake_case = \"snake_case_to_camel_case\"\nputs snake_to_camel(snake_case) # Output: \"snakeCaseToCamelCase\"\n" |
| 232 | + "code": "def snake_to_camel(str)\n str.split('_').map.with_index { |word, index| \n index == 0 ? word : word.capitalize \n }.join\nend\n\n# Usage\nsnake_case = \"snake_case_to_camel_case\"\nputs snake_to_camel(snake_case) # Output: \"snakeCaseToCamelCase\"\n" |
253 | 233 | }, |
254 | 234 | { |
255 | | - "title": "Truncate Strings", |
| 235 | + "title": "Truncate String", |
256 | 236 | "description": "Truncates a string to a specified length, optionally adding an ellipsis.", |
257 | 237 | "author": "ACR1209", |
258 | 238 | "tags": [ |
259 | | - "ruby", |
260 | 239 | "string", |
261 | | - "truncate", |
262 | | - "utility" |
| 240 | + "truncate" |
263 | 241 | ], |
264 | 242 | "contributors": [], |
265 | | - "code": "def truncate_string(max_length, str)\n return str if str.length <= max_length\n str[0, max_length - 3] + '...'\nend\n\nlong_string = \"Ruby is a dynamic, open source programming language.\"\nputs truncate_string(20, long_string) # Output: \"Ruby is a dynamic...\"\nputs truncate_string(54, long_string) # Output: \"Ruby is a dynamic, open source programming language.\"\n" |
| 243 | + "code": "def truncate_string(max_length, str)\n return str if str.length <= max_length\n str[0, max_length - 3] + '...'\nend\n\n# Usage\nlong_string = \"Ruby is a dynamic, open source programming language.\"\nputs truncate_string(20, long_string) # Output: \"Ruby is a dynamic...\"\nputs truncate_string(54, long_string) # Output: \"Ruby is a dynamic, open source programming language.\"\n" |
266 | 244 | } |
267 | 245 | ] |
268 | 246 | } |
|
0 commit comments