Skip to content

Commit

Permalink
Merge pull request #24 from artofhuman/fix-check-string-class
Browse files Browse the repository at this point in the history
fix: check String class from std lib
  • Loading branch information
artofhuman authored Apr 7, 2017
2 parents f82cebd + b0ef302 commit 5c8c949
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/string_tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ module CharDet
# Возвращает true если строка содержит допустимую
# последовательность байтов для кодировки utf8 и false в обратном случае
# см. http://en.wikipedia.org/wiki/UTF-8
def valid_utf8? string
case string
when String then string.is_utf8?
else false
end
def valid_utf8?(string)
string.respond_to?(:is_utf8?) && string.is_utf8?
end

# shorthand
Expand Down Expand Up @@ -66,7 +63,7 @@ def truncate_words(text, length = 75)
module ActionControllerExtension
def accepts_non_utf8_params(*args)
args.each do |arg|
next unless arg.is_a?(Symbol) || arg.is_a?(String)
next unless arg.is_a?(Symbol) || arg.is_a?(::String)
arg = arg.to_sym

class_eval do
Expand Down
5 changes: 5 additions & 0 deletions spec/string_tools_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,9 @@
end
end
end

describe '#valid_utf8?' do
it { expect(StringTools.valid_utf8?('foobar')).to be true }
it { expect(StringTools.valid_utf8?(nil)).to be false }
end
end

0 comments on commit 5c8c949

Please sign in to comment.