Class: String

Inherits:
Object
  • Object
show all
Includes:
Sconv
Defined in:
lib/sconv.rb

Overview

Helper Functions for String Class

Instance Method Summary collapse

Methods included from Sconv

#EUC?, #SJIS?, #UTF8?, #guess_ces, #normalize, #sconv

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

Provides methods of the form String#inputEncoding_to_outputEnoding and String#inputEncoding_to_outputEncoding!. Examples: String#UTF8_to_ISO88591



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/sconv.rb', line 127

def method_missing (method_name)
  okay = false
  if method_name.to_s =~ /^(.+)_to_([^!]+)(!)?$/
    from_code, to_code, exclamation = $1.normalize, $2.normalize, $3
    destructive = exclamation == '!'
    okay = true
    # test whether we can convert these encodings
    begin
      ''.sconv(from_code, to_code)
    end
    if okay
      if destructive
        self.class.send :define_method, method_name.to_sym do
          replace self.sconv(from_code, to_code)
        end
      else
        self.class.send :define_method, method_name.to_sym do
          sconv(from_code, to_code)
        end
      end
      send method_name
    else
      sconf_old_method_missing
    end
  end
  
  if method_name.to_s =~ /^to_(\w+)$/
    method_called = 'to' + $1.downcase
    self.class.send :define_method, method_name.to_sym do
      send method_called.to_sym
    end
    send method_name
  end
end

Instance Method Details

#sconf_old_method_missingObject



122
# File 'lib/sconv.rb', line 122

alias sconf_old_method_missing method_missing