1
+ [
2
+ {
3
+ "categoryName" : " Array Manipulation" ,
4
+ "snippets" : [
5
+ {
6
+ "title" : " Binary Search" ,
7
+ "description" : " Searches for an element in a sorted array using binary search." ,
8
+ "author" : " ACR1209" ,
9
+ "tags" : [
10
+ " array" ,
11
+ " binary-search" ,
12
+ " search"
13
+ ],
14
+ "contributors" : [],
15
+ "code" : " def binary_search(array, target)\n low = 0\n high = array.length - 1\n\n while low <= high\n mid = (low + high) / 2\n guess = array[mid]\n\n if guess == target\n return mid\n elsif guess > target\n high = mid - 1\n else\n low = mid + 1\n end\n end\n\n return nil\n end\n\n # Usage:\n array = [1, 3, 5, 7, 9]\n target = 5\n result = binary_search(array, target)\n puts result # Output: 2\n "
16
+ },
17
+ {
18
+ "title" : " Chunk Array" ,
19
+ "description" : " Splits an array into chunks of a specified size." ,
20
+ "author" : " ACR1209" ,
21
+ "tags" : [
22
+ " array" ,
23
+ " chunk"
24
+ ],
25
+ "contributors" : [],
26
+ "code" : " def chunk_array(array, size)\n array.each_slice(size).to_a\n end\n\n # Usage:\n arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n chunked_arr = chunk_array(arr, 2)\n puts chunked_arr.inspect # Output: [[1, 2], [3, 4], [5, 6], [7, 8], [9]]\n "
27
+ },
28
+ {
29
+ "title" : " Matrix Transpose" ,
30
+ "description" : " Transposes a 2D matrix." ,
31
+ "author" : " ACR1209" ,
32
+ "tags" : [
33
+ " array" ,
34
+ " matrix" ,
35
+ " transpose"
36
+ ],
37
+ "contributors" : [],
38
+ "code" : " def transpose_matrix(matrix)\n return [] if matrix.empty?\n return [] if matrix.first.empty?\n\n matrix.first.zip(*matrix[1..-1])\n end\n\n # Usage:\n matrix = [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]\n ]\n print transpose_matrix(matrix) # Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]\n "
39
+ }
40
+ ]
41
+ },
42
+ {
43
+ "categoryName" : " Basics" ,
44
+ "snippets" : [
45
+ {
46
+ "title" : " Hello, World!" ,
47
+ "description" : " Prints Hello, World! to the terminal." ,
48
+ "author" : " ACR1209" ,
49
+ "tags" : [
50
+ " printing" ,
51
+ " hello-world" ,
52
+ " utility"
53
+ ],
54
+ "contributors" : [],
55
+ "code" : " puts 'Hello, World!'\n "
56
+ }
57
+ ]
58
+ },
59
+ {
60
+ "categoryName" : " Error Handling" ,
61
+ "snippets" : [
62
+ {
63
+ "title" : " Custom Error Class" ,
64
+ "description" : " Defines and raises a custom error class in Ruby." ,
65
+ "author" : " ACR1209" ,
66
+ "tags" : [
67
+ " error handling" ,
68
+ " custom error"
69
+ ],
70
+ "contributors" : [],
71
+ "code" : " class MyCustomError < StandardError; end\n\n def risky_method(value)\n raise MyCustomError, \" Value must be positive\" if value <= 0\n \" Valid value: #{value}\"\n end\n\n # Usage:\n begin\n puts risky_method(-1)\n rescue MyCustomError => e\n puts e.message # Output: \" Value must be positive\"\n end\n "
72
+ }
73
+ ]
74
+ },
75
+ {
76
+ "categoryName" : " Math And Numbers" ,
77
+ "snippets" : [
78
+ {
79
+ "title" : " Calculate Compound Interest" ,
80
+ "description" : " Calculates compound interest for a given principal amount, rate, and time period." ,
81
+ "author" : " ACR1209" ,
82
+ "tags" : [
83
+ " math" ,
84
+ " compound interest" ,
85
+ " finance"
86
+ ],
87
+ "contributors" : [
88
+ " axorax"
89
+ ],
90
+ "code" : " def compound_interest(principal, rate, time, n = 1)\n principal * (1 + rate / n) ** (n * time)\n end\n\n # Usage:\n puts compound_interest(1000, 0.05, 5) # Output: 1276.2815625000003\n puts compound_interest(1000, 0.05, 5, 12) # Output: 1283.3586785035118\n "
91
+ },
92
+ {
93
+ "title" : " Calculate Factorial" ,
94
+ "description" : " Computes the factorial of a given integer." ,
95
+ "author" : " ACR1209" ,
96
+ "tags" : [
97
+ " math" ,
98
+ " factorial"
99
+ ],
100
+ "contributors" : [],
101
+ "code" : " def factorial(n)\n return 1 if n <= 1\n (2..n).reduce(1, :*)\n end\n\n # Usage:\n puts factorial(5) # Output: 120\n "
102
+ },
103
+ {
104
+ "title" : " Check Prime Number" ,
105
+ "description" : " Checks if a number is a prime number." ,
106
+ "author" : " ACR1209" ,
107
+ "tags" : [
108
+ " math" ,
109
+ " prime" ,
110
+ " check"
111
+ ],
112
+ "contributors" : [
113
+ " dostonnabotov"
114
+ ],
115
+ "code" : " def is_prime?(n)\n return false if n <= 1\n (2..Math.sqrt(n)).each do |i|\n return false if n % i == 0\n end\n true\n end\n\n # Usage:\n puts is_prime?(29) # Output: true\n puts is_prime?(30) # Output: false\n "
116
+ },
117
+ {
118
+ "title" : " Find all primes up to integer (Sieve of Sundaram)" ,
119
+ "description" : " Finds all the prime numbers up to a specific integer." ,
120
+ "author" : " ACR1209" ,
121
+ "tags" : [
122
+ " math" ,
123
+ " prime numbers"
124
+ ],
125
+ "contributors" : [],
126
+ "code" : " def sieve_of_sundaram(limit)\n n = (limit - 1) / 2\n marked = Array.new(n + 1, false)\n\n (1..n).each do |i|\n j = i\n while (i + j + 2 * i * j) <= n\n marked[i + j + 2 * i * j] = true\n j += 1\n end\n end\n\n primes = [2]\n (1..n).each do |i|\n primes << (2 * i + 1) unless marked[i]\n end\n\n primes\n end\n\n # Usage:\n print sieve_of_sundaram(30) # Output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]\n "
127
+ }
128
+ ]
129
+ },
130
+ {
131
+ "categoryName" : " String Manipulation" ,
132
+ "snippets" : [
133
+ {
134
+ "title" : " Capitalize Words" ,
135
+ "description" : " Capitalizes the first letter of each word in a string." ,
136
+ "author" : " ACR1209" ,
137
+ "tags" : [
138
+ " string" ,
139
+ " capitalize" ,
140
+ " words"
141
+ ],
142
+ "contributors" : [],
143
+ "code" : " def capitalize_words(str)\n str.split.map(&:capitalize).join(' ')\n end\n\n # Usage:\n sentence = \" ruby is awesome\"\n puts capitalize_words(sentence) # Output: \" Ruby Is Awesome\"\n "
144
+ },
145
+ {
146
+ "title" : " Count Word Occurrences in String" ,
147
+ "description" : " Counts the occurrences of each word in a given string." ,
148
+ "author" : " ACR1209" ,
149
+ "tags" : [
150
+ " string" ,
151
+ " occurrences" ,
152
+ " word-count"
153
+ ],
154
+ "contributors" : [],
155
+ "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\n end\n\n # Usage:\n text = \" ruby is awesome and Ruby is fun\"\n puts count_word_occurrences(text) # Output: {\" ruby\" =>2, \" is\" =>2, \" awesome\" =>1, \" and\" =>1, \" fun\" =>1}\n "
156
+ },
157
+ {
158
+ "title" : " Remove Punctuation" ,
159
+ "description" : " Removes all punctuation from a given string." ,
160
+ "author" : " ACR1209" ,
161
+ "tags" : [
162
+ " string" ,
163
+ " punctuation" ,
164
+ " remove"
165
+ ],
166
+ "contributors" : [],
167
+ "code" : " def remove_punctuation(str)\n str.gsub(/[[:punct:]]/, '')\n end\n\n # Usage:\n text = \" Hello, Ruby! How's it going?\"\n puts remove_punctuation(text) # Output: \" Hello Ruby Hows it going\"\n "
168
+ },
169
+ {
170
+ "title" : " Transform Camel Case to Snake Case" ,
171
+ "description" : " Converts a Camel or Pascal Case string to Snake case." ,
172
+ "author" : " ACR1209" ,
173
+ "tags" : [
174
+ " string" ,
175
+ " convert" ,
176
+ " camel-case" ,
177
+ " snake-case" ,
178
+ " pascal-case"
179
+ ],
180
+ "contributors" : [],
181
+ "code" : " def camel_to_snake(str)\n str.gsub(/([A-Z])/, '_\\ 1').sub(/^_/, '').downcase\n end\n\n # Usage:\n camel_case = \" camelCaseToSnakeCase\"\n pascal_case = \" PascalCaseToSnakeCase\"\n puts camel_to_snake(camel_case) # Output: \" camel_case_to_snake_case\"\n puts camel_to_snake(pascal_case) # Output: \" pascal_case_to_snake_case\"\n "
182
+ },
183
+ {
184
+ "title" : " Transform from Snake Case to Camel Case" ,
185
+ "description" : " Converts a Snake Case string to Camel Case." ,
186
+ "author" : " ACR1209" ,
187
+ "tags" : [
188
+ " string" ,
189
+ " convert" ,
190
+ " snake-case" ,
191
+ " camel-case"
192
+ ],
193
+ "contributors" : [],
194
+ "code" : " def snake_to_camel(str)\n str.split('_').map.with_index { |word, index| \n index == 0 ? word : word.capitalize \n }.join\n end\n\n # Usage:\n snake_case = \" snake_case_to_camel_case\"\n puts snake_to_camel(snake_case) # Output: \" snakeCaseToCamelCase\"\n "
195
+ },
196
+ {
197
+ "title" : " Transform from Snake Case to Pascal Case" ,
198
+ "description" : " Converts a Snake Case string to Pascal Case." ,
199
+ "author" : " ACR1209" ,
200
+ "tags" : [
201
+ " string" ,
202
+ " convert" ,
203
+ " snake-case" ,
204
+ " pascal-case"
205
+ ],
206
+ "contributors" : [],
207
+ "code" : " def snake_to_pascal(str)\n str.split('_').map.with_index { |word, index| \n word.capitalize \n }.join\n end\n\n # Usage:\n snake_case = \" snake_case_to_pascal_case\"\n puts snake_to_pascal(snake_case) # Output: \" SnakeCaseToPascalCase\"\n "
208
+ },
209
+ {
210
+ "title" : " Truncate String" ,
211
+ "description" : " Truncates a string to a specified length, optionally adding an ellipsis." ,
212
+ "author" : " ACR1209" ,
213
+ "tags" : [
214
+ " string" ,
215
+ " truncate"
216
+ ],
217
+ "contributors" : [],
218
+ "code" : " def truncate_string(str, max_length)\n return str if str.length <= max_length || max_length <= 3\n str[0, max_length - 3] + '...'\n end\n\n # Usage:\n long_string = \" Ruby is a dynamic, open source programming language.\"\n puts truncate_string(20, long_string) # Output: \" Ruby is a dynamic...\"\n puts truncate_string(54, long_string) # Output: \" Ruby is a dynamic, open source programming language.\"\n "
219
+ }
220
+ ]
221
+ }
222
+ ]
0 commit comments