Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Mathys-Gasnier merged 20 commits into quicksnip-dev:main from ACR1209:ruby-language
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add Ruby string manipulation snippets
  • Loading branch information
ACR1209 committed Jan 2, 2025
commit 92c408cb845d3b209085afc406249cf4b26e912c
15 changes: 15 additions & 0 deletions snippets/ruby/string-manipulation/camelcase-to-snakecase.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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"
```
15 changes: 15 additions & 0 deletions snippets/ruby/string-manipulation/capitalize-words.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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"
```
18 changes: 18 additions & 0 deletions snippets/ruby/string-manipulation/count-word-ocurrences.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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}
```
15 changes: 15 additions & 0 deletions snippets/ruby/string-manipulation/remove-punctuation.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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"
```
17 changes: 17 additions & 0 deletions snippets/ruby/string-manipulation/snakecase-to-camelcase.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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"
```
17 changes: 17 additions & 0 deletions snippets/ruby/string-manipulation/truncate-string.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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."
```

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /