Skip to content

Add Ruby Language #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
85ff2d8
Create Ruby programming language
ACR1209 Jan 2, 2025
f1291ff
Added ruby array manipulation category and some snippets
ACR1209 Jan 2, 2025
92c408c
Add Ruby string manipulation snippets
ACR1209 Jan 2, 2025
993a50a
Add Ruby snippet for calculating compound interest
ACR1209 Jan 2, 2025
43f49a1
Add Ruby Sieve of Sundaram snippet
ACR1209 Jan 2, 2025
3807b5c
Add Ruby snippet for checking prime numbers
ACR1209 Jan 2, 2025
ec3ee2b
Add Ruby snippet for calculating factorial
ACR1209 Jan 2, 2025
cdbd348
Add Ruby snippet for defining and raising a custom error class
ACR1209 Jan 2, 2025
88fcb81
Add Ruby snippet for implementing a basic binary tree with in-order t…
ACR1209 Jan 2, 2025
d6364c6
Add Ruby snippet for implementing a singly linked list with node inse…
ACR1209 Jan 2, 2025
9f15bd9
Add Ruby snippet for implementing a doubly linked list with node inse…
ACR1209 Jan 2, 2025
108ca7b
Update consolidated snippets
actions-user Jan 2, 2025
5caa33c
Fix to fit new guidelines for snippets
ACR1209 Jan 2, 2025
461ae2d
Update consolidated snippets
actions-user Jan 2, 2025
28703fa
Apply feedback to align with contributing guidelines and add new snip…
ACR1209 Jan 3, 2025
0c2e2b3
Update consolidated snippets
actions-user Jan 3, 2025
e40c812
Rename function to transform from snake_case to PascalCase in Ruby sn…
ACR1209 Jan 4, 2025
bebe1c7
Update consolidated snippets
actions-user Jan 4, 2025
0c87a21
Merge branch 'main' into ruby-language
ACR1209 Jan 4, 2025
0ac16f0
Update consolidated snippets
actions-user Jan 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions public/consolidated/_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
"lang": "REGEX",
"icon": "/icons/regex.svg"
},
{
"lang": "RUBY",
"icon": "/icons/ruby.svg"
},
{
"lang": "RUST",
"icon": "/icons/rust.svg"
Expand Down
2 changes: 1 addition & 1 deletion public/consolidated/c.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"numbers"
],
"contributors": [],
"code": "#include<stdio.h>\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<stdio.h>\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"
}
]
}
Expand Down
222 changes: 222 additions & 0 deletions public/consolidated/ruby.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
]
Loading