Skip to content
Turkish character support for core ruby methods.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
spec
.gitignore
.hound.yml
.rspec
.rubocop.yml
.travis.yml
CODE_OF_CONDUCT.md
Gemfile
LICENSE.txt
README.md
Rakefile
turkish_support.gemspec

README.md

TurkishSupport

Gem Version Build Status

Turkish character support for core ruby methods. This gem provides support nearly all String methods, such as String#upcase, String#downcase, String#match, String#gsub. It also provides support for Array#sortand some bonus methods like String#titleize.

Requirements

  • Ruby >= 2.2.0
  • Rails >= 4.0.0

Notice: TurkishSupport uses refinements instead of monkey patching.

Installation

Add this line to your application's Gemfile:

gem 'turkish_support'

And then execute:

$ bundle

Or install it yourself as:

$ gem install turkish_support

Usage

After the installation of the gem, you should follow these steps.

Using with ruby

  • Require the gem:
  require 'turkish_support'
  • Add using TurkishSupport line to where you want to activate it.
  using TurkishSupport

Example usage inside a class:

require 'turkish_support'

class Test
  using TurkishSupport
  def up_me(str)
    str.upcase
  end
end

Test.new.up_me('içel')  # => "İÇEL"

Using with ruby on rails

Note: You don't need to require, because it is already required by the rails.

  • You must add using TurkishSupport line to the top of the scope.
  using TurkishSupport

  class SampleModel < ActiveRecord::Base
    ...
  end
  • If you want to use TurkishSupport with a custom class or a module that is not inherited from any rails tie, you must add using TurkishSupport line to the class or module.
  class CustomClass
    using TurkishSupport

    ...
  end

Using Core Methods

If you want to use original set of the core methods in the same scope, you can use Object#public_send:

  str = 'Bağcılar'
  str.public_send(:upcase)  # => "BAğCıLAR"

String Methods

<=> (spaceship)

  'Cahit' <=> 'Çağla' # => -1
  'Sıtkı' <=> 'Ömer'  # =>  1
  'Eren'  <=> 'Eren'  # =>  0
  'c'     <=> 'ca'    # => -1

[] and []=

  'Türkiye Cumhuriyeti'[/\w+/] # => "Türkiye"
  'Çetin'[/[a-ğ]+/i]           # => "Çe"

=~ (equal-tilda)

  'Bağlarbaşı Çarşı Kalabalık' =~ (/[s-ü]+/i) # => 8

capitalize and capitalize!

  str = 'türkÇE desteĞİ'

  str.capitalize    # => "Türkçe desteği"
  str.capitalize!   # => "Türkçe desteği"

casecmp

  'sıtKI'.casecmp('SITkı')     # => 0

downcase and downcase!

  str = 'İSMAİL'

  str.downcase    # => "ismail"
  str.downcase!   # => "ismail"

gsub and gsub!

 'ağapaşaağa'.gsub(/[a-h]+/, 'bey')

index

  '?ç-!+*/-ğüı'.index(/\w+/)        # => 1
  '?ç-!+*/-ğüı'.index(/[a-z]+/, 2)  # => 8

match

  'Aşağı'.match(/\w+/)
  # => #<MatchData "Aşağı">

  'Aşağı Ayrancı'.match(/^\w+\s\w+$/)
  # => #<MatchData "Aşağı Ayrancı">

  'aüvvağğ öövvaağ'.match(/^[a-z]+\s[a-z]+$/)
  # => #<MatchData "aüvvağğ öövvaağ">

  'BAĞCIlar'.match(/[A-Z]+/)
  # => #<MatchData "BAĞCI">

  'Aşağı Ayrancı'.match(/\W+/)
  # => #<MatchData "">

partition

  'Bağlarbaşı Çarşı'.partition(/\W+/) # => ["Bağlarbaşı", " ", "Çarşı"]

rpartition

  'Bağlarbaşı Çarşı Kalabalık'.rpartition(/\W+/)
  # => ["Bağlarbaşı Çarşı", " ", "Kalabalık"]

rindex

  'şç-!+*/-ğüı'.rindex(/\w+/, 7)  # => 1

scan

  'Bağlarbaşı Çarşı Kalabalık'.scan(/[a-z]+/i)
  # => ["Bağlarbaşı", "Çarşı", "Kalabalık"]

slice and slice!

  'Duayen'.slice(/[^h-ö]+/) # => "Duaye"

split

  'Bağlarbaşı Çarşı Kalabalık'.split(/\W+/)
  # => ["Bağlarbaşı", "Çarşı", "Kalabalık"]

  'Bağlarbaşı Çarşı Kalabalık'.split(/[ç-ş]+/)
  # => ["Ba", "a", "ba", " Ça", " Ka", "aba"]

sub and sub!

  'ağapaşaağa'.sub(/[a-h]+/, 'bey')  # => "beypaşaağa"

swapcase and swapcase!

  'TuğÇE'.swapcase    # => "tUĞçe"
  'TuğÇE'.swapcase!   # => "tUĞçe"

titleize and titleize!

These methods are not core methods of ruby, but they are accepted as useful in most situations.

  'türkÇE desteĞİ'.titleize           # => "Türkçe Desteği"
  'türkÇE desteĞİ'.titleize!          # => "Türkçe Desteği"

  # Parenthesis, quotes, etc. support
  "rUBY roCkS... (really! 'tRUSt' ME)".titleize
  # => "Ruby Rocks... (Really! 'Trust' Me)"

  # If you don't want to capitalize conjuctions,
  # simply pass a false value as parameter
  "kerem VE aslı VeYa leyla İlE mecnun".titleize(false)
  # => "Kerem ve Aslı veya Leyla ile Mecnun"

upcase and upcase!

  str = 'Bağcılar'

  str.upcase    # => "BAĞCILAR"
  str           # => "Bağcılar"

  str.upcase!   # => "BAĞCILAR"
  str           # => "BAĞCILAR"

Array Methods

sort and sort!

  %w(iki üç dört ılık iğne iyne).sort
  # => ["dört", "ılık", "iğne", "iki", "iyne", "üç"]

Contributing

  1. Fork it ( http://github.com/sbagdat/turkish_support/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
You can’t perform that action at this time.