cheat sheets.

$ cheat iconv
# Iconv for Ruby
# -> Character Encoding Conversion Library

require 'iconv'

Iconv class methods generally take two or more parameters
   with the first two being strings containing:
   -> a target encoding name
   -> a source encoding name

A few character set name strings:
  - 'ascii'   - 'utf-16'
  - 'utf-8'   - 'latin1'
  ( blah de blah blah blah ...)
  --> try `iconv -l' on a command line for a full list

quick, one-time conversion of several strings:
  Iconv.iconv(to, from, *strings) # => [converted, strings, ...] or
  Iconv.conv(to, from, *strings)  # same as Iconv.iconv(to, from, *strings).join
                                  # => "converted strings joined togeter"

creating a reusable, conversion object that is
automatically closed (in the style of File.open):
  Iconv.open(to, from) do |c|   # c is a Iconv conversion object
    ...                         # the block value is returned 
  end                           # c is closed after the block exits

Example: 
  Iconv.open('utf-16', 'ascii') do |c|
    c.iconv("\377\376n\000o\000n\000s\000e\000n\000s\000e\000")
    # => "nonsense"
  end

create a plain old object you close explicitly somewhere later on:
  converter = Iconv.new(to, from)
  
Example:
  c = Iconv.new('utf-8', 'latin1')
  c.iconv("\253 frenchy \273")  # => "« frenchy »"
  ...
  c.close
Version 1, updated 1047 days ago.
. o 0 ( edit | history )
( add new | see all )