Permalink
Browse files

Applying styles leaves the original String unchanged. Fixes #9

  • Loading branch information...
1 parent 66af360 commit 8150ee3fad918ce7a684349ffc9e5adf2e385b1c @fcoury fcoury committed Dec 5, 2011
Showing with 11 additions and 4 deletions.
  1. +5 −4 lib/rainbow.rb
  2. +6 −0 test/rainbow_test.rb
View
@@ -86,10 +86,11 @@ def hide
def wrap_with_code(code) #:nodoc:
return self unless Sickill::Rainbow.enabled
- matched = match(/^(\e\[([\d;]+)m)*/)
- insert(matched.end(0), "\e[#{code}m")
- concat("\e[0m") unless self =~ /\e\[0m$/
- self
+ self.clone.tap do |var|
+ matched = var.match(/^(\e\[([\d;]+)m)*/)
+ var.insert(matched.end(0), "\e[#{code}m")
+ var.concat("\e[0m") unless var =~ /\e\[0m$/
+ end
end
end
View
@@ -106,6 +106,12 @@ def test_hide
assert_equal "\e[8mhello\e[0m", "hello".hide
end
+ def test_immutability
+ string = "hello"
+ string.color(:red)
+ assert_equal string, "hello"
+ end
+
class MyString < String
end

0 comments on commit 8150ee3

Please sign in to comment.