Method: Object#encode

Defined in:
lib/mspec/helpers/encode.rb

#encode(str, encoding) ⇒ Object

Helper to handle String encodings. The str and encoding parameters must be Strings and an ArgumentError will be raised if not. This ensures that the encode() helper can be used regardless of whether Encoding exits. The helper is a no-op (i.e. passes through str unmodified) if the :encoding feature is not enabled (see with_feature guard). If the :encoding feature is enabled, str.force_encoding(encoding) is called.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mspec/helpers/encode.rb', line 10

def encode(str, encoding)
  unless str.is_a? String and encoding.is_a? String
    raise ArgumentError, "encoding name must be a String"
  end

  if FeatureGuard.enabled? :encoding
    str.force_encoding encoding
  end

  str
end