Forum: Ruby Newbie Question (gets.chomp)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8dba11fb458de3101593e92c2a873a78?d=identicon&s=25 Guest (Guest)
on 2007-07-01 05:32
Hiya

just hoping someone can shed some light on this.  If i were to prompt
the user to enter a search parameter to look across a bigger string of
text using:

print "please enter search term: "
stdout.flush
string1 = gets.chomp

i run into trouble when the user enters non english search terms.  how
can i set this up to allow the user to enter other language words?
chinese/russian/etc.

thanks, seems like a simple question... but i couldn't find anything on
it in the forums
D0338c0de4cb3c5c17300396159933d1?d=identicon&s=25 Axel Etzold (Guest)
on 2007-07-01 11:47
(Received via mailing list)
-------- Original-Nachricht --------
Datum: Sun, 1 Jul 2007 12:32:37 +0900
Von: Guest <Guest@bugmenot.com>
An: ruby-talk@ruby-lang.org
Betreff: Newbie Question (gets.chomp)

> i run into trouble when the user enters non english search terms.  how
> can i set this up to allow the user to enter other language words?
> chinese/russian/etc.
>

Hello,

this is due to different encodings used in the console and in the text
document you're searching in.
You can tell a Ruby script to use Unicode encoding by setting

$KCODE='u'

or use Iconv

http://www.ruby-doc.org/stdlib/libdoc/iconv/rdoc/c...

to convert between different encodings,e.g.,
if you need to convert from Western European ISO-8859-1 to
UTF-8:

require "Iconv"
include Iconv

puts "What do you want to search for?"
search_what=gets.chomp
search_what=Iconv.new('UTF-8', 'ISO-8859-1').iconv(search_what)

# now, search for search_what (in UTF-8 encoding ) in a text
# (which is also in UTF-8 encoding ) ...


Best regards,

Axel
This topic is locked and can not be replied to.