From 85ff2d86394656b5f6401635d9a1fe7ac39c4a78 Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:04:01 -0500 Subject: [PATCH 01/19] Create Ruby programming language --- snippets/ruby/basics/hello-world.md | 10 ++ snippets/ruby/icon.svg | 139 ++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 snippets/ruby/basics/hello-world.md create mode 100644 snippets/ruby/icon.svg diff --git a/snippets/ruby/basics/hello-world.md b/snippets/ruby/basics/hello-world.md new file mode 100644 index 00000000..353e89c8 --- /dev/null +++ b/snippets/ruby/basics/hello-world.md @@ -0,0 +1,10 @@ +--- +title: Hello, World! +description: Prints Hello, World! to the terminal. +author: ACR1209 +tags: ruby,printing,hello-world,utility +--- + +```rb +puts 'Hello, World!' +``` diff --git a/snippets/ruby/icon.svg b/snippets/ruby/icon.svg new file mode 100644 index 00000000..10ec5836 --- /dev/null +++ b/snippets/ruby/icon.svg @@ -0,0 +1,139 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From f1291ff3436b27a31554b790bea756c8dd534141 Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:13:10 -0500 Subject: [PATCH 02/19] Added ruby array manipulation category and some snippets --- .../ruby/array-manipulation/binary-search.md | 34 +++++++++++++++++++ .../ruby/array-manipulation/chunk-array.md | 17 ++++++++++ .../array-manipulation/matrix-transpose.md | 23 +++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 snippets/ruby/array-manipulation/binary-search.md create mode 100644 snippets/ruby/array-manipulation/chunk-array.md create mode 100644 snippets/ruby/array-manipulation/matrix-transpose.md diff --git a/snippets/ruby/array-manipulation/binary-search.md b/snippets/ruby/array-manipulation/binary-search.md new file mode 100644 index 00000000..ee7d16d1 --- /dev/null +++ b/snippets/ruby/array-manipulation/binary-search.md @@ -0,0 +1,34 @@ +--- +title: Binary Search +description: Searches for an element in a sorted array using binary search. +author: ACR1209 +tags: ruby,array,binary-search,search +--- + +```rb +def binary_search(array, target) + low = 0 + high = array.length - 1 + + while low <= high + mid = (low + high) / 2 + guess = array[mid] + + if guess == target + return mid + elsif guess > target + high = mid - 1 + else + low = mid + 1 + end + end + + return nil +end + +# Usage +array = [1, 3, 5, 7, 9] +target = 5 +result = binary_search(array, target) +puts result # Output: 2 +``` \ No newline at end of file diff --git a/snippets/ruby/array-manipulation/chunk-array.md b/snippets/ruby/array-manipulation/chunk-array.md new file mode 100644 index 00000000..32536ab3 --- /dev/null +++ b/snippets/ruby/array-manipulation/chunk-array.md @@ -0,0 +1,17 @@ +--- +title: Chunk Array +description: Splits an array into chunks of a specified size. +author: ACR1209 +tags: ruby,array,chunk,utility +--- + +```rb +def chunk_array(array, size) + array.each_slice(size).to_a +end + +# Example usage: +arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] +chunked_arr = chunk_array(arr, 2) +puts chunked_arr.inspect # Output: [[1, 2], [3, 4], [5, 6], [7, 8], [9]] +``` \ No newline at end of file diff --git a/snippets/ruby/array-manipulation/matrix-transpose.md b/snippets/ruby/array-manipulation/matrix-transpose.md new file mode 100644 index 00000000..bb033c9b --- /dev/null +++ b/snippets/ruby/array-manipulation/matrix-transpose.md @@ -0,0 +1,23 @@ +--- +title: Matrix Transpose +description: Transposes a 2D matrix. +author: ACR1209 +tags: ruby,array,matrix,transpose +--- + +```ruby +def transpose_matrix(matrix) + return [] if matrix.empty? + return [] if matrix.first.empty? + + matrix.first.zip(*matrix[1..-1]) +end + +# Usage +matrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9] +] +print transpose_matrix(matrix) # Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]] +``` \ No newline at end of file From 92c408cb845d3b209085afc406249cf4b26e912c Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:21:20 -0500 Subject: [PATCH 03/19] Add Ruby string manipulation snippets --- .../camelcase-to-snakecase.md | 15 +++++++++++++++ .../string-manipulation/capitalize-words.md | 15 +++++++++++++++ .../count-word-ocurrences.md | 18 ++++++++++++++++++ .../string-manipulation/remove-punctuation.md | 15 +++++++++++++++ .../snakecase-to-camelcase.md | 17 +++++++++++++++++ .../string-manipulation/truncate-string.md | 17 +++++++++++++++++ 6 files changed, 97 insertions(+) create mode 100644 snippets/ruby/string-manipulation/camelcase-to-snakecase.md create mode 100644 snippets/ruby/string-manipulation/capitalize-words.md create mode 100644 snippets/ruby/string-manipulation/count-word-ocurrences.md create mode 100644 snippets/ruby/string-manipulation/remove-punctuation.md create mode 100644 snippets/ruby/string-manipulation/snakecase-to-camelcase.md create mode 100644 snippets/ruby/string-manipulation/truncate-string.md diff --git a/snippets/ruby/string-manipulation/camelcase-to-snakecase.md b/snippets/ruby/string-manipulation/camelcase-to-snakecase.md new file mode 100644 index 00000000..675ac7a4 --- /dev/null +++ b/snippets/ruby/string-manipulation/camelcase-to-snakecase.md @@ -0,0 +1,15 @@ +--- +title: Transform Camel Case to Snake Case +description: Converts a Camel Case string to Snake case. +author: ACR1209 +tags: ruby,string,convert,camel-case,snake-case,utility +--- + +```rb +def camel_to_snake(str) + str.gsub(/([A-Z])/, '_\1').downcase +end + +camel_case = "camelCaseToSnakeCase" +puts camel_to_snake(camel_case) # Output: "camel_case_to_snake_case" +``` \ No newline at end of file diff --git a/snippets/ruby/string-manipulation/capitalize-words.md b/snippets/ruby/string-manipulation/capitalize-words.md new file mode 100644 index 00000000..da5beace --- /dev/null +++ b/snippets/ruby/string-manipulation/capitalize-words.md @@ -0,0 +1,15 @@ +--- +title: Capitalize Words +description: Capitalizes the first letter of each word in a string. +author: ACR1209 +tags: ruby,string,capitalize,words +--- + +```rb +def capitalize_words(str) + str.split.map(&:capitalize).join(' ') +end + +sentence = "ruby is awesome" +puts capitalize_words(sentence) # Output: "Ruby Is Awesome" +``` diff --git a/snippets/ruby/string-manipulation/count-word-ocurrences.md b/snippets/ruby/string-manipulation/count-word-ocurrences.md new file mode 100644 index 00000000..42961093 --- /dev/null +++ b/snippets/ruby/string-manipulation/count-word-ocurrences.md @@ -0,0 +1,18 @@ +--- +title: Count Word Occurrences in String +description: Counts the occurrences of each word in a given string. +author: ACR1209 +tags: ruby,string,occurrences,word-count +--- + +```rb +def count_word_occurrences(text) + words = text.downcase.scan(/\w+/) + occurrences = Hash.new(0) + words.each { |word| occurrences[word] += 1 } + occurrences +end + +text = "ruby is awesome and Ruby is fun" +puts count_word_occurrences(text) # Output: {"ruby"=>2, "is"=>2, "awesome"=>1, "and"=>1, "fun"=>1} +``` \ No newline at end of file diff --git a/snippets/ruby/string-manipulation/remove-punctuation.md b/snippets/ruby/string-manipulation/remove-punctuation.md new file mode 100644 index 00000000..e8e44b9d --- /dev/null +++ b/snippets/ruby/string-manipulation/remove-punctuation.md @@ -0,0 +1,15 @@ +--- +title: Remove Punctuation +description: Removes all punctuation from a given string. +author: ACR1209 +tags: ruby,string,punctuation,remove +--- + +```rb +def remove_punctuation(str) + str.gsub(/[[:punct:]]/, '') +end + +text = "Hello, Ruby! How's it going?" +puts remove_punctuation(text) # Output: "Hello Ruby Hows it going" +``` diff --git a/snippets/ruby/string-manipulation/snakecase-to-camelcase.md b/snippets/ruby/string-manipulation/snakecase-to-camelcase.md new file mode 100644 index 00000000..b36f7fe0 --- /dev/null +++ b/snippets/ruby/string-manipulation/snakecase-to-camelcase.md @@ -0,0 +1,17 @@ +--- +title: Transform from Snake Case to Camel Case +description: Converts a Snake Case string to Camel Case. +author: ACR1209 +tags: ruby,string,convert,snake-case,camel-case,utility +--- + +```rb +def snake_to_camel(str) + str.split('_').map.with_index { |word, index| + index == 0 ? word : word.capitalize + }.join +end + +snake_case = "snake_case_to_camel_case" +puts snake_to_camel(snake_case) # Output: "snakeCaseToCamelCase" +``` diff --git a/snippets/ruby/string-manipulation/truncate-string.md b/snippets/ruby/string-manipulation/truncate-string.md new file mode 100644 index 00000000..60ad5a22 --- /dev/null +++ b/snippets/ruby/string-manipulation/truncate-string.md @@ -0,0 +1,17 @@ +--- +title: Truncate Strings +description: Truncates a string to a specified length, optionally adding an ellipsis. +author: ACR1209 +tags: ruby,string,truncate,utility +--- + +```rb +def truncate_string(max_length, str) + return str if str.length <= max_length + str[0, max_length - 3] + '...' +end + +long_string = "Ruby is a dynamic, open source programming language." +puts truncate_string(20, long_string) # Output: "Ruby is a dynamic..." +puts truncate_string(54, long_string) # Output: "Ruby is a dynamic, open source programming language." +``` From 993a50a8241963e5f18d011007b6617995ebdf7a Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:30:42 -0500 Subject: [PATCH 04/19] Add Ruby snippet for calculating compound interest --- .../calculate-compound-interest.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 snippets/ruby/math-and-numbers/calculate-compound-interest.md diff --git a/snippets/ruby/math-and-numbers/calculate-compound-interest.md b/snippets/ruby/math-and-numbers/calculate-compound-interest.md new file mode 100644 index 00000000..594c1307 --- /dev/null +++ b/snippets/ruby/math-and-numbers/calculate-compound-interest.md @@ -0,0 +1,17 @@ +--- +title: Calculate Compound Interest +description: Calculates compound interest for a given principal amount, rate, and time period. +author: ACR1209 +contributors: axorax +tags: ruby,math,compound interest,finance +--- + +```ruby +def compound_interest(principal, rate, time, n = 1) + principal * (1 + rate / n) ** (n * time) +end + +# Usage: +puts compound_interest(1000, 0.05, 5) # Output: 1276.2815625000003 +puts compound_interest(1000, 0.05, 5, 12) # Output: 1283.3586785035118 +``` From 43f49a1510d34995fdaaa82374d507e9e9eba173 Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:34:43 -0500 Subject: [PATCH 05/19] Add Ruby Sieve of Sundaram snippet --- .../calculate-compound-interest.md | 2 +- .../math-and-numbers/sieve-of-sundaram.md | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 snippets/ruby/math-and-numbers/sieve-of-sundaram.md diff --git a/snippets/ruby/math-and-numbers/calculate-compound-interest.md b/snippets/ruby/math-and-numbers/calculate-compound-interest.md index 594c1307..ecdae54a 100644 --- a/snippets/ruby/math-and-numbers/calculate-compound-interest.md +++ b/snippets/ruby/math-and-numbers/calculate-compound-interest.md @@ -6,7 +6,7 @@ contributors: axorax tags: ruby,math,compound interest,finance --- -```ruby +```rb def compound_interest(principal, rate, time, n = 1) principal * (1 + rate / n) ** (n * time) end diff --git a/snippets/ruby/math-and-numbers/sieve-of-sundaram.md b/snippets/ruby/math-and-numbers/sieve-of-sundaram.md new file mode 100644 index 00000000..30de1c27 --- /dev/null +++ b/snippets/ruby/math-and-numbers/sieve-of-sundaram.md @@ -0,0 +1,31 @@ +--- +title: Find all primes up to integer (Sieve of Sundaram) +description: Finds all the prime numbers up to a specific integer. +author: ACR1209 +tags: ruby,math,prime numbers +--- + +```rb +def sieve_of_sundaram(limit) + n = (limit - 1) / 2 + marked = Array.new(n + 1, false) + + (1..n).each do |i| + j = i + while (i + j + 2 * i * j) <= n + marked[i + j + 2 * i * j] = true + j += 1 + end + end + + primes = [2] + (1..n).each do |i| + primes << (2 * i + 1) unless marked[i] + end + + primes +end + +# Usage: +print sieve_of_sundaram(30) # Output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] +``` \ No newline at end of file From 3807b5c780f3effe249deb35fc714dc05e8f51d5 Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:39:58 -0500 Subject: [PATCH 06/19] Add Ruby snippet for checking prime numbers --- .../math-and-numbers/check-prime-number.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/ruby/math-and-numbers/check-prime-number.md diff --git a/snippets/ruby/math-and-numbers/check-prime-number.md b/snippets/ruby/math-and-numbers/check-prime-number.md new file mode 100644 index 00000000..8919266d --- /dev/null +++ b/snippets/ruby/math-and-numbers/check-prime-number.md @@ -0,0 +1,21 @@ +--- +title: Check Prime Number +description: Checks if a number is a prime number. +author: ACR1209 +contributors: dostonnabotov +tags: ruby,math,prime,check +--- + +```rb +def is_prime?(n) + return false if n <= 1 + (2..Math.sqrt(n)).each do |i| + return false if n % i == 0 + end + true +end + +# Usage: +puts is_prime?(29) # Output: true +puts is_prime?(30) # Output: false +``` From ec3ee2b2fa76b3915de9af06ca440211a11e1718 Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:40:24 -0500 Subject: [PATCH 07/19] Add Ruby snippet for calculating factorial --- .../ruby/math-and-numbers/calculate-factorial.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 snippets/ruby/math-and-numbers/calculate-factorial.md diff --git a/snippets/ruby/math-and-numbers/calculate-factorial.md b/snippets/ruby/math-and-numbers/calculate-factorial.md new file mode 100644 index 00000000..adc50b47 --- /dev/null +++ b/snippets/ruby/math-and-numbers/calculate-factorial.md @@ -0,0 +1,16 @@ +--- +title: Calculate Factorial +description: Computes the factorial of a given integer. +author: ACR1209 +tags: ruby,math,factorial +--- + +```rb +def factorial(n) + return 1 if n <= 1 + (2..n).reduce(1, :*) +end + +# Usage: +puts factorial(5) # Output: 120 +``` \ No newline at end of file From cdbd348674b84911669988d701e761f0a3a7717a Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:44:25 -0500 Subject: [PATCH 08/19] Add Ruby snippet for defining and raising a custom error class --- snippets/ruby/error-handling/custom-error.md | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/ruby/error-handling/custom-error.md diff --git a/snippets/ruby/error-handling/custom-error.md b/snippets/ruby/error-handling/custom-error.md new file mode 100644 index 00000000..3b98ad51 --- /dev/null +++ b/snippets/ruby/error-handling/custom-error.md @@ -0,0 +1,22 @@ +--- +title: Custom Error Class +description: Defines and raises a custom error class in Ruby. +author: ACR1209 +tags: ruby,error handling,custom error +--- + +```rb +class MyCustomError < StandardError; end + +def risky_method(value) + raise MyCustomError, "Value must be positive" if value <= 0 + "Valid value: #{value}" +end + +# Usage: +begin + puts risky_method(-1) +rescue MyCustomError => e + puts e.message # Output: "Value must be positive" +end +``` \ No newline at end of file From 88fcb81d1a47dee6590e0f38f750bd263bbc2f6d Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:50:01 -0500 Subject: [PATCH 09/19] Add Ruby snippet for implementing a basic binary tree with in-order traversal --- snippets/ruby/data-structures/binary-tree.md | 42 ++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 snippets/ruby/data-structures/binary-tree.md diff --git a/snippets/ruby/data-structures/binary-tree.md b/snippets/ruby/data-structures/binary-tree.md new file mode 100644 index 00000000..266eca5e --- /dev/null +++ b/snippets/ruby/data-structures/binary-tree.md @@ -0,0 +1,42 @@ +--- +title: Binary Tree +description: Implements a basic binary tree with in-order traversal. +author: ACR1209 +tags: ruby,data structures,binary tree +--- + +```rb +class TreeNode + attr_accessor :data, :left, :right + + def initialize(data) + @data = data + @left = nil + @right = nil + end +end + +class BinaryTree + attr_accessor :root + + def initialize(root_data) + @root = TreeNode.new(root_data) + end + + def in_order_traversal(node = @root, result = []) + return result unless node + + in_order_traversal(node.left, result) + result << node.data + in_order_traversal(node.right, result) + end +end + +# Usage: +tree = BinaryTree.new(10) +tree.root.left = TreeNode.new(5) +tree.root.right = TreeNode.new(15) +tree.root.left.left = TreeNode.new(3) + +print tree.in_order_traversal # Output: [3, 5, 10, 15] +``` \ No newline at end of file From d6364c6c56bce46ba7857ed0037b826c19d1acef Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:54:55 -0500 Subject: [PATCH 10/19] Add Ruby snippet for implementing a singly linked list with node insertion and traversal --- snippets/ruby/data-structures/linked-list.md | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 snippets/ruby/data-structures/linked-list.md diff --git a/snippets/ruby/data-structures/linked-list.md b/snippets/ruby/data-structures/linked-list.md new file mode 100644 index 00000000..1a27d60b --- /dev/null +++ b/snippets/ruby/data-structures/linked-list.md @@ -0,0 +1,50 @@ +--- +title: Singly Linked List +description: Implements a basic singly linked list with node insertion and traversal. +author: ACR1209 +tags: ruby,data structures,linked list +--- + +```rb +class Node + attr_accessor :data, :next + + def initialize(data) + @data = data + @next = nil + end +end + +class LinkedList + def initialize + @head = nil + end + + def append(data) + new_node = Node.new(data) + if @head.nil? + @head = new_node + else + current = @head + current = current.next while current.next + current.next = new_node + end + end + + def display + current = @head + while current + print "#{current.data} -> " + current = current.next + end + puts "nil" + end +end + +# Usage: +list = LinkedList.new +list.append(1) +list.append(2) +list.append(3) +list.display # Output: 1 -> 2 -> 3 -> nil +``` \ No newline at end of file From 9f15bd9bb01cb3cc4c60a3cb7eb5598c80cf20d5 Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 13:59:25 -0500 Subject: [PATCH 11/19] Add Ruby snippet for implementing a doubly linked list with node insertion and traversal --- .../data-structures/doubly-linked-list.md | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 snippets/ruby/data-structures/doubly-linked-list.md diff --git a/snippets/ruby/data-structures/doubly-linked-list.md b/snippets/ruby/data-structures/doubly-linked-list.md new file mode 100644 index 00000000..a0e2f949 --- /dev/null +++ b/snippets/ruby/data-structures/doubly-linked-list.md @@ -0,0 +1,75 @@ +--- +title: Doubly Linked List +description: Implements a doubly linked list with node insertion and traversal. +author: ACR1209 +tags: ruby,data structures,linked list,doubly linked list +--- + +```rb +class Node + attr_accessor :data, :next, :prev + + def initialize(data) + @data = data + @next = nil + @prev = nil + end +end + +class DoublyLinkedList + def initialize + @head = nil + @tail = nil + end + + def append(data) + new_node = Node.new(data) + if @head.nil? + @head = new_node + @tail = new_node + else + @tail.next = new_node + new_node.prev = @tail + @tail = new_node + end + end + + def prepend(data) + new_node = Node.new(data) + if @head.nil? + @head = new_node + @tail = new_node + else + new_node.next = @head + @head.prev = new_node + @head = new_node + end + end + + def display_forward + current = @head + while current + print "#{current.data} <-> " + current = current.next + end + puts "nil" + end + + def display_backward + current = @tail + while current + print "#{current.data} <-> " + current = current.prev + end + puts "nil" + end +end + +# Usage: +dll = DoublyLinkedList.new +dll.append(1) +dll.append(2) +dll.append(3) +dll.display_forward # Output: 1 <-> 2 <-> 3 <-> nil +dll.display_backward # Output: 3 <-> 2 <-> 1 <-> nil +``` \ No newline at end of file From 108ca7b89d234d98f65fc9d4fb15e328c527dc0b Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 2 Jan 2025 19:00:01 +0000 Subject: [PATCH 12/19] Update consolidated snippets --- public/consolidated/_index.json | 4 + public/consolidated/ruby.json | 269 ++++++++++++++++++++++++++++++++ public/icons/ruby.svg | 139 +++++++++++++++++ 3 files changed, 412 insertions(+) create mode 100644 public/consolidated/ruby.json create mode 100644 public/icons/ruby.svg diff --git a/public/consolidated/_index.json b/public/consolidated/_index.json index 2591d382..c5ed3c1a 100644 --- a/public/consolidated/_index.json +++ b/public/consolidated/_index.json @@ -31,6 +31,10 @@ "lang": "PYTHON", "icon": "/icons/python.svg" }, + { + "lang": "RUBY", + "icon": "/icons/ruby.svg" + }, { "lang": "RUST", "icon": "/icons/rust.svg" diff --git a/public/consolidated/ruby.json b/public/consolidated/ruby.json new file mode 100644 index 00000000..0bfdf295 --- /dev/null +++ b/public/consolidated/ruby.json @@ -0,0 +1,269 @@ +[ + { + "categoryName": "Array Manipulation", + "snippets": [ + { + "title": "Binary Search", + "description": "Searches for an element in a sorted array using binary search.", + "author": "ACR1209", + "tags": [ + "ruby", + "array", + "binary-search", + "search" + ], + "contributors": [], + "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\nend\n\n# Usage\narray = [1, 3, 5, 7, 9]\ntarget = 5\nresult = binary_search(array, target)\nputs result # Output: 2\n" + }, + { + "title": "Chunk Array", + "description": "Splits an array into chunks of a specified size.", + "author": "ACR1209", + "tags": [ + "ruby", + "array", + "chunk", + "utility" + ], + "contributors": [], + "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" + }, + { + "title": "Matrix Transpose", + "description": "Transposes a 2D matrix.", + "author": "ACR1209", + "tags": [ + "ruby", + "array", + "matrix", + "transpose" + ], + "contributors": [], + "code": "def transpose_matrix(matrix)\n return [] if matrix.empty?\n return [] if matrix.first.empty?\n\n matrix.first.zip(*matrix[1..-1])\nend\n\n# Usage\nmatrix = [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]\n]\nprint transpose_matrix(matrix) # Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]\n" + } + ] + }, + { + "categoryName": "Basics", + "snippets": [ + { + "title": "Hello, World!", + "description": "Prints Hello, World! to the terminal.", + "author": "ACR1209", + "tags": [ + "ruby", + "printing", + "hello-world", + "utility" + ], + "contributors": [], + "code": "puts 'Hello, World!'\n" + } + ] + }, + { + "categoryName": "Data Structures", + "snippets": [ + { + "title": "Binary Tree", + "description": "Implements a basic binary tree with in-order traversal.", + "author": "ACR1209", + "tags": [ + "ruby", + "data structures", + "binary tree" + ], + "contributors": [], + "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\nend\n\nclass 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\nend\n\n# Usage:\ntree = BinaryTree.new(10)\ntree.root.left = TreeNode.new(5)\ntree.root.right = TreeNode.new(15)\ntree.root.left.left = TreeNode.new(3)\n\nprint tree.in_order_traversal # Output: [3, 5, 10, 15]\n" + }, + { + "title": "Doubly Linked List", + "description": "Implements a doubly linked list with node insertion and traversal.", + "author": "ACR1209", + "tags": [ + "ruby", + "data structures", + "linked list", + "doubly linked list" + ], + "contributors": [], + "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" + }, + { + "title": "Singly Linked List", + "description": "Implements a basic singly linked list with node insertion and traversal.", + "author": "ACR1209", + "tags": [ + "ruby", + "data structures", + "linked list" + ], + "contributors": [], + "code": "class Node\n attr_accessor :data, :next\n\n def initialize(data)\n @data = data\n @next = nil\n end\nend\n\nclass 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\nend\n\n# Usage:\nlist = LinkedList.new\nlist.append(1)\nlist.append(2)\nlist.append(3)\nlist.display # Output: 1 -> 2 -> 3 -> nil\n" + } + ] + }, + { + "categoryName": "Error Handling", + "snippets": [ + { + "title": "Custom Error Class", + "description": "Defines and raises a custom error class in Ruby.", + "author": "ACR1209", + "tags": [ + "ruby", + "error handling", + "custom error" + ], + "contributors": [], + "code": "class MyCustomError < StandardError; end\n\ndef risky_method(value)\n raise MyCustomError, \"Value must be positive\" if value <= 0\n \"Valid value: #{value}\"\nend\n\n# Usage:\nbegin\n puts risky_method(-1)\nrescue MyCustomError => e\n puts e.message # Output: \"Value must be positive\"\nend\n" + } + ] + }, + { + "categoryName": "Math And Numbers", + "snippets": [ + { + "title": "Calculate Compound Interest", + "description": "Calculates compound interest for a given principal amount, rate, and time period.", + "author": "ACR1209", + "tags": [ + "ruby", + "math", + "compound interest", + "finance" + ], + "contributors": [ + "axorax" + ], + "code": "def compound_interest(principal, rate, time, n = 1)\n principal * (1 + rate / n) ** (n * time)\nend\n\n# Usage:\nputs compound_interest(1000, 0.05, 5) # Output: 1276.2815625000003\nputs compound_interest(1000, 0.05, 5, 12) # Output: 1283.3586785035118\n" + }, + { + "title": "Calculate Factorial", + "description": "Computes the factorial of a given integer.", + "author": "ACR1209", + "tags": [ + "ruby", + "math", + "factorial" + ], + "contributors": [], + "code": "def factorial(n)\n return 1 if n <= 1\n (2..n).reduce(1, :*)\nend\n\n# Usage:\nputs factorial(5) # Output: 120\n" + }, + { + "title": "Check Prime Number", + "description": "Checks if a number is a prime number.", + "author": "ACR1209", + "tags": [ + "ruby", + "math", + "prime", + "check" + ], + "contributors": [ + "dostonnabotov" + ], + "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\nend\n\n# Usage:\nputs is_prime?(29) # Output: true\nputs is_prime?(30) # Output: false\n" + }, + { + "title": "Find all primes up to integer (Sieve of Sundaram)", + "description": "Finds all the prime numbers up to a specific integer.", + "author": "ACR1209", + "tags": [ + "ruby", + "math", + "prime numbers" + ], + "contributors": [], + "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\nend\n\n# Usage:\nprint sieve_of_sundaram(30) # Output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]\n" + } + ] + }, + { + "categoryName": "String Manipulation", + "snippets": [ + { + "title": "Transform Camel Case to Snake Case", + "description": "Converts a Camel Case string to Snake case.", + "author": "ACR1209", + "tags": [ + "ruby", + "string", + "convert", + "camel-case", + "snake-case", + "utility" + ], + "contributors": [], + "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" + }, + { + "title": "Capitalize Words", + "description": "Capitalizes the first letter of each word in a string.", + "author": "ACR1209", + "tags": [ + "ruby", + "string", + "capitalize", + "words" + ], + "contributors": [], + "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" + }, + { + "title": "Count Word Occurrences in String", + "description": "Counts the occurrences of each word in a given string.", + "author": "ACR1209", + "tags": [ + "ruby", + "string", + "occurrences", + "word-count" + ], + "contributors": [], + "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" + }, + { + "title": "Remove Punctuation", + "description": "Removes all punctuation from a given string.", + "author": "ACR1209", + "tags": [ + "ruby", + "string", + "punctuation", + "remove" + ], + "contributors": [], + "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" + }, + { + "title": "Transform from Snake Case to Camel Case", + "description": "Converts a Snake Case string to Camel Case.", + "author": "ACR1209", + "tags": [ + "ruby", + "string", + "convert", + "snake-case", + "camel-case", + "utility" + ], + "contributors": [], + "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" + }, + { + "title": "Truncate Strings", + "description": "Truncates a string to a specified length, optionally adding an ellipsis.", + "author": "ACR1209", + "tags": [ + "ruby", + "string", + "truncate", + "utility" + ], + "contributors": [], + "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" + } + ] + } +] \ No newline at end of file diff --git a/public/icons/ruby.svg b/public/icons/ruby.svg new file mode 100644 index 00000000..10ec5836 --- /dev/null +++ b/public/icons/ruby.svg @@ -0,0 +1,139 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 5caa33ccb01ae48798c443563cf57b0f423aa7b2 Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Thu, 2 Jan 2025 15:57:16 -0500 Subject: [PATCH 13/19] Fix to fit new guidelines for snippets --- snippets/ruby/array-manipulation/binary-search.md | 2 +- snippets/ruby/array-manipulation/chunk-array.md | 2 +- snippets/ruby/array-manipulation/matrix-transpose.md | 2 +- snippets/ruby/basics/hello-world.md | 2 +- snippets/ruby/data-structures/binary-tree.md | 2 +- snippets/ruby/data-structures/doubly-linked-list.md | 2 +- .../{linked-list.md => singly-linked-list.md} | 2 +- .../{custom-error.md => custom-error-class.md} | 2 +- .../ruby/math-and-numbers/calculate-compound-interest.md | 2 +- snippets/ruby/math-and-numbers/calculate-factorial.md | 2 +- snippets/ruby/math-and-numbers/check-prime-number.md | 2 +- ...md => find-all-primes-up-to-integer-sieve-of-sundaram.md} | 2 +- snippets/ruby/string-manipulation/capitalize-words.md | 3 ++- ...ord-ocurrences.md => count-word-occurrences-in-string.md} | 3 ++- snippets/ruby/string-manipulation/remove-punctuation.md | 3 ++- ...to-snakecase.md => transform-camel-case-to-snake-case.md} | 3 ++- ...melcase.md => transform-from-snake-case-to-camel-case.md} | 3 ++- snippets/ruby/string-manipulation/truncate-string.md | 5 +++-- 18 files changed, 25 insertions(+), 19 deletions(-) rename snippets/ruby/data-structures/{linked-list.md => singly-linked-list.md} (95%) rename snippets/ruby/error-handling/{custom-error.md => custom-error-class.md} (91%) rename snippets/ruby/math-and-numbers/{sieve-of-sundaram.md => find-all-primes-up-to-integer-sieve-of-sundaram.md} (95%) rename snippets/ruby/string-manipulation/{count-word-ocurrences.md => count-word-occurrences-in-string.md} (91%) rename snippets/ruby/string-manipulation/{camelcase-to-snakecase.md => transform-camel-case-to-snake-case.md} (84%) rename snippets/ruby/string-manipulation/{snakecase-to-camelcase.md => transform-from-snake-case-to-camel-case.md} (87%) diff --git a/snippets/ruby/array-manipulation/binary-search.md b/snippets/ruby/array-manipulation/binary-search.md index ee7d16d1..b181b8af 100644 --- a/snippets/ruby/array-manipulation/binary-search.md +++ b/snippets/ruby/array-manipulation/binary-search.md @@ -2,7 +2,7 @@ title: Binary Search description: Searches for an element in a sorted array using binary search. author: ACR1209 -tags: ruby,array,binary-search,search +tags: array,binary-search,search --- ```rb diff --git a/snippets/ruby/array-manipulation/chunk-array.md b/snippets/ruby/array-manipulation/chunk-array.md index 32536ab3..29bfb50d 100644 --- a/snippets/ruby/array-manipulation/chunk-array.md +++ b/snippets/ruby/array-manipulation/chunk-array.md @@ -2,7 +2,7 @@ title: Chunk Array description: Splits an array into chunks of a specified size. author: ACR1209 -tags: ruby,array,chunk,utility +tags: array,chunk --- ```rb diff --git a/snippets/ruby/array-manipulation/matrix-transpose.md b/snippets/ruby/array-manipulation/matrix-transpose.md index bb033c9b..499cea53 100644 --- a/snippets/ruby/array-manipulation/matrix-transpose.md +++ b/snippets/ruby/array-manipulation/matrix-transpose.md @@ -2,7 +2,7 @@ title: Matrix Transpose description: Transposes a 2D matrix. author: ACR1209 -tags: ruby,array,matrix,transpose +tags: array,matrix,transpose --- ```ruby diff --git a/snippets/ruby/basics/hello-world.md b/snippets/ruby/basics/hello-world.md index 353e89c8..962c50c3 100644 --- a/snippets/ruby/basics/hello-world.md +++ b/snippets/ruby/basics/hello-world.md @@ -2,7 +2,7 @@ title: Hello, World! description: Prints Hello, World! to the terminal. author: ACR1209 -tags: ruby,printing,hello-world,utility +tags: printing,hello-world,utility --- ```rb diff --git a/snippets/ruby/data-structures/binary-tree.md b/snippets/ruby/data-structures/binary-tree.md index 266eca5e..e1d41542 100644 --- a/snippets/ruby/data-structures/binary-tree.md +++ b/snippets/ruby/data-structures/binary-tree.md @@ -2,7 +2,7 @@ title: Binary Tree description: Implements a basic binary tree with in-order traversal. author: ACR1209 -tags: ruby,data structures,binary tree +tags: data structures,binary tree --- ```rb diff --git a/snippets/ruby/data-structures/doubly-linked-list.md b/snippets/ruby/data-structures/doubly-linked-list.md index a0e2f949..6d6ade07 100644 --- a/snippets/ruby/data-structures/doubly-linked-list.md +++ b/snippets/ruby/data-structures/doubly-linked-list.md @@ -2,7 +2,7 @@ title: Doubly Linked List description: Implements a doubly linked list with node insertion and traversal. author: ACR1209 -tags: ruby,data structures,linked list,doubly linked list +tags: data structures,linked list,doubly linked list --- ```rb diff --git a/snippets/ruby/data-structures/linked-list.md b/snippets/ruby/data-structures/singly-linked-list.md similarity index 95% rename from snippets/ruby/data-structures/linked-list.md rename to snippets/ruby/data-structures/singly-linked-list.md index 1a27d60b..f50aadce 100644 --- a/snippets/ruby/data-structures/linked-list.md +++ b/snippets/ruby/data-structures/singly-linked-list.md @@ -2,7 +2,7 @@ title: Singly Linked List description: Implements a basic singly linked list with node insertion and traversal. author: ACR1209 -tags: ruby,data structures,linked list +tags: data structures,linked list --- ```rb diff --git a/snippets/ruby/error-handling/custom-error.md b/snippets/ruby/error-handling/custom-error-class.md similarity index 91% rename from snippets/ruby/error-handling/custom-error.md rename to snippets/ruby/error-handling/custom-error-class.md index 3b98ad51..af9ed6ef 100644 --- a/snippets/ruby/error-handling/custom-error.md +++ b/snippets/ruby/error-handling/custom-error-class.md @@ -2,7 +2,7 @@ title: Custom Error Class description: Defines and raises a custom error class in Ruby. author: ACR1209 -tags: ruby,error handling,custom error +tags: error handling,custom error --- ```rb diff --git a/snippets/ruby/math-and-numbers/calculate-compound-interest.md b/snippets/ruby/math-and-numbers/calculate-compound-interest.md index ecdae54a..c13faf26 100644 --- a/snippets/ruby/math-and-numbers/calculate-compound-interest.md +++ b/snippets/ruby/math-and-numbers/calculate-compound-interest.md @@ -3,7 +3,7 @@ title: Calculate Compound Interest description: Calculates compound interest for a given principal amount, rate, and time period. author: ACR1209 contributors: axorax -tags: ruby,math,compound interest,finance +tags: math,compound interest,finance --- ```rb diff --git a/snippets/ruby/math-and-numbers/calculate-factorial.md b/snippets/ruby/math-and-numbers/calculate-factorial.md index adc50b47..b4110ee6 100644 --- a/snippets/ruby/math-and-numbers/calculate-factorial.md +++ b/snippets/ruby/math-and-numbers/calculate-factorial.md @@ -2,7 +2,7 @@ title: Calculate Factorial description: Computes the factorial of a given integer. author: ACR1209 -tags: ruby,math,factorial +tags: math,factorial --- ```rb diff --git a/snippets/ruby/math-and-numbers/check-prime-number.md b/snippets/ruby/math-and-numbers/check-prime-number.md index 8919266d..e6f60928 100644 --- a/snippets/ruby/math-and-numbers/check-prime-number.md +++ b/snippets/ruby/math-and-numbers/check-prime-number.md @@ -3,7 +3,7 @@ title: Check Prime Number description: Checks if a number is a prime number. author: ACR1209 contributors: dostonnabotov -tags: ruby,math,prime,check +tags: math,prime,check --- ```rb diff --git a/snippets/ruby/math-and-numbers/sieve-of-sundaram.md b/snippets/ruby/math-and-numbers/find-all-primes-up-to-integer-sieve-of-sundaram.md similarity index 95% rename from snippets/ruby/math-and-numbers/sieve-of-sundaram.md rename to snippets/ruby/math-and-numbers/find-all-primes-up-to-integer-sieve-of-sundaram.md index 30de1c27..123f970e 100644 --- a/snippets/ruby/math-and-numbers/sieve-of-sundaram.md +++ b/snippets/ruby/math-and-numbers/find-all-primes-up-to-integer-sieve-of-sundaram.md @@ -2,7 +2,7 @@ title: Find all primes up to integer (Sieve of Sundaram) description: Finds all the prime numbers up to a specific integer. author: ACR1209 -tags: ruby,math,prime numbers +tags: math,prime numbers --- ```rb diff --git a/snippets/ruby/string-manipulation/capitalize-words.md b/snippets/ruby/string-manipulation/capitalize-words.md index da5beace..a3baba1c 100644 --- a/snippets/ruby/string-manipulation/capitalize-words.md +++ b/snippets/ruby/string-manipulation/capitalize-words.md @@ -2,7 +2,7 @@ title: Capitalize Words description: Capitalizes the first letter of each word in a string. author: ACR1209 -tags: ruby,string,capitalize,words +tags: string,capitalize,words --- ```rb @@ -10,6 +10,7 @@ def capitalize_words(str) str.split.map(&:capitalize).join(' ') end +# Usage sentence = "ruby is awesome" puts capitalize_words(sentence) # Output: "Ruby Is Awesome" ``` diff --git a/snippets/ruby/string-manipulation/count-word-ocurrences.md b/snippets/ruby/string-manipulation/count-word-occurrences-in-string.md similarity index 91% rename from snippets/ruby/string-manipulation/count-word-ocurrences.md rename to snippets/ruby/string-manipulation/count-word-occurrences-in-string.md index 42961093..b780b527 100644 --- a/snippets/ruby/string-manipulation/count-word-ocurrences.md +++ b/snippets/ruby/string-manipulation/count-word-occurrences-in-string.md @@ -2,7 +2,7 @@ title: Count Word Occurrences in String description: Counts the occurrences of each word in a given string. author: ACR1209 -tags: ruby,string,occurrences,word-count +tags: string,occurrences,word-count --- ```rb @@ -13,6 +13,7 @@ def count_word_occurrences(text) occurrences end +# Usage text = "ruby is awesome and Ruby is fun" puts count_word_occurrences(text) # Output: {"ruby"=>2, "is"=>2, "awesome"=>1, "and"=>1, "fun"=>1} ``` \ No newline at end of file diff --git a/snippets/ruby/string-manipulation/remove-punctuation.md b/snippets/ruby/string-manipulation/remove-punctuation.md index e8e44b9d..ff1411ec 100644 --- a/snippets/ruby/string-manipulation/remove-punctuation.md +++ b/snippets/ruby/string-manipulation/remove-punctuation.md @@ -2,7 +2,7 @@ title: Remove Punctuation description: Removes all punctuation from a given string. author: ACR1209 -tags: ruby,string,punctuation,remove +tags: string,punctuation,remove --- ```rb @@ -10,6 +10,7 @@ def remove_punctuation(str) str.gsub(/[[:punct:]]/, '') end +# Usage text = "Hello, Ruby! How's it going?" puts remove_punctuation(text) # Output: "Hello Ruby Hows it going" ``` diff --git a/snippets/ruby/string-manipulation/camelcase-to-snakecase.md b/snippets/ruby/string-manipulation/transform-camel-case-to-snake-case.md similarity index 84% rename from snippets/ruby/string-manipulation/camelcase-to-snakecase.md rename to snippets/ruby/string-manipulation/transform-camel-case-to-snake-case.md index 675ac7a4..109e9364 100644 --- a/snippets/ruby/string-manipulation/camelcase-to-snakecase.md +++ b/snippets/ruby/string-manipulation/transform-camel-case-to-snake-case.md @@ -2,7 +2,7 @@ title: Transform Camel Case to Snake Case description: Converts a Camel Case string to Snake case. author: ACR1209 -tags: ruby,string,convert,camel-case,snake-case,utility +tags: string,convert,camel-case,snake-case --- ```rb @@ -10,6 +10,7 @@ def camel_to_snake(str) str.gsub(/([A-Z])/, '_\1').downcase end +# Usage camel_case = "camelCaseToSnakeCase" puts camel_to_snake(camel_case) # Output: "camel_case_to_snake_case" ``` \ No newline at end of file diff --git a/snippets/ruby/string-manipulation/snakecase-to-camelcase.md b/snippets/ruby/string-manipulation/transform-from-snake-case-to-camel-case.md similarity index 87% rename from snippets/ruby/string-manipulation/snakecase-to-camelcase.md rename to snippets/ruby/string-manipulation/transform-from-snake-case-to-camel-case.md index b36f7fe0..8dcdf088 100644 --- a/snippets/ruby/string-manipulation/snakecase-to-camelcase.md +++ b/snippets/ruby/string-manipulation/transform-from-snake-case-to-camel-case.md @@ -2,7 +2,7 @@ title: Transform from Snake Case to Camel Case description: Converts a Snake Case string to Camel Case. author: ACR1209 -tags: ruby,string,convert,snake-case,camel-case,utility +tags: string,convert,snake-case,camel-case --- ```rb @@ -12,6 +12,7 @@ def snake_to_camel(str) }.join end +# Usage snake_case = "snake_case_to_camel_case" puts snake_to_camel(snake_case) # Output: "snakeCaseToCamelCase" ``` diff --git a/snippets/ruby/string-manipulation/truncate-string.md b/snippets/ruby/string-manipulation/truncate-string.md index 60ad5a22..a2abd9bf 100644 --- a/snippets/ruby/string-manipulation/truncate-string.md +++ b/snippets/ruby/string-manipulation/truncate-string.md @@ -1,8 +1,8 @@ --- -title: Truncate Strings +title: Truncate String description: Truncates a string to a specified length, optionally adding an ellipsis. author: ACR1209 -tags: ruby,string,truncate,utility +tags: string,truncate --- ```rb @@ -11,6 +11,7 @@ def truncate_string(max_length, str) str[0, max_length - 3] + '...' end +# Usage long_string = "Ruby is a dynamic, open source programming language." puts truncate_string(20, long_string) # Output: "Ruby is a dynamic..." puts truncate_string(54, long_string) # Output: "Ruby is a dynamic, open source programming language." From 461ae2daff67cb2c7a576a15c415b2541025f1b4 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 2 Jan 2025 20:58:43 +0000 Subject: [PATCH 14/19] Update consolidated snippets --- public/consolidated/ruby.json | 66 ++++++++++++----------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/public/consolidated/ruby.json b/public/consolidated/ruby.json index 0bfdf295..008f1c7e 100644 --- a/public/consolidated/ruby.json +++ b/public/consolidated/ruby.json @@ -7,7 +7,6 @@ "description": "Searches for an element in a sorted array using binary search.", "author": "ACR1209", "tags": [ - "ruby", "array", "binary-search", "search" @@ -20,10 +19,8 @@ "description": "Splits an array into chunks of a specified size.", "author": "ACR1209", "tags": [ - "ruby", "array", - "chunk", - "utility" + "chunk" ], "contributors": [], "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 @@ "description": "Transposes a 2D matrix.", "author": "ACR1209", "tags": [ - "ruby", "array", "matrix", "transpose" @@ -51,7 +47,6 @@ "description": "Prints Hello, World! to the terminal.", "author": "ACR1209", "tags": [ - "ruby", "printing", "hello-world", "utility" @@ -69,7 +64,6 @@ "description": "Implements a basic binary tree with in-order traversal.", "author": "ACR1209", "tags": [ - "ruby", "data structures", "binary tree" ], @@ -81,7 +75,6 @@ "description": "Implements a doubly linked list with node insertion and traversal.", "author": "ACR1209", "tags": [ - "ruby", "data structures", "linked list", "doubly linked list" @@ -94,7 +87,6 @@ "description": "Implements a basic singly linked list with node insertion and traversal.", "author": "ACR1209", "tags": [ - "ruby", "data structures", "linked list" ], @@ -111,7 +103,6 @@ "description": "Defines and raises a custom error class in Ruby.", "author": "ACR1209", "tags": [ - "ruby", "error handling", "custom error" ], @@ -128,7 +119,6 @@ "description": "Calculates compound interest for a given principal amount, rate, and time period.", "author": "ACR1209", "tags": [ - "ruby", "math", "compound interest", "finance" @@ -143,7 +133,6 @@ "description": "Computes the factorial of a given integer.", "author": "ACR1209", "tags": [ - "ruby", "math", "factorial" ], @@ -155,7 +144,6 @@ "description": "Checks if a number is a prime number.", "author": "ACR1209", "tags": [ - "ruby", "math", "prime", "check" @@ -170,7 +158,6 @@ "description": "Finds all the prime numbers up to a specific integer.", "author": "ACR1209", "tags": [ - "ruby", "math", "prime numbers" ], @@ -182,87 +169,78 @@ { "categoryName": "String Manipulation", "snippets": [ - { - "title": "Transform Camel Case to Snake Case", - "description": "Converts a Camel Case string to Snake case.", - "author": "ACR1209", - "tags": [ - "ruby", - "string", - "convert", - "camel-case", - "snake-case", - "utility" - ], - "contributors": [], - "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" - }, { "title": "Capitalize Words", "description": "Capitalizes the first letter of each word in a string.", "author": "ACR1209", "tags": [ - "ruby", "string", "capitalize", "words" ], "contributors": [], - "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" + "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" }, { "title": "Count Word Occurrences in String", "description": "Counts the occurrences of each word in a given string.", "author": "ACR1209", "tags": [ - "ruby", "string", "occurrences", "word-count" ], "contributors": [], - "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" + "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" }, { "title": "Remove Punctuation", "description": "Removes all punctuation from a given string.", "author": "ACR1209", "tags": [ - "ruby", "string", "punctuation", "remove" ], "contributors": [], - "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" + "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" + }, + { + "title": "Transform Camel Case to Snake Case", + "description": "Converts a Camel Case string to Snake case.", + "author": "ACR1209", + "tags": [ + "string", + "convert", + "camel-case", + "snake-case" + ], + "contributors": [], + "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" }, { "title": "Transform from Snake Case to Camel Case", "description": "Converts a Snake Case string to Camel Case.", "author": "ACR1209", "tags": [ - "ruby", "string", "convert", "snake-case", - "camel-case", - "utility" + "camel-case" ], "contributors": [], - "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" + "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" }, { - "title": "Truncate Strings", + "title": "Truncate String", "description": "Truncates a string to a specified length, optionally adding an ellipsis.", "author": "ACR1209", "tags": [ - "ruby", "string", - "truncate", - "utility" + "truncate" ], "contributors": [], - "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" + "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" } ] } From 28703fa5e4c6bbf5fd4d9ed22d43c5714e544c3d Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Fri, 3 Jan 2025 18:51:20 -0500 Subject: [PATCH 15/19] Apply feedback to align with contributing guidelines and add new snippet to transform from sc to pc --- .../ruby/array-manipulation/binary-search.md | 2 +- .../ruby/array-manipulation/chunk-array.md | 2 +- .../array-manipulation/matrix-transpose.md | 2 +- snippets/ruby/data-structures/binary-tree.md | 42 ----------- .../data-structures/doubly-linked-list.md | 75 ------------------- .../data-structures/singly-linked-list.md | 50 ------------- .../string-manipulation/capitalize-words.md | 2 +- .../count-word-occurrences-in-string.md | 2 +- .../string-manipulation/remove-punctuation.md | 2 +- .../transform-camel-case-to-snake-case.md | 10 ++- ...transform-from-snake-case-to-camel-case.md | 2 +- ...ransform-from-snake-case-to-pascal-case.md | 18 +++++ .../string-manipulation/truncate-string.md | 6 +- 13 files changed, 34 insertions(+), 181 deletions(-) delete mode 100644 snippets/ruby/data-structures/binary-tree.md delete mode 100644 snippets/ruby/data-structures/doubly-linked-list.md delete mode 100644 snippets/ruby/data-structures/singly-linked-list.md create mode 100644 snippets/ruby/string-manipulation/transform-from-snake-case-to-pascal-case.md diff --git a/snippets/ruby/array-manipulation/binary-search.md b/snippets/ruby/array-manipulation/binary-search.md index b181b8af..1ed1e34d 100644 --- a/snippets/ruby/array-manipulation/binary-search.md +++ b/snippets/ruby/array-manipulation/binary-search.md @@ -26,7 +26,7 @@ def binary_search(array, target) return nil end -# Usage +# Usage: array = [1, 3, 5, 7, 9] target = 5 result = binary_search(array, target) diff --git a/snippets/ruby/array-manipulation/chunk-array.md b/snippets/ruby/array-manipulation/chunk-array.md index 29bfb50d..102de3c6 100644 --- a/snippets/ruby/array-manipulation/chunk-array.md +++ b/snippets/ruby/array-manipulation/chunk-array.md @@ -10,7 +10,7 @@ def chunk_array(array, size) array.each_slice(size).to_a end -# Example usage: +# Usage: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] chunked_arr = chunk_array(arr, 2) puts chunked_arr.inspect # Output: [[1, 2], [3, 4], [5, 6], [7, 8], [9]] diff --git a/snippets/ruby/array-manipulation/matrix-transpose.md b/snippets/ruby/array-manipulation/matrix-transpose.md index 499cea53..041d35ea 100644 --- a/snippets/ruby/array-manipulation/matrix-transpose.md +++ b/snippets/ruby/array-manipulation/matrix-transpose.md @@ -13,7 +13,7 @@ def transpose_matrix(matrix) matrix.first.zip(*matrix[1..-1]) end -# Usage +# Usage: matrix = [ [1, 2, 3], [4, 5, 6], diff --git a/snippets/ruby/data-structures/binary-tree.md b/snippets/ruby/data-structures/binary-tree.md deleted file mode 100644 index e1d41542..00000000 --- a/snippets/ruby/data-structures/binary-tree.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -title: Binary Tree -description: Implements a basic binary tree with in-order traversal. -author: ACR1209 -tags: data structures,binary tree ---- - -```rb -class TreeNode - attr_accessor :data, :left, :right - - def initialize(data) - @data = data - @left = nil - @right = nil - end -end - -class BinaryTree - attr_accessor :root - - def initialize(root_data) - @root = TreeNode.new(root_data) - end - - def in_order_traversal(node = @root, result = []) - return result unless node - - in_order_traversal(node.left, result) - result << node.data - in_order_traversal(node.right, result) - end -end - -# Usage: -tree = BinaryTree.new(10) -tree.root.left = TreeNode.new(5) -tree.root.right = TreeNode.new(15) -tree.root.left.left = TreeNode.new(3) - -print tree.in_order_traversal # Output: [3, 5, 10, 15] -``` \ No newline at end of file diff --git a/snippets/ruby/data-structures/doubly-linked-list.md b/snippets/ruby/data-structures/doubly-linked-list.md deleted file mode 100644 index 6d6ade07..00000000 --- a/snippets/ruby/data-structures/doubly-linked-list.md +++ /dev/null @@ -1,75 +0,0 @@ ---- -title: Doubly Linked List -description: Implements a doubly linked list with node insertion and traversal. -author: ACR1209 -tags: data structures,linked list,doubly linked list ---- - -```rb -class Node - attr_accessor :data, :next, :prev - - def initialize(data) - @data = data - @next = nil - @prev = nil - end -end - -class DoublyLinkedList - def initialize - @head = nil - @tail = nil - end - - def append(data) - new_node = Node.new(data) - if @head.nil? - @head = new_node - @tail = new_node - else - @tail.next = new_node - new_node.prev = @tail - @tail = new_node - end - end - - def prepend(data) - new_node = Node.new(data) - if @head.nil? - @head = new_node - @tail = new_node - else - new_node.next = @head - @head.prev = new_node - @head = new_node - end - end - - def display_forward - current = @head - while current - print "#{current.data} <-> " - current = current.next - end - puts "nil" - end - - def display_backward - current = @tail - while current - print "#{current.data} <-> " - current = current.prev - end - puts "nil" - end -end - -# Usage: -dll = DoublyLinkedList.new -dll.append(1) -dll.append(2) -dll.append(3) -dll.display_forward # Output: 1 <-> 2 <-> 3 <-> nil -dll.display_backward # Output: 3 <-> 2 <-> 1 <-> nil -``` \ No newline at end of file diff --git a/snippets/ruby/data-structures/singly-linked-list.md b/snippets/ruby/data-structures/singly-linked-list.md deleted file mode 100644 index f50aadce..00000000 --- a/snippets/ruby/data-structures/singly-linked-list.md +++ /dev/null @@ -1,50 +0,0 @@ ---- -title: Singly Linked List -description: Implements a basic singly linked list with node insertion and traversal. -author: ACR1209 -tags: data structures,linked list ---- - -```rb -class Node - attr_accessor :data, :next - - def initialize(data) - @data = data - @next = nil - end -end - -class LinkedList - def initialize - @head = nil - end - - def append(data) - new_node = Node.new(data) - if @head.nil? - @head = new_node - else - current = @head - current = current.next while current.next - current.next = new_node - end - end - - def display - current = @head - while current - print "#{current.data} -> " - current = current.next - end - puts "nil" - end -end - -# Usage: -list = LinkedList.new -list.append(1) -list.append(2) -list.append(3) -list.display # Output: 1 -> 2 -> 3 -> nil -``` \ No newline at end of file diff --git a/snippets/ruby/string-manipulation/capitalize-words.md b/snippets/ruby/string-manipulation/capitalize-words.md index a3baba1c..4287d12f 100644 --- a/snippets/ruby/string-manipulation/capitalize-words.md +++ b/snippets/ruby/string-manipulation/capitalize-words.md @@ -10,7 +10,7 @@ def capitalize_words(str) str.split.map(&:capitalize).join(' ') end -# Usage +# Usage: sentence = "ruby is awesome" puts capitalize_words(sentence) # Output: "Ruby Is Awesome" ``` diff --git a/snippets/ruby/string-manipulation/count-word-occurrences-in-string.md b/snippets/ruby/string-manipulation/count-word-occurrences-in-string.md index b780b527..7765d4b3 100644 --- a/snippets/ruby/string-manipulation/count-word-occurrences-in-string.md +++ b/snippets/ruby/string-manipulation/count-word-occurrences-in-string.md @@ -13,7 +13,7 @@ def count_word_occurrences(text) occurrences end -# Usage +# Usage: text = "ruby is awesome and Ruby is fun" puts count_word_occurrences(text) # Output: {"ruby"=>2, "is"=>2, "awesome"=>1, "and"=>1, "fun"=>1} ``` \ No newline at end of file diff --git a/snippets/ruby/string-manipulation/remove-punctuation.md b/snippets/ruby/string-manipulation/remove-punctuation.md index ff1411ec..d549d53e 100644 --- a/snippets/ruby/string-manipulation/remove-punctuation.md +++ b/snippets/ruby/string-manipulation/remove-punctuation.md @@ -10,7 +10,7 @@ def remove_punctuation(str) str.gsub(/[[:punct:]]/, '') end -# Usage +# Usage: text = "Hello, Ruby! How's it going?" puts remove_punctuation(text) # Output: "Hello Ruby Hows it going" ``` diff --git a/snippets/ruby/string-manipulation/transform-camel-case-to-snake-case.md b/snippets/ruby/string-manipulation/transform-camel-case-to-snake-case.md index 109e9364..1828c98f 100644 --- a/snippets/ruby/string-manipulation/transform-camel-case-to-snake-case.md +++ b/snippets/ruby/string-manipulation/transform-camel-case-to-snake-case.md @@ -1,16 +1,18 @@ --- title: Transform Camel Case to Snake Case -description: Converts a Camel Case string to Snake case. +description: Converts a Camel or Pascal Case string to Snake case. author: ACR1209 -tags: string,convert,camel-case,snake-case +tags: string,convert,camel-case,snake-case,pascal-case --- ```rb def camel_to_snake(str) - str.gsub(/([A-Z])/, '_\1').downcase + str.gsub(/([A-Z])/, '_\1').sub(/^_/, '').downcase end -# Usage +# Usage: camel_case = "camelCaseToSnakeCase" +pascal_case = "PascalCaseToSnakeCase" puts camel_to_snake(camel_case) # Output: "camel_case_to_snake_case" +puts camel_to_snake(pascal_case) # Output: "pascal_case_to_snake_case" ``` \ No newline at end of file diff --git a/snippets/ruby/string-manipulation/transform-from-snake-case-to-camel-case.md b/snippets/ruby/string-manipulation/transform-from-snake-case-to-camel-case.md index 8dcdf088..4714ca3b 100644 --- a/snippets/ruby/string-manipulation/transform-from-snake-case-to-camel-case.md +++ b/snippets/ruby/string-manipulation/transform-from-snake-case-to-camel-case.md @@ -12,7 +12,7 @@ def snake_to_camel(str) }.join end -# Usage +# Usage: snake_case = "snake_case_to_camel_case" puts snake_to_camel(snake_case) # Output: "snakeCaseToCamelCase" ``` diff --git a/snippets/ruby/string-manipulation/transform-from-snake-case-to-pascal-case.md b/snippets/ruby/string-manipulation/transform-from-snake-case-to-pascal-case.md new file mode 100644 index 00000000..92a51a51 --- /dev/null +++ b/snippets/ruby/string-manipulation/transform-from-snake-case-to-pascal-case.md @@ -0,0 +1,18 @@ +--- +title: Transform from Snake Case to Pascal Case +description: Converts a Snake Case string to Pascal Case. +author: ACR1209 +tags: string,convert,snake-case,pascal-case +--- + +```rb +def snake_to_camel(str) + str.split('_').map.with_index { |word, index| + word.capitalize + }.join +end + +# Usage: +snake_case = "snake_case_to_pascal_case" +puts snake_to_camel(snake_case) # Output: "SnakeCaseToPascalCase" +``` diff --git a/snippets/ruby/string-manipulation/truncate-string.md b/snippets/ruby/string-manipulation/truncate-string.md index a2abd9bf..8b6571ac 100644 --- a/snippets/ruby/string-manipulation/truncate-string.md +++ b/snippets/ruby/string-manipulation/truncate-string.md @@ -6,12 +6,12 @@ tags: string,truncate --- ```rb -def truncate_string(max_length, str) - return str if str.length <= max_length +def truncate_string(str, max_length) + return str if str.length <= max_length || max_length <= 3 str[0, max_length - 3] + '...' end -# Usage +# Usage: long_string = "Ruby is a dynamic, open source programming language." puts truncate_string(20, long_string) # Output: "Ruby is a dynamic..." puts truncate_string(54, long_string) # Output: "Ruby is a dynamic, open source programming language." From 0c2e2b33cb3ea6a6217954ee5646f7d1cf187d1f Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 3 Jan 2025 23:54:00 +0000 Subject: [PATCH 16/19] Update consolidated snippets --- public/consolidated/ruby.json | 75 ++++++++++++----------------------- 1 file changed, 25 insertions(+), 50 deletions(-) diff --git a/public/consolidated/ruby.json b/public/consolidated/ruby.json index 008f1c7e..2432be6d 100644 --- a/public/consolidated/ruby.json +++ b/public/consolidated/ruby.json @@ -12,7 +12,7 @@ "search" ], "contributors": [], - "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\nend\n\n# Usage\narray = [1, 3, 5, 7, 9]\ntarget = 5\nresult = binary_search(array, target)\nputs result # Output: 2\n" + "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\nend\n\n# Usage:\narray = [1, 3, 5, 7, 9]\ntarget = 5\nresult = binary_search(array, target)\nputs result # Output: 2\n" }, { "title": "Chunk Array", @@ -23,7 +23,7 @@ "chunk" ], "contributors": [], - "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" + "code": "def chunk_array(array, size)\n array.each_slice(size).to_a\nend\n\n# 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" }, { "title": "Matrix Transpose", @@ -35,7 +35,7 @@ "transpose" ], "contributors": [], - "code": "def transpose_matrix(matrix)\n return [] if matrix.empty?\n return [] if matrix.first.empty?\n\n matrix.first.zip(*matrix[1..-1])\nend\n\n# Usage\nmatrix = [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]\n]\nprint transpose_matrix(matrix) # Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]\n" + "code": "def transpose_matrix(matrix)\n return [] if matrix.empty?\n return [] if matrix.first.empty?\n\n matrix.first.zip(*matrix[1..-1])\nend\n\n# Usage:\nmatrix = [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]\n]\nprint transpose_matrix(matrix) # Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]\n" } ] }, @@ -56,45 +56,6 @@ } ] }, - { - "categoryName": "Data Structures", - "snippets": [ - { - "title": "Binary Tree", - "description": "Implements a basic binary tree with in-order traversal.", - "author": "ACR1209", - "tags": [ - "data structures", - "binary tree" - ], - "contributors": [], - "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\nend\n\nclass 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\nend\n\n# Usage:\ntree = BinaryTree.new(10)\ntree.root.left = TreeNode.new(5)\ntree.root.right = TreeNode.new(15)\ntree.root.left.left = TreeNode.new(3)\n\nprint tree.in_order_traversal # Output: [3, 5, 10, 15]\n" - }, - { - "title": "Doubly Linked List", - "description": "Implements a doubly linked list with node insertion and traversal.", - "author": "ACR1209", - "tags": [ - "data structures", - "linked list", - "doubly linked list" - ], - "contributors": [], - "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" - }, - { - "title": "Singly Linked List", - "description": "Implements a basic singly linked list with node insertion and traversal.", - "author": "ACR1209", - "tags": [ - "data structures", - "linked list" - ], - "contributors": [], - "code": "class Node\n attr_accessor :data, :next\n\n def initialize(data)\n @data = data\n @next = nil\n end\nend\n\nclass 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\nend\n\n# Usage:\nlist = LinkedList.new\nlist.append(1)\nlist.append(2)\nlist.append(3)\nlist.display # Output: 1 -> 2 -> 3 -> nil\n" - } - ] - }, { "categoryName": "Error Handling", "snippets": [ @@ -179,7 +140,7 @@ "words" ], "contributors": [], - "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" + "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" }, { "title": "Count Word Occurrences in String", @@ -191,7 +152,7 @@ "word-count" ], "contributors": [], - "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" + "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" }, { "title": "Remove Punctuation", @@ -203,20 +164,21 @@ "remove" ], "contributors": [], - "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" + "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" }, { "title": "Transform Camel Case to Snake Case", - "description": "Converts a Camel Case string to Snake case.", + "description": "Converts a Camel or Pascal Case string to Snake case.", "author": "ACR1209", "tags": [ "string", "convert", "camel-case", - "snake-case" + "snake-case", + "pascal-case" ], "contributors": [], - "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" + "code": "def camel_to_snake(str)\n str.gsub(/([A-Z])/, '_\\1').sub(/^_/, '').downcase\nend\n\n# Usage:\ncamel_case = \"camelCaseToSnakeCase\"\npascal_case = \"PascalCaseToSnakeCase\"\nputs camel_to_snake(camel_case) # Output: \"camel_case_to_snake_case\"\nputs camel_to_snake(pascal_case) # Output: \"pascal_case_to_snake_case\"\n" }, { "title": "Transform from Snake Case to Camel Case", @@ -229,7 +191,20 @@ "camel-case" ], "contributors": [], - "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" + "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" + }, + { + "title": "Transform from Snake Case to Pascal Case", + "description": "Converts a Snake Case string to Pascal Case.", + "author": "ACR1209", + "tags": [ + "string", + "convert", + "snake-case", + "pascal-case" + ], + "contributors": [], + "code": "def snake_to_camel(str)\n str.split('_').map.with_index { |word, index| \n word.capitalize \n }.join\nend\n\n# Usage:\nsnake_case = \"snake_case_to_pascal_case\"\nputs snake_to_camel(snake_case) # Output: \"SnakeCaseToPascalCase\"\n" }, { "title": "Truncate String", @@ -240,7 +215,7 @@ "truncate" ], "contributors": [], - "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" + "code": "def truncate_string(str, max_length)\n return str if str.length <= max_length || max_length <= 3\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" } ] } From e40c8123d6480f5c196e3b3121b8b30d27c91548 Mon Sep 17 00:00:00 2001 From: ACR1209 Date: Sat, 4 Jan 2025 13:59:24 -0500 Subject: [PATCH 17/19] Rename function to transform from snake_case to PascalCase in Ruby snippet --- .../transform-from-snake-case-to-pascal-case.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/ruby/string-manipulation/transform-from-snake-case-to-pascal-case.md b/snippets/ruby/string-manipulation/transform-from-snake-case-to-pascal-case.md index 92a51a51..8a8ef980 100644 --- a/snippets/ruby/string-manipulation/transform-from-snake-case-to-pascal-case.md +++ b/snippets/ruby/string-manipulation/transform-from-snake-case-to-pascal-case.md @@ -6,7 +6,7 @@ tags: string,convert,snake-case,pascal-case --- ```rb -def snake_to_camel(str) +def snake_to_pascal(str) str.split('_').map.with_index { |word, index| word.capitalize }.join @@ -14,5 +14,5 @@ end # Usage: snake_case = "snake_case_to_pascal_case" -puts snake_to_camel(snake_case) # Output: "SnakeCaseToPascalCase" +puts snake_to_pascal(snake_case) # Output: "SnakeCaseToPascalCase" ``` From bebe1c7683783971b39550c49428612a321d7383 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sat, 4 Jan 2025 19:01:06 +0000 Subject: [PATCH 18/19] Update consolidated snippets --- public/consolidated/ruby.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/consolidated/ruby.json b/public/consolidated/ruby.json index 2432be6d..5d1da9f7 100644 --- a/public/consolidated/ruby.json +++ b/public/consolidated/ruby.json @@ -204,7 +204,7 @@ "pascal-case" ], "contributors": [], - "code": "def snake_to_camel(str)\n str.split('_').map.with_index { |word, index| \n word.capitalize \n }.join\nend\n\n# Usage:\nsnake_case = \"snake_case_to_pascal_case\"\nputs snake_to_camel(snake_case) # Output: \"SnakeCaseToPascalCase\"\n" + "code": "def snake_to_pascal(str)\n str.split('_').map.with_index { |word, index| \n word.capitalize \n }.join\nend\n\n# Usage:\nsnake_case = \"snake_case_to_pascal_case\"\nputs snake_to_pascal(snake_case) # Output: \"SnakeCaseToPascalCase\"\n" }, { "title": "Truncate String", From 0ac16f027b6589b6bb9967b04486ce2187f25b7e Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sat, 4 Jan 2025 19:04:34 +0000 Subject: [PATCH 19/19] Update consolidated snippets --- public/consolidated/c.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/consolidated/c.json b/public/consolidated/c.json index 1588cd69..a9258611 100644 --- a/public/consolidated/c.json +++ b/public/consolidated/c.json @@ -38,7 +38,7 @@ "numbers" ], "contributors": [], - "code": "#include\nvoid swap(int* num1,int* num2){\n *num1 = *num1 + *num2;\n *num2 = *num1 - *num2;\n *num1 = *num1 - *num2;\n}\n\n// Usage:\nint a = 3,b = 4;\nswap(&a,&b); // simply use printf after this to print swapped values\n" + "code": "#include\nvoid swap(int* num1,int* num2){\n *num1 = *num1 + *num2;\n *num2 = *num1 - *num2;\n *num1 = *num1 - *num2;\n}\n\n// Usage:\nint a = 3,b = 4;\nswap(&a,&b); // swaps the values of the a and b variables\n" } ] }