Sha256: 71aa1d7595e195247aa69aaf7a1bdd2c0183f604aab504cc70306de110adec00

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

class String

  # Split a string into an array according to ath egiven mode.
  # The mode is a recognized symbol or a regexp. If no mode is
  # given the default mode :char (same as regular expression //)
  # is used.
  #
  # Symbolized modes include :byte, :char, :word, :line and :enum.
  #
  # The :enum mode passes the call up to the Enumerable#to_a, thus
  # providing a means to Ruby's current built-in behavior.
  #
  #   'abc 123'.to_arr        #=> ['a','b','c',' ','1','2','3']
  #   'abc 123'.to_arr(:word) #=> ['abc','123']
  #
  def to_arr(mode=nil)
    case mode
    when nil, :char, :chars
      split(//)
    when :enum
      super
    when :byte, :bytes
      unpack('C*')
    when :word, :words
      split(/[ ]+/)
    when :line, :lines
      split(/\n/)
    else
      split(mode)
    end
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_to_arr
      assert_equal( ['a','b','c'], 'abc'.to_arr )
    end

  end

=end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
facets-1.0.3 packages/core/lib/facet/string/to_arr.rb
facets-0.9.0 lib/nano/string/to_arr.rb
facets-1.0.0 lib/facet/string/to_arr.rb
facets-1.3.0 lib/facets/core/string/to_arr.rb
facets-1.1.0 lib/facet/string/to_arr.rb
facets-1.2.0 lib/facets/core/string/to_arr.rb
facets-1.2.1 lib/facets/core/string/to_arr.rb