Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/parsi-digits.rb

Instance Method Summary collapse

Instance Method Details

#has_parsi_digits?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/parsi-digits.rb', line 74

def has_parsi_digits?
  self =~ /[۰-۹]/
end

#remove_delimiters(options = {locale: :fa}) ⇒ Object



78
79
80
81
82
83
# File 'lib/parsi-digits.rb', line 78

def remove_delimiters options={locale: :fa}
  locale = options[:locale]
  delimiter = options[:delimiter]
  delimiter ||= Parsi::Digits.locale_delimiter locale: locale
  gsub delimiter, ''
end

#remove_delimiters!Object



85
86
87
88
89
90
# File 'lib/parsi-digits.rb', line 85

def remove_delimiters!
  locale = options[:locale]
  delimiter = options[:delimiter]
  delimiter ||= Parsi::Digits.locale_delimiter locale: locale
  gsub! delimiter, ''
end

#to_fObject



102
103
104
105
106
107
108
109
# File 'lib/parsi-digits.rb', line 102

def to_f
  if has_parsi_digits?
    separator = Parsi::Digits.fa_separator
    with_western_digits.sub(separator, '.').western_to_f
  else
    western_to_f
  end
end

#to_i(base = 10) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/parsi-digits.rb', line 93

def to_i base=10
  if has_parsi_digits?
    with_western_digits.western_to_i base
  else
    western_to_i base
  end
end

#western_to_fObject



101
# File 'lib/parsi-digits.rb', line 101

alias_method :western_to_f, :to_f

#western_to_iObject



92
# File 'lib/parsi-digits.rb', line 92

alias_method :western_to_i, :to_i

#with_parsi_digitsObject



58
59
60
# File 'lib/parsi-digits.rb', line 58

def with_parsi_digits
  gsub(/\d/) { |d| Parsi::Digits.to_parsi d }
end

#with_parsi_digits!Object



62
63
64
# File 'lib/parsi-digits.rb', line 62

def with_parsi_digits!
  gsub!(/\d/) { |d| Parsi::Digits.to_parsi d }
end

#with_western_digitsObject



66
67
68
# File 'lib/parsi-digits.rb', line 66

def with_western_digits
  gsub(/[۰-۹]/) { |d| Parsi::Digits.to_western d }
end

#with_western_digits!Object



70
71
72
# File 'lib/parsi-digits.rb', line 70

def with_western_digits!
  gsub!(/[۰-۹]/) { |d| Parsi::Digits.to_western d }
end