[Ruby] Как правильно в руби перекодировать текст

vserge vserge at altlinux.ru
Wed Dec 4 10:38:33 MSK 2002


Приветствую

Вопрос как правильно в руби перекодировать текст из UTF8 в любую другую?

Я собрал пока в тестовой варианте ruby-iconv-0.5
вроде работает, но есть два скрипта (приведены ниже), но первый скрипт
работает, т.е. перекодирует строки, а второй нет. Где я ошибаюсь?

scritp1:
-----------------------------------------------------------
require 'iconv'

cd = Iconv.new('UTF-8', 'KOI8-R')

cd.class
begin
  print("Введите то что хотите перевести из KOI8-R в UTF-8:")
  input = gets
  print("Вот оригинал: #{input}\n")
  print("Вот перевод: #{cd.iconv(input)}\n")
end
------------------------------------------------------------

script2:
------------------------------------------------------------
require 'ldap'
require 'iconv'

ldap_host = 'localhost'
ldap_port = LDAP::LDAP_PORT
ldap_base_dn = 'dc=mydomain,dc=com'
ldap_filter  = 'uid=*'
ldap_attrs   = ''
if( ! ldap_attrs[0] )
    ldap_attrs = nil
end
max_entries  = 50


def print_entry(entry)  
  convert = Iconv.new('UTF-8', 'KOI8-R')
  print("dn: #{entry.dn} \n")
  entry.attrs.each{|attr|
    entry.vals(attr).each{|val|
      print(" #{attr}: #{convert.iconv(val)} \n")
    }
  }
end  

ldap_conn = LDAP::Conn.new(ldap_host, ldap_port.to_i)
ldap_conn.bind{
  i = 0
  ldap_conn.search(ldap_base_dn, LDAP::LDAP_SCOPE_SUBTREE,
 		   ldap_filter, ldap_attrs){|entry|
	i += 1
	if( i > max_entries )
	  raise(RuntimeError,"too many entries are found.\n")
	end
	print_entry(entry)
	}
  }
--------------------------------------------------------------

-- 
With best wishes, Volkov Serge		
Network Administrator/Security Administrator 		



More information about the Ruby mailing list