Skip to content
This repository
Browse code

remove checks for encodings availability

  • Loading branch information...
commit 1e9e88fcd335c7d5a99159d592c3e1b605510a16 1 parent 4c1701c
Sergey Nartimov authored December 25, 2011

Showing 25 changed files with 168 additions and 298 deletions. Show diff stats Hide diff stats

  1. 4  actionmailer/test/base_test.rb
  2. 2  actionpack/lib/action_dispatch/http/request.rb
  3. 2  actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
  4. 2  actionpack/lib/action_view/helpers/capture_helper.rb
  5. 2  actionpack/test/controller/routing_test.rb
  6. 2  actionpack/test/controller/send_file_test.rb
  7. 2  actionpack/test/controller/test_test.rb
  8. 2  actionpack/test/dispatch/request/multipart_params_parsing_test.rb
  9. 16  actionpack/test/template/output_buffer_test.rb
  10. 70  actionpack/test/template/render_test.rb
  11. 88  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
  12. 2  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
  13. 16  activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
  14. 6  activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
  15. 6  activerecord/test/cases/binary_test.rb
  16. 2  activerecord/test/cases/fixtures_test.rb
  17. 112  activesupport/lib/active_support/core_ext/string/access.rb
  18. 8  activesupport/lib/active_support/json/encoding.rb
  19. 4  activesupport/lib/active_support/multibyte/chars.rb
  20. 55  activesupport/lib/active_support/multibyte/utils.rb
  21. 12  activesupport/test/buffered_logger_test.rb
  22. 40  activesupport/test/caching_test.rb
  23. 4  activesupport/test/json/encoding_test.rb
  24. 5  activesupport/test/multibyte_test_helpers.rb
  25. 2  railties/test/application/initializers/frameworks_test.rb
4  actionmailer/test/base_test.rb
@@ -107,7 +107,7 @@ def teardown
107 107
     assert_equal(1, email.attachments.length)
108 108
     assert_equal('invoice.jpg', email.attachments[0].filename)
109 109
     expected = "\312\213\254\232)b"
110  
-    expected.force_encoding(Encoding::BINARY) if '1.9'.respond_to?(:force_encoding)
  110
+    expected.force_encoding(Encoding::BINARY)
111 111
     assert_equal expected, email.attachments['invoice.jpg'].decoded
112 112
   end
113 113
 
@@ -116,7 +116,7 @@ def teardown
116 116
     assert_equal(1, email.attachments.length)
117 117
     assert_equal('invoice.jpg', email.attachments[0].filename)
118 118
     expected = "\312\213\254\232)b"
119  
-    expected.force_encoding(Encoding::BINARY) if '1.9'.respond_to?(:force_encoding)
  119
+    expected.force_encoding(Encoding::BINARY)
120 120
     assert_equal expected, email.attachments['invoice.jpg'].decoded
121 121
   end
122 122
 
2  actionpack/lib/action_dispatch/http/request.rb
@@ -189,7 +189,7 @@ def raw_post
189 189
     # variable is already set, wrap it in a StringIO.
190 190
     def body
191 191
       if raw_post = @env['RAW_POST_DATA']
192  
-        raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding)
  192
+        raw_post.force_encoding(Encoding::BINARY)
193 193
         StringIO.new(raw_post)
194 194
       else
195 195
         @env['rack.input']
2  actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
@@ -30,7 +30,7 @@ def initialize(app, options = {})
30 30
 
31 31
       def generate_sid
32 32
         sid = SecureRandom.hex(16)
33  
-        sid.encode!('UTF-8') if sid.respond_to?(:encode!)
  33
+        sid.encode!('UTF-8')
34 34
         sid
35 35
       end
36 36
 
2  actionpack/lib/action_view/helpers/capture_helper.rb
@@ -181,7 +181,7 @@ def content_for?(name)
181 181
       def with_output_buffer(buf = nil) #:nodoc:
182 182
         unless buf
183 183
           buf = ActionView::OutputBuffer.new
184  
-          buf.force_encoding(output_buffer.encoding) if output_buffer.respond_to?(:encoding) && buf.respond_to?(:force_encoding)
  184
+          buf.force_encoding(output_buffer.encoding) if output_buffer
185 185
         end
186 186
         self.output_buffer, old_buffer = buf, output_buffer
187 187
         yield
2  actionpack/test/controller/routing_test.rb
@@ -483,7 +483,7 @@ def test_route_with_text_default
483 483
     assert_equal({ :controller => "content", :action => 'show_page', :id => 'foo' }, rs.recognize_path("/page/foo"))
484 484
 
485 485
     token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in Russian
486  
-    token.force_encoding(Encoding::BINARY) if token.respond_to?(:force_encoding)
  486
+    token.force_encoding(Encoding::BINARY)
487 487
     escaped_token = CGI::escape(token)
488 488
 
489 489
     assert_equal '/page/' + escaped_token, url_for(rs, { :controller => 'content', :action => 'show_page', :id => token })
2  actionpack/test/controller/send_file_test.rb
@@ -61,7 +61,7 @@ def test_file_stream
61 61
     require 'stringio'
62 62
     output = StringIO.new
63 63
     output.binmode
64  
-    output.string.force_encoding(file_data.encoding) if output.string.respond_to?(:force_encoding)
  64
+    output.string.force_encoding(file_data.encoding)
65 65
     assert_nothing_raised { response.body_parts.each { |part| output << part.to_s } }
66 66
     assert_equal file_data, output.string
67 67
   end
2  actionpack/test/controller/test_test.rb
@@ -673,7 +673,7 @@ def test_test_uploaded_file
673 673
     path = "#{FILES_DIR}/#{filename}"
674 674
     content_type = 'image/png'
675 675
     expected = File.read(path)
676  
-    expected.force_encoding(Encoding::BINARY) if expected.respond_to?(:force_encoding)
  676
+    expected.force_encoding(Encoding::BINARY)
677 677
 
678 678
     file = Rack::Test::UploadedFile.new(path, content_type)
679 679
     assert_equal filename, file.original_filename
2  actionpack/test/dispatch/request/multipart_params_parsing_test.rb
@@ -89,7 +89,7 @@ def teardown
89 89
 
90 90
     # Rack doesn't handle multipart/mixed for us.
91 91
     files = params['files']
92  
-    files.force_encoding('ASCII-8BIT') if files.respond_to?(:force_encoding)
  92
+    files.force_encoding('ASCII-8BIT')
93 93
     assert_equal 19756, files.size
94 94
   end
95 95
 
16  actionpack/test/template/output_buffer_test.rb
@@ -39,15 +39,13 @@ def setup
39 39
     assert_equal ['foo', 'bar'], body_parts
40 40
   end
41 41
 
42  
-  if '1.9'.respond_to?(:force_encoding)
43  
-    test 'flushing preserves output buffer encoding' do
44  
-      original_buffer = ' '.force_encoding(Encoding::EUC_JP)
45  
-      @vc.output_buffer = original_buffer
46  
-      @vc.flush_output_buffer
47  
-      assert_equal ['foo', original_buffer], body_parts
48  
-      assert_not_equal original_buffer, output_buffer
49  
-      assert_equal Encoding::EUC_JP, output_buffer.encoding
50  
-    end
  42
+  test 'flushing preserves output buffer encoding' do
  43
+    original_buffer = ' '.force_encoding(Encoding::EUC_JP)
  44
+    @vc.output_buffer = original_buffer
  45
+    @vc.flush_output_buffer
  46
+    assert_equal ['foo', original_buffer], body_parts
  47
+    assert_not_equal original_buffer, output_buffer
  48
+    assert_equal Encoding::EUC_JP, output_buffer.encoding
51 49
   end
52 50
 
53 51
   protected
70  actionpack/test/template/render_test.rb
@@ -410,51 +410,49 @@ def teardown
410 410
     GC.start
411 411
   end
412 412
 
413  
-  if '1.9'.respond_to?(:force_encoding)
414  
-    def test_render_utf8_template_with_magic_comment
415  
-      with_external_encoding Encoding::ASCII_8BIT do
416  
-        result = @view.render(:file => "test/utf8_magic", :formats => [:html], :layouts => "layouts/yield")
417  
-        assert_equal Encoding::UTF_8, result.encoding
418  
-        assert_equal "\nРусский \nтекст\n\nUTF-8\nUTF-8\nUTF-8\n", result
419  
-      end
  413
+  def test_render_utf8_template_with_magic_comment
  414
+    with_external_encoding Encoding::ASCII_8BIT do
  415
+      result = @view.render(:file => "test/utf8_magic", :formats => [:html], :layouts => "layouts/yield")
  416
+      assert_equal Encoding::UTF_8, result.encoding
  417
+      assert_equal "\nРусский \nтекст\n\nUTF-8\nUTF-8\nUTF-8\n", result
420 418
     end
  419
+  end
421 420
 
422  
-    def test_render_utf8_template_with_default_external_encoding
423  
-      with_external_encoding Encoding::UTF_8 do
424  
-        result = @view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
425  
-        assert_equal Encoding::UTF_8, result.encoding
426  
-        assert_equal "Русский текст\n\nUTF-8\nUTF-8\nUTF-8\n", result
427  
-      end
  421
+  def test_render_utf8_template_with_default_external_encoding
  422
+    with_external_encoding Encoding::UTF_8 do
  423
+      result = @view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
  424
+      assert_equal Encoding::UTF_8, result.encoding
  425
+      assert_equal "Русский текст\n\nUTF-8\nUTF-8\nUTF-8\n", result
428 426
     end
  427
+  end
429 428
 
430  
-    def test_render_utf8_template_with_incompatible_external_encoding
431  
-      with_external_encoding Encoding::SHIFT_JIS do
432  
-        begin
433  
-          @view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
434  
-          flunk 'Should have raised incompatible encoding error'
435  
-        rescue ActionView::Template::Error => error
436  
-          assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
437  
-        end
  429
+  def test_render_utf8_template_with_incompatible_external_encoding
  430
+    with_external_encoding Encoding::SHIFT_JIS do
  431
+      begin
  432
+        @view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
  433
+        flunk 'Should have raised incompatible encoding error'
  434
+      rescue ActionView::Template::Error => error
  435
+        assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
438 436
       end
439 437
     end
  438
+  end
440 439
 
441  
-    def test_render_utf8_template_with_partial_with_incompatible_encoding
442  
-      with_external_encoding Encoding::SHIFT_JIS do
443  
-        begin
444  
-          @view.render(:file => "test/utf8_magic_with_bare_partial", :formats => [:html], :layouts => "layouts/yield")
445  
-          flunk 'Should have raised incompatible encoding error'
446  
-        rescue ActionView::Template::Error => error
447  
-          assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
448  
-        end
  440
+  def test_render_utf8_template_with_partial_with_incompatible_encoding
  441
+    with_external_encoding Encoding::SHIFT_JIS do
  442
+      begin
  443
+        @view.render(:file => "test/utf8_magic_with_bare_partial", :formats => [:html], :layouts => "layouts/yield")
  444
+        flunk 'Should have raised incompatible encoding error'
  445
+      rescue ActionView::Template::Error => error
  446
+        assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
449 447
       end
450 448
     end
  449
+  end
451 450
 
452  
-    def with_external_encoding(encoding)
453  
-      old = Encoding.default_external
454  
-      silence_warnings { Encoding.default_external = encoding }
455  
-      yield
456  
-    ensure
457  
-      silence_warnings { Encoding.default_external = old }
458  
-    end
  451
+  def with_external_encoding(encoding)
  452
+    old = Encoding.default_external
  453
+    silence_warnings { Encoding.default_external = encoding }
  454
+    yield
  455
+  ensure
  456
+    silence_warnings { Encoding.default_external = old }
459 457
   end
460 458
 end
88  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -224,52 +224,48 @@ def clear_cache!
224 224
         @statements.clear
225 225
       end
226 226
 
227  
-      if "<3".respond_to?(:encode)
228  
-        # Taken from here:
229  
-        #   https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb
230  
-        # Author: TOMITA Masahiro <tommy@tmtm.org>
231  
-        ENCODINGS = {
232  
-          "armscii8" => nil,
233  
-          "ascii"    => Encoding::US_ASCII,
234  
-          "big5"     => Encoding::Big5,
235  
-          "binary"   => Encoding::ASCII_8BIT,
236  
-          "cp1250"   => Encoding::Windows_1250,
237  
-          "cp1251"   => Encoding::Windows_1251,
238  
-          "cp1256"   => Encoding::Windows_1256,
239  
-          "cp1257"   => Encoding::Windows_1257,
240  
-          "cp850"    => Encoding::CP850,
241  
-          "cp852"    => Encoding::CP852,
242  
-          "cp866"    => Encoding::IBM866,
243  
-          "cp932"    => Encoding::Windows_31J,
244  
-          "dec8"     => nil,
245  
-          "eucjpms"  => Encoding::EucJP_ms,
246  
-          "euckr"    => Encoding::EUC_KR,
247  
-          "gb2312"   => Encoding::EUC_CN,
248  
-          "gbk"      => Encoding::GBK,
249  
-          "geostd8"  => nil,
250  
-          "greek"    => Encoding::ISO_8859_7,
251  
-          "hebrew"   => Encoding::ISO_8859_8,
252  
-          "hp8"      => nil,
253  
-          "keybcs2"  => nil,
254  
-          "koi8r"    => Encoding::KOI8_R,
255  
-          "koi8u"    => Encoding::KOI8_U,
256  
-          "latin1"   => Encoding::ISO_8859_1,
257  
-          "latin2"   => Encoding::ISO_8859_2,
258  
-          "latin5"   => Encoding::ISO_8859_9,
259  
-          "latin7"   => Encoding::ISO_8859_13,
260  
-          "macce"    => Encoding::MacCentEuro,
261  
-          "macroman" => Encoding::MacRoman,
262  
-          "sjis"     => Encoding::SHIFT_JIS,
263  
-          "swe7"     => nil,
264  
-          "tis620"   => Encoding::TIS_620,
265  
-          "ucs2"     => Encoding::UTF_16BE,
266  
-          "ujis"     => Encoding::EucJP_ms,
267  
-          "utf8"     => Encoding::UTF_8,
268  
-          "utf8mb4"  => Encoding::UTF_8,
269  
-        }
270  
-      else
271  
-        ENCODINGS = Hash.new { |h,k| h[k] = k }
272  
-      end
  227
+      # Taken from here:
  228
+      #   https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb
  229
+      # Author: TOMITA Masahiro <tommy@tmtm.org>
  230
+      ENCODINGS = {
  231
+        "armscii8" => nil,
  232
+        "ascii"    => Encoding::US_ASCII,
  233
+        "big5"     => Encoding::Big5,
  234
+        "binary"   => Encoding::ASCII_8BIT,
  235
+        "cp1250"   => Encoding::Windows_1250,
  236
+        "cp1251"   => Encoding::Windows_1251,
  237
+        "cp1256"   => Encoding::Windows_1256,
  238
+        "cp1257"   => Encoding::Windows_1257,
  239
+        "cp850"    => Encoding::CP850,
  240
+        "cp852"    => Encoding::CP852,
  241
+        "cp866"    => Encoding::IBM866,
  242
+        "cp932"    => Encoding::Windows_31J,
  243
+        "dec8"     => nil,
  244
+        "eucjpms"  => Encoding::EucJP_ms,
  245
+        "euckr"    => Encoding::EUC_KR,
  246
+        "gb2312"   => Encoding::EUC_CN,
  247
+        "gbk"      => Encoding::GBK,
  248
+        "geostd8"  => nil,
  249
+        "greek"    => Encoding::ISO_8859_7,
  250
+        "hebrew"   => Encoding::ISO_8859_8,
  251
+        "hp8"      => nil,
  252
+        "keybcs2"  => nil,
  253
+        "koi8r"    => Encoding::KOI8_R,
  254
+        "koi8u"    => Encoding::KOI8_U,
  255
+        "latin1"   => Encoding::ISO_8859_1,
  256
+        "latin2"   => Encoding::ISO_8859_2,
  257
+        "latin5"   => Encoding::ISO_8859_9,
  258
+        "latin7"   => Encoding::ISO_8859_13,
  259
+        "macce"    => Encoding::MacCentEuro,
  260
+        "macroman" => Encoding::MacRoman,
  261
+        "sjis"     => Encoding::SHIFT_JIS,
  262
+        "swe7"     => nil,
  263
+        "tis620"   => Encoding::TIS_620,
  264
+        "ucs2"     => Encoding::UTF_16BE,
  265
+        "ujis"     => Encoding::EucJP_ms,
  266
+        "utf8"     => Encoding::UTF_8,
  267
+        "utf8mb4"  => Encoding::UTF_8,
  268
+      }
273 269
 
274 270
       # Get the client encoding for this database
275 271
       def client_encoding
2  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -16,7 +16,7 @@ def string_to_binary(value)
16 16
         end
17 17
 
18 18
         def binary_to_string(value)
19  
-          if value.respond_to?(:force_encoding) && value.encoding != Encoding::ASCII_8BIT
  19
+          if value.encoding != Encoding::ASCII_8BIT
20 20
             value = value.force_encoding(Encoding::ASCII_8BIT)
21 21
           end
22 22
 
16  activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
@@ -17,11 +17,7 @@ def setup
17 17
       end
18 18
 
19 19
       def test_client_encoding
20  
-        if "<3".respond_to?(:encoding)
21  
-          assert_equal Encoding::UTF_8, @conn.client_encoding
22  
-        else
23  
-          assert_equal 'utf8', @conn.client_encoding
24  
-        end
  20
+        assert_equal Encoding::UTF_8, @conn.client_encoding
25 21
       end
26 22
 
27 23
       def test_exec_insert_number
@@ -41,13 +37,11 @@ def test_exec_insert_string
41 37
 
42 38
         value = result.rows.last.last
43 39
 
44  
-        if "<3".respond_to?(:encoding)
45  
-          # FIXME: this should probably be inside the mysql AR adapter?
46  
-          value.force_encoding(@conn.client_encoding)
  40
+        # FIXME: this should probably be inside the mysql AR adapter?
  41
+        value.force_encoding(@conn.client_encoding)
47 42
 
48  
-          # The strings in this file are utf-8, so transcode to utf-8
49  
-          value.encode!(Encoding::UTF_8)
50  
-        end
  43
+        # The strings in this file are utf-8, so transcode to utf-8
  44
+        value.encode!(Encoding::UTF_8)
51 45
 
52 46
         assert_equal str, value
53 47
       end
6  activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
@@ -142,8 +142,6 @@ def test_exec_query_typecasts_bind_vals
142 142
       end
143 143
 
144 144
       def test_quote_binary_column_escapes_it
145  
-        return unless "<3".respond_to?(:encode)
146  
-
147 145
         DualEncoding.connection.execute(<<-eosql)
148 146
           CREATE TABLE dual_encodings (
149 147
             id integer PRIMARY KEY AUTOINCREMENT,
@@ -157,9 +155,7 @@ def test_quote_binary_column_escapes_it
157 155
         assert_equal str, binary.data
158 156
 
159 157
       ensure
160  
-        if "<3".respond_to?(:encode)
161  
-          DualEncoding.connection.drop_table('dual_encodings')
162  
-        end
  158
+        DualEncoding.connection.drop_table('dual_encodings')
163 159
       end
164 160
 
165 161
       def test_execute
6  activerecord/test/cases/binary_test.rb
@@ -12,7 +12,7 @@ class BinaryTest < ActiveRecord::TestCase
12 12
 
13 13
     def test_mixed_encoding
14 14
       str = "\x80"
15  
-      str.force_encoding('ASCII-8BIT') if str.respond_to?(:force_encoding)
  15
+      str.force_encoding('ASCII-8BIT')
16 16
 
17 17
       binary = Binary.new :name => 'いただきます!', :data => str
18 18
       binary.save!
@@ -23,7 +23,7 @@ def test_mixed_encoding
23 23
 
24 24
       # Mysql adapter doesn't properly encode things, so we have to do it
25 25
       if current_adapter?(:MysqlAdapter)
26  
-        name.force_encoding('UTF-8') if name.respond_to?(:force_encoding)
  26
+        name.force_encoding('UTF-8')
27 27
       end
28 28
       assert_equal 'いただきます!', name
29 29
     end
@@ -33,7 +33,7 @@ def test_load_save
33 33
 
34 34
       FIXTURES.each do |filename|
35 35
         data = File.read(ASSETS_ROOT + "/#{filename}")
36  
-        data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding)
  36
+        data.force_encoding('ASCII-8BIT')
37 37
         data.freeze
38 38
 
39 39
         bin = Binary.new(:data => data)
2  activerecord/test/cases/fixtures_test.rb
@@ -213,7 +213,7 @@ def test_subsubdir_file_with_arbitrary_name
213 213
 
214 214
   def test_binary_in_fixtures
215 215
     data = File.open(ASSETS_ROOT + "/flowers.jpg", 'rb') { |f| f.read }
216  
-    data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding)
  216
+    data.force_encoding('ASCII-8BIT')
217 217
     data.freeze
218 218
     assert_equal data, @flowers.data
219 219
   end
112  activesupport/lib/active_support/core_ext/string/access.rb
... ...
@@ -1,99 +1,35 @@
1 1
 require "active_support/multibyte"
2 2
 
3 3
 class String
4  
-  unless '1.9'.respond_to?(:force_encoding)
5  
-    # Returns the character at the +position+ treating the string as an array (where 0 is the first character).
6  
-    #
7  
-    # Examples:
8  
-    #   "hello".at(0)  # => "h"
9  
-    #   "hello".at(4)  # => "o"
10  
-    #   "hello".at(10) # => ERROR if < 1.9, nil in 1.9
11  
-    def at(position)
12  
-      mb_chars[position, 1].to_s
13  
-    end
14  
-
15  
-    # Returns the remaining of the string from the +position+ treating the string as an array (where 0 is the first character).
16  
-    #
17  
-    # Examples:
18  
-    #   "hello".from(0)  # => "hello"
19  
-    #   "hello".from(2)  # => "llo"
20  
-    #   "hello".from(10) # => "" if < 1.9, nil in 1.9
21  
-    def from(position)
22  
-      mb_chars[position..-1].to_s
23  
-    end
24  
-
25  
-    # Returns the beginning of the string up to the +position+ treating the string as an array (where 0 is the first character).
26  
-    #
27  
-    # Examples:
28  
-    #   "hello".to(0)  # => "h"
29  
-    #   "hello".to(2)  # => "hel"
30  
-    #   "hello".to(10) # => "hello"
31  
-    def to(position)
32  
-      mb_chars[0..position].to_s
33  
-    end
34  
-
35  
-    # Returns the first character of the string or the first +limit+ characters.
36  
-    #
37  
-    # Examples:
38  
-    #   "hello".first     # => "h"
39  
-    #   "hello".first(2)  # => "he"
40  
-    #   "hello".first(10) # => "hello"
41  
-    def first(limit = 1)
42  
-      if limit == 0
43  
-        ''
44  
-      elsif limit >= size
45  
-        self
46  
-      else
47  
-        mb_chars[0...limit].to_s
48  
-      end
49  
-    end
50  
-
51  
-    # Returns the last character of the string or the last +limit+ characters.
52  
-    #
53  
-    # Examples:
54  
-    #   "hello".last     # => "o"
55  
-    #   "hello".last(2)  # => "lo"
56  
-    #   "hello".last(10) # => "hello"
57  
-    def last(limit = 1)
58  
-      if limit == 0
59  
-        ''
60  
-      elsif limit >= size
61  
-        self
62  
-      else
63  
-        mb_chars[(-limit)..-1].to_s
64  
-      end
65  
-    end
66  
-  else
67  
-    def at(position)
68  
-      self[position]
69  
-    end
  4
+  def at(position)
  5
+    self[position]
  6
+  end
70 7
 
71  
-    def from(position)
72  
-      self[position..-1]
73  
-    end
  8
+  def from(position)
  9
+    self[position..-1]
  10
+  end
74 11
 
75  
-    def to(position)
76  
-      self[0..position]
77  
-    end
  12
+  def to(position)
  13
+    self[0..position]
  14
+  end
78 15
 
79  
-    def first(limit = 1)
80  
-      if limit == 0
81  
-        ''
82  
-      elsif limit >= size
83  
-        self
84  
-      else
85  
-        to(limit - 1)
86  
-      end
  16
+  def first(limit = 1)
  17
+    if limit == 0
  18
+      ''
  19
+    elsif limit >= size
  20
+      self
  21
+    else
  22
+      to(limit - 1)
87 23
     end
  24
+  end
88 25
 
89  
-    def last(limit = 1)
90  
-      if limit == 0
91  
-        ''
92  
-      elsif limit >= size
93  
-        self
94  
-      else
95  
-        from(-limit)
96  
-      end
  26
+  def last(limit = 1)
  27
+    if limit == 0
  28
+      ''
  29
+    elsif limit >= size
  30
+      self
  31
+    else
  32
+      from(-limit)
97 33
     end
98 34
   end
99 35
 end
8  activesupport/lib/active_support/json/encoding.rb
@@ -119,9 +119,7 @@ def escape_html_entities_in_json=(value)
119 119
         end
120 120
 
121 121
         def escape(string)
122  
-          if string.respond_to?(:force_encoding)
123  
-            string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY)
124  
-          end
  122
+          string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY)
125 123
           json = string.
126 124
             gsub(escape_regex) { |s| ESCAPED_CHARS[s] }.
127 125
             gsub(/([\xC0-\xDF][\x80-\xBF]|
@@ -130,7 +128,7 @@ def escape(string)
130 128
             s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/n, '\\\\u\&')
131 129
           }
132 130
           json = %("#{json}")
133  
-          json.force_encoding(::Encoding::UTF_8) if json.respond_to?(:force_encoding)
  131
+          json.force_encoding(::Encoding::UTF_8)
134 132
           json
135 133
         end
136 134
       end
@@ -281,4 +279,4 @@ def as_json(options = nil) #:nodoc:
281 279
       strftime('%Y/%m/%d %H:%M:%S %z')
282 280
     end
283 281
   end
284  
-end
  282
+end
4  activesupport/lib/active_support/multibyte/chars.rb
@@ -282,9 +282,7 @@ def translate_offset(byte_offset) #:nodoc:
282 282
           return nil if byte_offset.nil?
283 283
           return 0   if @wrapped_string == ''
284 284
 
285  
-          if @wrapped_string.respond_to?(:force_encoding)
286  
-            @wrapped_string = @wrapped_string.dup.force_encoding(Encoding::ASCII_8BIT)
287  
-          end
  285
+          @wrapped_string = @wrapped_string.dup.force_encoding(Encoding::ASCII_8BIT)
288 286
 
289 287
           begin
290 288
             @wrapped_string[0...byte_offset].unpack('U*').length
55  activesupport/lib/active_support/multibyte/utils.rb
@@ -2,36 +2,14 @@
2 2
 
3 3
 module ActiveSupport #:nodoc:
4 4
   module Multibyte #:nodoc:
5  
-    if Kernel.const_defined?(:Encoding)
6  
-      # Returns a regular expression that matches valid characters in the current encoding
7  
-      def self.valid_character
8  
-        VALID_CHARACTER[Encoding.default_external.to_s]
9  
-      end
10  
-    else
11  
-      def self.valid_character
12  
-        case $KCODE
13  
-        when 'UTF8'
14  
-          VALID_CHARACTER['UTF-8']
15  
-        when 'SJIS'
16  
-          VALID_CHARACTER['Shift_JIS']
17  
-        end
18  
-      end
  5
+    # Returns a regular expression that matches valid characters in the current encoding
  6
+    def self.valid_character
  7
+      VALID_CHARACTER[Encoding.default_external.to_s]
19 8
     end
20 9
 
21  
-    if 'string'.respond_to?(:valid_encoding?)
22  
-      # Verifies the encoding of a string
23  
-      def self.verify(string)
24  
-        string.valid_encoding?
25  
-      end
26  
-    else
27  
-      def self.verify(string)
28  
-        if expression = valid_character
29  
-          # Splits the string on character boundaries, which are determined based on $KCODE.
30  
-          string.split(//).all? { |c| expression =~ c }
31  
-        else
32  
-          true
33  
-        end
34  
-      end
  10
+    # Verifies the encoding of a string
  11
+    def self.verify(string)
  12
+      string.valid_encoding?
35 13
     end
36 14
 
37 15
     # Verifies the encoding of the string and raises an exception when it's not valid
@@ -39,22 +17,11 @@ def self.verify!(string)
39 17
       raise EncodingError.new("Found characters with invalid encoding") unless verify(string)
40 18
     end
41 19
 
42  
-    if 'string'.respond_to?(:force_encoding)
43  
-      # Removes all invalid characters from the string.
44  
-      #
45  
-      # Note: this method is a no-op in Ruby 1.9
46  
-      def self.clean(string)
47  
-        string
48  
-      end
49  
-    else
50  
-      def self.clean(string)
51  
-        if expression = valid_character
52  
-          # Splits the string on character boundaries, which are determined based on $KCODE.
53  
-          string.split(//).grep(expression).join
54  
-        else
55  
-          string
56  
-        end
57  
-      end
  20
+    # Removes all invalid characters from the string.
  21
+    #
  22
+    # Note: this method is a no-op in Ruby 1.9
  23
+    def self.clean(string)
  24
+      string
58 25
     end
59 26
   end
60 27
 end
12  activesupport/test/buffered_logger_test.rb
@@ -32,9 +32,7 @@ def test_write_binary_data_to_existing_file
32 32
     logger.level = Logger::DEBUG
33 33
 
34 34
     str = "\x80"
35  
-    if str.respond_to?(:force_encoding)
36  
-      str.force_encoding("ASCII-8BIT")
37  
-    end
  35
+    str.force_encoding("ASCII-8BIT")
38 36
 
39 37
     logger.add Logger::DEBUG, str
40 38
   ensure
@@ -52,9 +50,7 @@ def test_write_binary_data_create_file
52 50
     logger.level = Logger::DEBUG
53 51
 
54 52
     str = "\x80"
55  
-    if str.respond_to?(:force_encoding)
56  
-      str.force_encoding("ASCII-8BIT")
57  
-    end
  53
+    str.force_encoding("ASCII-8BIT")
58 54
 
59 55
     logger.add Logger::DEBUG, str
60 56
   ensure
@@ -124,9 +120,7 @@ def test_buffer_multibyte
124 120
     @logger.info(BYTE_STRING)
125 121
     assert @output.string.include?(UNICODE_STRING)
126 122
     byte_string = @output.string.dup
127  
-    if byte_string.respond_to?(:force_encoding)
128  
-      byte_string.force_encoding("ASCII-8BIT")
129  
-    end
  123
+    byte_string.force_encoding("ASCII-8BIT")
130 124
     assert byte_string.include?(BYTE_STRING)
131 125
   end
132 126
 end
40  activesupport/test/caching_test.rb
@@ -398,22 +398,9 @@ def test_really_long_keys
398 398
 # The error is caused by charcter encodings that can't be compared with ASCII-8BIT regular expressions and by special
399 399
 # characters like the umlaut in UTF-8.
400 400
 module EncodedKeyCacheBehavior
401  
-  if defined?(Encoding)
402  
-    Encoding.list.each do |encoding|
403  
-      define_method "test_#{encoding.name.underscore}_encoded_values" do
404  
-        key = "foo".force_encoding(encoding)
405  
-        assert @cache.write(key, "1", :raw => true)
406  
-        assert_equal "1", @cache.read(key)
407  
-        assert_equal "1", @cache.fetch(key)
408  
-        assert @cache.delete(key)
409  
-        assert_equal "2", @cache.fetch(key, :raw => true) { "2" }
410  
-        assert_equal 3, @cache.increment(key)
411  
-        assert_equal 2, @cache.decrement(key)
412  
-      end
413  
-    end
414  
-
415  
-    def test_common_utf8_values
416  
-      key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8)
  401
+  Encoding.list.each do |encoding|
  402
+    define_method "test_#{encoding.name.underscore}_encoded_values" do
  403
+      key = "foo".force_encoding(encoding)
417 404
       assert @cache.write(key, "1", :raw => true)
418 405
       assert_equal "1", @cache.read(key)
419 406
       assert_equal "1", @cache.fetch(key)
@@ -422,12 +409,23 @@ def test_common_utf8_values
422 409
       assert_equal 3, @cache.increment(key)
423 410
       assert_equal 2, @cache.decrement(key)
424 411
     end
  412
+  end
425 413
 
426  
-    def test_retains_encoding
427  
-      key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8)
428  
-      assert @cache.write(key, "1", :raw => true)
429  
-      assert_equal Encoding::UTF_8, key.encoding
430  
-    end
  414
+  def test_common_utf8_values
  415
+    key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8)
  416
+    assert @cache.write(key, "1", :raw => true)
  417
+    assert_equal "1", @cache.read(key)
  418
+    assert_equal "1", @cache.fetch(key)
  419
+    assert @cache.delete(key)
  420
+    assert_equal "2", @cache.fetch(key, :raw => true) { "2" }
  421
+    assert_equal 3, @cache.increment(key)
  422
+    assert_equal 2, @cache.decrement(key)
  423
+  end
  424
+
  425
+  def test_retains_encoding
  426
+    key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8)
  427
+    assert @cache.write(key, "1", :raw => true)
  428
+    assert_equal Encoding::UTF_8, key.encoding
431 429
   end
432 430
 end
433 431
 
4  activesupport/test/json/encoding_test.rb
@@ -91,11 +91,11 @@ def test_hash_encoding
91 91
   def test_utf8_string_encoded_properly
92 92
     result = ActiveSupport::JSON.encode('€2.99')
93 93
     assert_equal '"\\u20ac2.99"', result
94  
-    assert_equal(Encoding::UTF_8, result.encoding) if result.respond_to?(:encoding)
  94
+    assert_equal(Encoding::UTF_8, result.encoding)
95 95
 
96 96
     result = ActiveSupport::JSON.encode('✎☺')
97 97
     assert_equal '"\\u270e\\u263a"', result
98  
-    assert_equal(Encoding::UTF_8, result.encoding) if result.respond_to?(:encoding)
  98
+    assert_equal(Encoding::UTF_8, result.encoding)
99 99
   end
100 100
 
101 101
   def test_non_utf8_string_transcodes
5  activesupport/test/multibyte_test_helpers.rb
@@ -3,10 +3,7 @@
3 3
 module MultibyteTestHelpers
4 4
   UNICODE_STRING = 'こにちわ'
5 5
   ASCII_STRING = 'ohayo'
6  
-  BYTE_STRING = "\270\236\010\210\245"
7  
-  if BYTE_STRING.respond_to?(:force_encoding)
8  
-    BYTE_STRING.force_encoding("ASCII-8BIT")
9  
-  end
  6
+  BYTE_STRING = "\270\236\010\210\245".force_encoding("ASCII-8BIT")
10 7
 
11 8
   def chars(str)
12 9
     ActiveSupport::Multibyte::Chars.new(str)
2  railties/test/application/initializers/frameworks_test.rb
@@ -137,7 +137,7 @@ def from_bar_helper
137 137
     end
138 138
 
139 139
     test "assignment config.encoding to default_charset" do
140  
-      charset = "ruby".respond_to?(:force_encoding) ? 'Shift_JIS' : 'UTF8'
  140
+      charset = 'Shift_JIS'
141 141
       add_to_config "config.encoding = '#{charset}'"
142 142
       require "#{app_path}/config/environment"
143 143
       assert_equal charset, ActionDispatch::Response.default_charset

1 note on commit 1e9e88f

42he

Reintroduced Bug #5179
https://rails.lighthouseapp.com/projects/8994/tickets/5179-link_to_function-and-fields_for-inside-block-empty-array-in-with_output_buffer-error

in actionpack/lib/action_view/helpers/capture_helper.rb

buf can be [] and then no encoding.

Tested on 1.9.3

Please sign in to comment.
Something went wrong with that request. Please try again.