instance method slice

Ruby on Rails 3.2.22.5

Since v2.2.3 Last seen in v3.2.22.5

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5

Signature

slice(*args)

Implements Unicode-aware slice with codepoints. Slicing on one point returns the codepoints for that character.

Example:

'こんにちは'.mb_chars.slice(2..3).to_s # => "にち"

Parameters

args rest
Source
# File activesupport/lib/active_support/multibyte/chars.rb, line 307
      def slice(*args)
        if args.size > 2
          raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" # Do as if we were native
        elsif (args.size == 2 && !(args.first.is_a?(Numeric) || args.first.is_a?(Regexp)))
          raise TypeError, "cannot convert #{args.first.class} into Integer" # Do as if we were native
        elsif (args.size == 2 && !args[1].is_a?(Numeric))
          raise TypeError, "cannot convert #{args[1].class} into Integer" # Do as if we were native
        elsif args[0].kind_of? Range
          cps = Unicode.u_unpack(@wrapped_string).slice(*args)
          result = cps.nil? ? nil : cps.pack('U*')
        elsif args[0].kind_of? Regexp
          result = @wrapped_string.slice(*args)
        elsif args.size == 1 && args[0].kind_of?(Numeric)
          character = Unicode.u_unpack(@wrapped_string)[args[0]]
          result = character && [character].pack('U')
        else
          cps = Unicode.u_unpack(@wrapped_string).slice(*args)
          result = cps && cps.pack('U*')
        end
        result && chars(result)
      end

Defined in activesupport/lib/active_support/multibyte/chars.rb line 307 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::Multibyte::Chars

Type at least 2 characters to search.

↑↓ navigate · open · esc close