StringMagic super-charges Ruby's String
with batteries-included helpers for formatting, validation, analysis, transformation, and security all in one tidy gem.
- Zero dependencies – pure Ruby
- Non-destructive: original strings are never mutated
- Thread-safe & fully tested (160+ RSpec examples, ~98% coverage)
- Drop-in: optionally auto-mixes into String, or use module methods (
StringMagic.to_slug(...)
)
# Formatting
"Hello world".truncate_words(1) # => "Hello..."
"user@domain.com".mask_emails # => "u********@domain.com"
"Ruby on Rails".highlight('Ruby') # => "<mark>Ruby</mark> on Rails"
# Validation
"test@example.com".email? # => true
"4111111111111111".credit_card? # => true
"racecar".palindrome? # => true
# Transformation
"CamelCase".to_snake_case # => "camel_case"
"user input".to_filename_safe # => "user_input"
"second".ordinalize # => "2nd"
# Analysis
"Visit https://example.com".extract_urls # => ["https://example.com"]
"Great product!".sentiment_indicators # => {:positive=>1.0, :negative=>0.0, :neutral=>0}
gem install string_magic
Or add to your Gemfile:
gem 'string_magic'
- Case conversion:
to_snake_case
,to_kebab_case
,to_camel_case
,to_pascal_case
,to_title_case
- Text manipulation:
reverse_words
,alternating_case
,remove_duplicate_chars
,remove_duplicate_words
- HTML handling:
remove_html_tags
,escape_html
- Format checks:
email?
,url?
,phone?
,credit_card?
- Text relations:
palindrome?
,anagram_of?
- Password strength:
strong_password?
- Entity extraction:
extract_emails
,extract_urls
,extract_phones
,extract_dates
- Text metrics:
readability_score
,word_frequency
,sentiment_indicators
- Truncation:
truncate_words
,truncate_sentences
,truncate_characters
,smart_truncate
- Highlighting:
highlight
,remove_highlights
,highlight_urls
- Data masking:
mask_sensitive_data
,mask_credit_cards
,mask_emails
,mask_phones
- Detection:
contains_sensitive_data?
- Inflection:
to_plural
,to_singular
,ordinalize
,humanize
- Slug generation:
to_slug
,to_url_slug
,to_filename_safe
- Fork →
git checkout -b feature/awesome
- Add specs for your change
bundle exec rspec
- PR ✉️ – we love improvements!
MIT - See LICENSE for details.