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+ " ruby" ,
11+ " array" ,
12+ " binary-search" ,
13+ " search"
14+ ],
15+ "contributors" : [],
16+ "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 "
17+ },
18+ {
19+ "title" : " Chunk Array" ,
20+ "description" : " Splits an array into chunks of a specified size." ,
21+ "author" : " ACR1209" ,
22+ "tags" : [
23+ " ruby" ,
24+ " array" ,
25+ " chunk" ,
26+ " utility"
27+ ],
28+ "contributors" : [],
29+ "code" : " def chunk_array(array, size)\n array.each_slice(size).to_a\n end\n\n # Example 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 "
30+ },
31+ {
32+ "title" : " Matrix Transpose" ,
33+ "description" : " Transposes a 2D matrix." ,
34+ "author" : " ACR1209" ,
35+ "tags" : [
36+ " ruby" ,
37+ " array" ,
38+ " matrix" ,
39+ " transpose"
40+ ],
41+ "contributors" : [],
42+ "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 "
43+ }
44+ ]
45+ },
46+ {
47+ "categoryName" : " Basics" ,
48+ "snippets" : [
49+ {
50+ "title" : " Hello, World!" ,
51+ "description" : " Prints Hello, World! to the terminal." ,
52+ "author" : " ACR1209" ,
53+ "tags" : [
54+ " ruby" ,
55+ " printing" ,
56+ " hello-world" ,
57+ " utility"
58+ ],
59+ "contributors" : [],
60+ "code" : " puts 'Hello, World!'\n "
61+ }
62+ ]
63+ },
64+ {
65+ "categoryName" : " Data Structures" ,
66+ "snippets" : [
67+ {
68+ "title" : " Binary Tree" ,
69+ "description" : " Implements a basic binary tree with in-order traversal." ,
70+ "author" : " ACR1209" ,
71+ "tags" : [
72+ " ruby" ,
73+ " data structures" ,
74+ " binary tree"
75+ ],
76+ "contributors" : [],
77+ "code" : " class TreeNode\n attr_accessor :data, :left, :right\n\n def initialize(data)\n @data = data\n @left = nil\n @right = nil\n end\n end\n\n class BinaryTree\n attr_accessor :root\n\n def initialize(root_data)\n @root = TreeNode.new(root_data)\n end\n\n def in_order_traversal(node = @root, result = [])\n return result unless node\n\n in_order_traversal(node.left, result)\n result << node.data\n in_order_traversal(node.right, result)\n end\n end\n\n # Usage:\n tree = BinaryTree.new(10)\n tree.root.left = TreeNode.new(5)\n tree.root.right = TreeNode.new(15)\n tree.root.left.left = TreeNode.new(3)\n\n print tree.in_order_traversal # Output: [3, 5, 10, 15]\n "
78+ },
79+ {
80+ "title" : " Doubly Linked List" ,
81+ "description" : " Implements a doubly linked list with node insertion and traversal." ,
82+ "author" : " ACR1209" ,
83+ "tags" : [
84+ " ruby" ,
85+ " data structures" ,
86+ " linked list" ,
87+ " doubly linked list"
88+ ],
89+ "contributors" : [],
90+ "code": "class Node\n attr_accessor :data, :next, :prev\n\n def initialize(data)\n @data = data\n @next = nil\n @prev = nil\n end\nend\n\nclass DoublyLinkedList\n def initialize\n @head = nil\n @tail = nil\n end\n\n def append(data)\n new_node = Node.new(data)\n if @head.nil?\n @head = new_node\n @tail = new_node\n else\n @tail.next = new_node\n new_node.prev = @tail\n @tail = new_node\n end\n end\n\n def prepend(data)\n new_node = Node.new(data)\n if @head.nil?\n @head = new_node\n @tail = new_node\n else\n new_node.next = @head\n @head.prev = new_node\n @head = new_node\n end\n end\n\n def display_forward\n current = @head\n while current\n print \"#{current.data} <-> \"\n current = current.next\n end\n puts \"nil\"\n end\n\n def display_backward\n current = @tail\n while current\n print \"#{current.data} <-> \"\n current = current.prev\n end\n puts \"nil\"\n end\nend\n\n# Usage:\ndll = DoublyLinkedList.new\ndll.append(1)\ndll.append(2)\ndll.append(3)\ndll.display_forward # Output: 1 <-> 2 <-> 3 <-> nil\ndll.display_backward # Output: 3 <-> 2 <-> 1 <-> nil\n"
91+ },
92+ {
93+ "title" : " Singly Linked List" ,
94+ "description" : " Implements a basic singly linked list with node insertion and traversal." ,
95+ "author" : " ACR1209" ,
96+ "tags" : [
97+ " ruby" ,
98+ " data structures" ,
99+ " linked list"
100+ ],
101+ "contributors" : [],
102+ "code" : " class Node\n attr_accessor :data, :next\n\n def initialize(data)\n @data = data\n @next = nil\n end\n end\n\n class LinkedList\n def initialize\n @head = nil\n end\n\n def append(data)\n new_node = Node.new(data)\n if @head.nil?\n @head = new_node\n else\n current = @head\n current = current.next while current.next\n current.next = new_node\n end\n end\n\n def display\n current = @head\n while current\n print \" #{current.data} -> \"\n current = current.next\n end\n puts \" nil\"\n end\n end\n\n # Usage:\n list = LinkedList.new\n list.append(1)\n list.append(2)\n list.append(3)\n list.display # Output: 1 -> 2 -> 3 -> nil\n "
103+ }
104+ ]
105+ },
106+ {
107+ "categoryName" : " Error Handling" ,
108+ "snippets" : [
109+ {
110+ "title" : " Custom Error Class" ,
111+ "description" : " Defines and raises a custom error class in Ruby." ,
112+ "author" : " ACR1209" ,
113+ "tags" : [
114+ " ruby" ,
115+ " error handling" ,
116+ " custom error"
117+ ],
118+ "contributors" : [],
119+ "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 "
120+ }
121+ ]
122+ },
123+ {
124+ "categoryName" : " Math And Numbers" ,
125+ "snippets" : [
126+ {
127+ "title" : " Calculate Compound Interest" ,
128+ "description" : " Calculates compound interest for a given principal amount, rate, and time period." ,
129+ "author" : " ACR1209" ,
130+ "tags" : [
131+ " ruby" ,
132+ " math" ,
133+ " compound interest" ,
134+ " finance"
135+ ],
136+ "contributors" : [
137+ " axorax"
138+ ],
139+ "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 "
140+ },
141+ {
142+ "title" : " Calculate Factorial" ,
143+ "description" : " Computes the factorial of a given integer." ,
144+ "author" : " ACR1209" ,
145+ "tags" : [
146+ " ruby" ,
147+ " math" ,
148+ " factorial"
149+ ],
150+ "contributors" : [],
151+ "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 "
152+ },
153+ {
154+ "title" : " Check Prime Number" ,
155+ "description" : " Checks if a number is a prime number." ,
156+ "author" : " ACR1209" ,
157+ "tags" : [
158+ " ruby" ,
159+ " math" ,
160+ " prime" ,
161+ " check"
162+ ],
163+ "contributors" : [
164+ " dostonnabotov"
165+ ],
166+ "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 "
167+ },
168+ {
169+ "title" : " Find all primes up to integer (Sieve of Sundaram)" ,
170+ "description" : " Finds all the prime numbers up to a specific integer." ,
171+ "author" : " ACR1209" ,
172+ "tags" : [
173+ " ruby" ,
174+ " math" ,
175+ " prime numbers"
176+ ],
177+ "contributors" : [],
178+ "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 "
179+ }
180+ ]
181+ },
182+ {
183+ "categoryName" : " String Manipulation" ,
184+ "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\n end\n\n camel_case = \" camelCaseToSnakeCase\"\n puts camel_to_snake(camel_case) # Output: \" camel_case_to_snake_case\"\n "
199+ },
200+ {
201+ "title" : " Capitalize Words" ,
202+ "description" : " Capitalizes the first letter of each word in a string." ,
203+ "author" : " ACR1209" ,
204+ "tags" : [
205+ " ruby" ,
206+ " string" ,
207+ " capitalize" ,
208+ " words"
209+ ],
210+ "contributors" : [],
211+ "code" : " def capitalize_words(str)\n str.split.map(&:capitalize).join(' ')\n end\n\n sentence = \" ruby is awesome\"\n puts capitalize_words(sentence) # Output: \" Ruby Is Awesome\"\n "
212+ },
213+ {
214+ "title" : " Count Word Occurrences in String" ,
215+ "description" : " Counts the occurrences of each word in a given string." ,
216+ "author" : " ACR1209" ,
217+ "tags" : [
218+ " ruby" ,
219+ " string" ,
220+ " occurrences" ,
221+ " word-count"
222+ ],
223+ "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\n end\n\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 "
225+ },
226+ {
227+ "title" : " Remove Punctuation" ,
228+ "description" : " Removes all punctuation from a given string." ,
229+ "author" : " ACR1209" ,
230+ "tags" : [
231+ " ruby" ,
232+ " string" ,
233+ " punctuation" ,
234+ " remove"
235+ ],
236+ "contributors" : [],
237+ "code" : " def remove_punctuation(str)\n str.gsub(/[[:punct:]]/, '')\n end\n\n text = \" Hello, Ruby! How's it going?\"\n puts remove_punctuation(text) # Output: \" Hello Ruby Hows it going\"\n "
238+ },
239+ {
240+ "title" : " Transform from Snake Case to Camel Case" ,
241+ "description" : " Converts a Snake Case string to Camel Case." ,
242+ "author" : " ACR1209" ,
243+ "tags" : [
244+ " ruby" ,
245+ " string" ,
246+ " convert" ,
247+ " snake-case" ,
248+ " camel-case" ,
249+ " utility"
250+ ],
251+ "contributors" : [],
252+ "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 snake_case = \" snake_case_to_camel_case\"\n puts snake_to_camel(snake_case) # Output: \" snakeCaseToCamelCase\"\n "
253+ },
254+ {
255+ "title" : " Truncate Strings" ,
256+ "description" : " Truncates a string to a specified length, optionally adding an ellipsis." ,
257+ "author" : " ACR1209" ,
258+ "tags" : [
259+ " ruby" ,
260+ " string" ,
261+ " truncate" ,
262+ " utility"
263+ ],
264+ "contributors" : [],
265+ "code" : " def truncate_string(max_length, str)\n return str if str.length <= max_length\n str[0, max_length - 3] + '...'\n end\n\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 "
266+ }
267+ ]
268+ }
269+ ]
0 commit comments