Sha256: 8e539af20cfeed352f7fec91495957e168725c37cf267b9c31615ee16059f59b

Contents?: true

Size: 1.53 KB

Versions: 20

Compression:

Stored size: 1.53 KB

Contents

= ANSI::String

The ANSI::String class is a very sophisticated implementation
of Ruby's standard String class, but one that can handle
ANSI codes seamlessly.

  require 'ansi/string'

  flower1 = ANSI::String.new("Roses")
  flower2 = ANSI::String.new("Violets")

Like any other string.

  flower1.to_s.assert == "Roses"
  flower2.to_s.assert == "Violets"

Bet now we can add color.

  flower1.red!
  flower2.blue!

  flower1.to_s.assert == "\e[31mRoses\e[0m"
  flower2.to_s.assert == "\e[34mViolets\e[0m"

Despite that the string representation now contains ANSI codes,
we can still manipulate the string in much the same way that
we manipulate an ordinary string.

  flower1.size.assert == 5
  flower2.size.assert == 7

Like ordinary strings we can concatenate the two strings

  flowers = flower1 + ' ' + flower2
  flowers.to_s.assert == "\e[31mRoses\e[0m \e[34mViolets\e[0m"

  flowers.size.assert == 13

Standard case conversion such as #upcase and #downcase work.

  flower1.upcase.to_s.assert == "\e[31mROSES\e[0m"
  flower1.downcase.to_s.assert == "\e[31mroses\e[0m"

Some of the most difficult methods to re-implement were the 
substitution methods such as #sub and #gsub. They are still
somewhat more limited than the original string methods, but
their primary functionality should work.

  flower1.gsub('s', 'z').to_s.assert == "\e[31mRozez\e[0m"

There are still a number of methods that need implementation.
ANSI::String is currently a very partial implementation. But
as you can see from the methods it does currently support,
is it already useful.


Version data entries

20 entries across 20 versions & 5 rubygems

Version Path
arcabouco-0.2.13 vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
challah-1.0.0 vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
swipe-rails-0.0.5 vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
challah-1.0.0.beta3 vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
challah-1.0.0.beta2 vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
challah-1.0.0.beta vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
challah-0.9.1.beta.3 vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
challah-0.9.1.beta vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
challah-0.9.0 vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
challah-0.8.3 vendor/bundle/gems/ansi-1.4.3/demo/06_string.rdoc
ansi-1.4.3 demo/06_string.rdoc
challah-0.6.1 vendor/bundle/gems/ansi-1.4.2/qed/06_string.rdoc
challah-0.6.0 vendor/bundle/gems/ansi-1.4.2/qed/06_string.rdoc
ansi-1.4.2 qed/06_string.rdoc
ansi-1.4.1 qed/06_string.rdoc
ansi-1.4.0 qed/06_string.rdoc
ansi-1.3.0 qed/06_string.rdoc
ansi-1.2.5 qed/06_string.rdoc
ansi-1.2.4 qed/06_string.rdoc