Skip to content

Instantly share code, notes, and snippets.

@bopm
Created November 25, 2010 11:25
Show Gist options
  • Save bopm/715235 to your computer and use it in GitHub Desktop.
Save bopm/715235 to your computer and use it in GitHub Desktop.
example of gem using script
#!/usr/local/bin/ruby
require "rubygems"
require "activerecord"
require "activesupport"
require 'iconv'
ActiveRecord::Base.establish_connection(:adapter => "postgresql",
:database => "database", :username => "user", :password => "password",
:host => "db", :port => 9999, :schema_search_path => "migration, public")
def clean_utf str
str = str.chars.normalize(:c)
begin
result = Iconv.new("CP1251", "UTF-8").iconv(str)
rescue Iconv::IllegalSequence => e
failed = e.failed.chars.split(//, 2)
str = str.gsub(failed[0], '')
retry
end
str.chars.normalize(:c)
end
class BookSource < ActiveRecord::Base
set_table_name "book_xls"
set_primary_key "book_xls_id"
end
$KCODE = 'u'
puts BookSource.find_by_book_xls_id(15638).lib_annot
puts BookSource.find_by_book_xls_id(15638).lib_annot.chars.length
puts clean_utf BookSource.find_by_book_xls_id(15638).lib_annot
puts (clean_utf BookSource.find_by_book_xls_id(15638).lib_annot).chars.length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment