Skip to content

Commit 461ae2d

Browse files
committed
Update consolidated snippets
1 parent 5caa33c commit 461ae2d

File tree

1 file changed

+22
-44
lines changed

1 file changed

+22
-44
lines changed

public/consolidated/ruby.json

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"description": "Searches for an element in a sorted array using binary search.",
88
"author": "ACR1209",
99
"tags": [
10-
"ruby",
1110
"array",
1211
"binary-search",
1312
"search"
@@ -20,10 +19,8 @@
2019
"description": "Splits an array into chunks of a specified size.",
2120
"author": "ACR1209",
2221
"tags": [
23-
"ruby",
2422
"array",
25-
"chunk",
26-
"utility"
23+
"chunk"
2724
],
2825
"contributors": [],
2926
"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,7 +30,6 @@
3330
"description": "Transposes a 2D matrix.",
3431
"author": "ACR1209",
3532
"tags": [
36-
"ruby",
3733
"array",
3834
"matrix",
3935
"transpose"
@@ -51,7 +47,6 @@
5147
"description": "Prints Hello, World! to the terminal.",
5248
"author": "ACR1209",
5349
"tags": [
54-
"ruby",
5550
"printing",
5651
"hello-world",
5752
"utility"
@@ -69,7 +64,6 @@
6964
"description": "Implements a basic binary tree with in-order traversal.",
7065
"author": "ACR1209",
7166
"tags": [
72-
"ruby",
7367
"data structures",
7468
"binary tree"
7569
],
@@ -81,7 +75,6 @@
8175
"description": "Implements a doubly linked list with node insertion and traversal.",
8276
"author": "ACR1209",
8377
"tags": [
84-
"ruby",
8578
"data structures",
8679
"linked list",
8780
"doubly linked list"
@@ -94,7 +87,6 @@
9487
"description": "Implements a basic singly linked list with node insertion and traversal.",
9588
"author": "ACR1209",
9689
"tags": [
97-
"ruby",
9890
"data structures",
9991
"linked list"
10092
],
@@ -111,7 +103,6 @@
111103
"description": "Defines and raises a custom error class in Ruby.",
112104
"author": "ACR1209",
113105
"tags": [
114-
"ruby",
115106
"error handling",
116107
"custom error"
117108
],
@@ -128,7 +119,6 @@
128119
"description": "Calculates compound interest for a given principal amount, rate, and time period.",
129120
"author": "ACR1209",
130121
"tags": [
131-
"ruby",
132122
"math",
133123
"compound interest",
134124
"finance"
@@ -143,7 +133,6 @@
143133
"description": "Computes the factorial of a given integer.",
144134
"author": "ACR1209",
145135
"tags": [
146-
"ruby",
147136
"math",
148137
"factorial"
149138
],
@@ -155,7 +144,6 @@
155144
"description": "Checks if a number is a prime number.",
156145
"author": "ACR1209",
157146
"tags": [
158-
"ruby",
159147
"math",
160148
"prime",
161149
"check"
@@ -170,7 +158,6 @@
170158
"description": "Finds all the prime numbers up to a specific integer.",
171159
"author": "ACR1209",
172160
"tags": [
173-
"ruby",
174161
"math",
175162
"prime numbers"
176163
],
@@ -182,87 +169,78 @@
182169
{
183170
"categoryName": "String Manipulation",
184171
"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-
},
200172
{
201173
"title": "Capitalize Words",
202174
"description": "Capitalizes the first letter of each word in a string.",
203175
"author": "ACR1209",
204176
"tags": [
205-
"ruby",
206177
"string",
207178
"capitalize",
208179
"words"
209180
],
210181
"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"
212183
},
213184
{
214185
"title": "Count Word Occurrences in String",
215186
"description": "Counts the occurrences of each word in a given string.",
216187
"author": "ACR1209",
217188
"tags": [
218-
"ruby",
219189
"string",
220190
"occurrences",
221191
"word-count"
222192
],
223193
"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"
225195
},
226196
{
227197
"title": "Remove Punctuation",
228198
"description": "Removes all punctuation from a given string.",
229199
"author": "ACR1209",
230200
"tags": [
231-
"ruby",
232201
"string",
233202
"punctuation",
234203
"remove"
235204
],
236205
"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"
238220
},
239221
{
240222
"title": "Transform from Snake Case to Camel Case",
241223
"description": "Converts a Snake Case string to Camel Case.",
242224
"author": "ACR1209",
243225
"tags": [
244-
"ruby",
245226
"string",
246227
"convert",
247228
"snake-case",
248-
"camel-case",
249-
"utility"
229+
"camel-case"
250230
],
251231
"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"
253233
},
254234
{
255-
"title": "Truncate Strings",
235+
"title": "Truncate String",
256236
"description": "Truncates a string to a specified length, optionally adding an ellipsis.",
257237
"author": "ACR1209",
258238
"tags": [
259-
"ruby",
260239
"string",
261-
"truncate",
262-
"utility"
240+
"truncate"
263241
],
264242
"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"
266244
}
267245
]
268246
}

0 commit comments

Comments
 (0)