Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/corefoundation/extensions.rb

Overview

Ruby String class

Instance Method Summary collapse

Instance Method Details

#binary!(bin = true) ⇒ Object

Note:

There is no advantage to using this over the standard encoding methods unless you wish to retain 1.8.7 compatibility

On ruby 1.8.7 sets or clears the flag used by #binary?. On ruby 1.9 the string’s encoding is forced.

Parameters:

  • bin (optional, Boolean, Encoding) (defaults to: true)

    On ruby 1.8.7 only boolean values are admissible. On ruby 1.9 you can pass a specific encoding to force. If you pass ‘true` then `Encoding::ASCII_BIT` is used, if you pass `false` then `Encoding::UTF_8`

See Also:



100
101
102
103
104
105
106
107
108
# File 'lib/corefoundation/extensions.rb', line 100

def binary!(bin=true)
  if bin == true
    self.force_encoding Encoding::ASCII_8BIT
  else
    # default to utf-8
    self.force_encoding( (bin == false) ? "UTF-8" : bin)
  end
  self
end

#binary?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/corefoundation/extensions.rb', line 72

def binary?
  encoding == Encoding::ASCII_8BIT
end

#to_cfCF::String, CF::Data

Returns a CF::String or CF::Data representing the string. If #binary? returns true a CF::Data is returned, if not a CF::String is returned

If you want a CF::Data with the contents of a non binary string, use #to_cf_data

Returns:



54
55
56
# File 'lib/corefoundation/extensions.rb', line 54

def to_cf
  self.binary? ? self.to_cf_data : self.to_cf_string
end

#to_cf_dataCF::Data

Returns a CF::Data representing the string

Returns:



132
133
134
# File 'lib/corefoundation/extensions.rb', line 132

def to_cf_data
  CF::Data.from_string self
end

#to_cf_stringCF::String

Returns a CF::String representing the string

Returns:



126
127
128
# File 'lib/corefoundation/extensions.rb', line 126

def to_cf_string
  CF::String.from_string self
end