Newbie Question (gets.chomp)

G

Guest

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
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Sun, 1 Jul 2007 12:32:37 +0900
Von: Guest <[email protected]>
An: (e-mail address removed)
Betreff: Newbie Question (gets.chomp)
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.

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/classes/Iconv.html

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top