Skip to content

Commit 92c408c

Browse files
committed
Add Ruby string manipulation snippets
1 parent f1291ff commit 92c408c

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Transform Camel Case to Snake Case
3+
description: Converts a Camel Case string to Snake case.
4+
author: ACR1209
5+
tags: ruby,string,convert,camel-case,snake-case,utility
6+
---
7+
8+
```rb
9+
def camel_to_snake(str)
10+
str.gsub(/([A-Z])/, '_\1').downcase
11+
end
12+
13+
camel_case = "camelCaseToSnakeCase"
14+
puts camel_to_snake(camel_case) # Output: "camel_case_to_snake_case"
15+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Capitalize Words
3+
description: Capitalizes the first letter of each word in a string.
4+
author: ACR1209
5+
tags: ruby,string,capitalize,words
6+
---
7+
8+
```rb
9+
def capitalize_words(str)
10+
str.split.map(&:capitalize).join(' ')
11+
end
12+
13+
sentence = "ruby is awesome"
14+
puts capitalize_words(sentence) # Output: "Ruby Is Awesome"
15+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Count Word Occurrences in String
3+
description: Counts the occurrences of each word in a given string.
4+
author: ACR1209
5+
tags: ruby,string,occurrences,word-count
6+
---
7+
8+
```rb
9+
def count_word_occurrences(text)
10+
words = text.downcase.scan(/\w+/)
11+
occurrences = Hash.new(0)
12+
words.each { |word| occurrences[word] += 1 }
13+
occurrences
14+
end
15+
16+
text = "ruby is awesome and Ruby is fun"
17+
puts count_word_occurrences(text) # Output: {"ruby"=>2, "is"=>2, "awesome"=>1, "and"=>1, "fun"=>1}
18+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Remove Punctuation
3+
description: Removes all punctuation from a given string.
4+
author: ACR1209
5+
tags: ruby,string,punctuation,remove
6+
---
7+
8+
```rb
9+
def remove_punctuation(str)
10+
str.gsub(/[[:punct:]]/, '')
11+
end
12+
13+
text = "Hello, Ruby! How's it going?"
14+
puts remove_punctuation(text) # Output: "Hello Ruby Hows it going"
15+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Transform from Snake Case to Camel Case
3+
description: Converts a Snake Case string to Camel Case.
4+
author: ACR1209
5+
tags: ruby,string,convert,snake-case,camel-case,utility
6+
---
7+
8+
```rb
9+
def snake_to_camel(str)
10+
str.split('_').map.with_index { |word, index|
11+
index == 0 ? word : word.capitalize
12+
}.join
13+
end
14+
15+
snake_case = "snake_case_to_camel_case"
16+
puts snake_to_camel(snake_case) # Output: "snakeCaseToCamelCase"
17+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Truncate Strings
3+
description: Truncates a string to a specified length, optionally adding an ellipsis.
4+
author: ACR1209
5+
tags: ruby,string,truncate,utility
6+
---
7+
8+
```rb
9+
def truncate_string(max_length, str)
10+
return str if str.length <= max_length
11+
str[0, max_length - 3] + '...'
12+
end
13+
14+
long_string = "Ruby is a dynamic, open source programming language."
15+
puts truncate_string(20, long_string) # Output: "Ruby is a dynamic..."
16+
puts truncate_string(54, long_string) # Output: "Ruby is a dynamic, open source programming language."
17+
```

0 commit comments

Comments
 (0)