diff --git a/public/consolidated/_index.json b/public/consolidated/_index.json index 48d9f945..fb8c7e77 100644 --- a/public/consolidated/_index.json +++ b/public/consolidated/_index.json @@ -39,6 +39,10 @@ "lang": "REGEX", "icon": "/icons/regex.svg" }, + { + "lang": "RUBY", + "icon": "/icons/ruby.svg" + }, { "lang": "RUST", "icon": "/icons/rust.svg" 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" } ] } diff --git a/public/consolidated/ruby.json b/public/consolidated/ruby.json new file mode 100644 index 00000000..5d1da9f7 --- /dev/null +++ b/public/consolidated/ruby.json @@ -0,0 +1,222 @@ +[ + { + "categoryName": "Array Manipulation", + "snippets": [ + { + "title": "Binary Search", + "description": "Searches for an element in a sorted array using binary search.", + "author": "ACR1209", + "tags": [ + "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": [ + "array", + "chunk" + ], + "contributors": [], + "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", + "description": "Transposes a 2D matrix.", + "author": "ACR1209", + "tags": [ + "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": [ + "printing", + "hello-world", + "utility" + ], + "contributors": [], + "code": "puts 'Hello, World!'\n" + } + ] + }, + { + "categoryName": "Error Handling", + "snippets": [ + { + "title": "Custom Error Class", + "description": "Defines and raises a custom error class in Ruby.", + "author": "ACR1209", + "tags": [ + "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": [ + "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": [ + "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": [ + "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": [ + "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": "Capitalize Words", + "description": "Capitalizes the first letter of each word in a string.", + "author": "ACR1209", + "tags": [ + "string", + "capitalize", + "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" + }, + { + "title": "Count Word Occurrences in String", + "description": "Counts the occurrences of each word in a given string.", + "author": "ACR1209", + "tags": [ + "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\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": [ + "string", + "punctuation", + "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" + }, + { + "title": "Transform Camel Case to Snake Case", + "description": "Converts a Camel or Pascal Case string to Snake case.", + "author": "ACR1209", + "tags": [ + "string", + "convert", + "camel-case", + "snake-case", + "pascal-case" + ], + "contributors": [], + "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", + "description": "Converts a Snake Case string to Camel Case.", + "author": "ACR1209", + "tags": [ + "string", + "convert", + "snake-case", + "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" + }, + { + "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_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", + "description": "Truncates a string to a specified length, optionally adding an ellipsis.", + "author": "ACR1209", + "tags": [ + "string", + "truncate" + ], + "contributors": [], + "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" + } + ] + } +] \ 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/snippets/ruby/array-manipulation/binary-search.md b/snippets/ruby/array-manipulation/binary-search.md new file mode 100644 index 00000000..1ed1e34d --- /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: 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..102de3c6 --- /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: array,chunk +--- + +```rb +def chunk_array(array, size) + array.each_slice(size).to_a +end + +# 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..041d35ea --- /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: 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 diff --git a/snippets/ruby/basics/hello-world.md b/snippets/ruby/basics/hello-world.md new file mode 100644 index 00000000..962c50c3 --- /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: printing,hello-world,utility +--- + +```rb +puts 'Hello, World!' +``` diff --git a/snippets/ruby/error-handling/custom-error-class.md b/snippets/ruby/error-handling/custom-error-class.md new file mode 100644 index 00000000..af9ed6ef --- /dev/null +++ b/snippets/ruby/error-handling/custom-error-class.md @@ -0,0 +1,22 @@ +--- +title: Custom Error Class +description: Defines and raises a custom error class in Ruby. +author: ACR1209 +tags: 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 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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..c13faf26 --- /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: math,compound interest,finance +--- + +```rb +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 +``` 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..b4110ee6 --- /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: 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 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..e6f60928 --- /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: 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 +``` diff --git a/snippets/ruby/math-and-numbers/find-all-primes-up-to-integer-sieve-of-sundaram.md b/snippets/ruby/math-and-numbers/find-all-primes-up-to-integer-sieve-of-sundaram.md new file mode 100644 index 00000000..123f970e --- /dev/null +++ b/snippets/ruby/math-and-numbers/find-all-primes-up-to-integer-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: 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 diff --git a/snippets/ruby/string-manipulation/capitalize-words.md b/snippets/ruby/string-manipulation/capitalize-words.md new file mode 100644 index 00000000..4287d12f --- /dev/null +++ b/snippets/ruby/string-manipulation/capitalize-words.md @@ -0,0 +1,16 @@ +--- +title: Capitalize Words +description: Capitalizes the first letter of each word in a string. +author: ACR1209 +tags: string,capitalize,words +--- + +```rb +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-occurrences-in-string.md b/snippets/ruby/string-manipulation/count-word-occurrences-in-string.md new file mode 100644 index 00000000..7765d4b3 --- /dev/null +++ b/snippets/ruby/string-manipulation/count-word-occurrences-in-string.md @@ -0,0 +1,19 @@ +--- +title: Count Word Occurrences in String +description: Counts the occurrences of each word in a given string. +author: ACR1209 +tags: 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 + +# 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 new file mode 100644 index 00000000..d549d53e --- /dev/null +++ b/snippets/ruby/string-manipulation/remove-punctuation.md @@ -0,0 +1,16 @@ +--- +title: Remove Punctuation +description: Removes all punctuation from a given string. +author: ACR1209 +tags: string,punctuation,remove +--- + +```rb +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/transform-camel-case-to-snake-case.md b/snippets/ruby/string-manipulation/transform-camel-case-to-snake-case.md new file mode 100644 index 00000000..1828c98f --- /dev/null +++ b/snippets/ruby/string-manipulation/transform-camel-case-to-snake-case.md @@ -0,0 +1,18 @@ +--- +title: Transform Camel Case to Snake Case +description: Converts a Camel or Pascal Case string to Snake case. +author: ACR1209 +tags: string,convert,camel-case,snake-case,pascal-case +--- + +```rb +def camel_to_snake(str) + str.gsub(/([A-Z])/, '_\1').sub(/^_/, '').downcase +end + +# 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 new file mode 100644 index 00000000..4714ca3b --- /dev/null +++ b/snippets/ruby/string-manipulation/transform-from-snake-case-to-camel-case.md @@ -0,0 +1,18 @@ +--- +title: Transform from Snake Case to Camel Case +description: Converts a Snake Case string to Camel Case. +author: ACR1209 +tags: string,convert,snake-case,camel-case +--- + +```rb +def snake_to_camel(str) + str.split('_').map.with_index { |word, index| + index == 0 ? word : word.capitalize + }.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/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..8a8ef980 --- /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_pascal(str) + str.split('_').map.with_index { |word, index| + word.capitalize + }.join +end + +# Usage: +snake_case = "snake_case_to_pascal_case" +puts snake_to_pascal(snake_case) # Output: "SnakeCaseToPascalCase" +``` diff --git a/snippets/ruby/string-manipulation/truncate-string.md b/snippets/ruby/string-manipulation/truncate-string.md new file mode 100644 index 00000000..8b6571ac --- /dev/null +++ b/snippets/ruby/string-manipulation/truncate-string.md @@ -0,0 +1,18 @@ +--- +title: Truncate String +description: Truncates a string to a specified length, optionally adding an ellipsis. +author: ACR1209 +tags: string,truncate +--- + +```rb +def truncate_string(str, max_length) + return str if str.length <= max_length || max_length <= 3 + 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." +```