Iterate binary string and sqlite3 query

Hi guys, my problem is the following,

Right now I have a list of japanese words that are loaded from the
database, they display okay, the problem begins when I need to split
that word into ‘characters’ in this case ‘kanjis’ so I can make a link
for each kanji,for example:


I have on my view:
<%= word.kanji %> which display 何曜日

But what I need is something like:
何曜日


So I decided to go one step at a time, first split the thing… but
using each_char didn’t work because it seems that the string ‘何曜日’ is
being manipulated as ‘\xE4\xBD\x95\xE6\x9B\x9C\xE6\x97\xA5’ which is
ASCII-8BIT.

Then I tried going 3 bytes a time which allowed me to split it by kanji,
it was something like this


parts = []
(word.size/3).times{ |i| parts << word[i*3,3] }


Now “parts” has each kanji stored and when I try to make a query with
it, something like:
k = Kanji.find(:first, :conditions => “kanji = ‘#{parts[0]}’”)
everything seems ok, I even get:
k.class => Kanji
k.methods => (lots including :id)
k.nil? => false

but when I try:
k.id
I get:
undefined method `id’ for nil:NilClass

So I’m really confused, I’m pretty noob at both ruby and rails so Idk if
I’m doing something really stupid or the wrong way, I’ve tried doing
encode, toutf8 and stuff like that but it still gets me nowhere, I’m
worried because with this I may not be able to do a search feature by
kanji.

Thanks in advance.

okay lol forget about the “nil” problem I mentioned at the end, that was
because out of the huge list of words there were a few that started with
a symbol other than a kanji so just doing:

“k.id unless k.nil?” did the trick

so I’m going to change my question to… is there another(simpler) way
to do this? right now my helper looks like crap, I mean… like this:


def word_to_links(word)
parts, o = [], “”
(word.size/3).times{ |i| parts << word[i*3,3] }
parts.each do |part|
k = Kanji.find(:first, :conditions => “kanjis.kanji = ‘#{part}’”)
o << (k.nil? ? part : link_to(part, “/kanjis/#{k.id}”))
end
o
end


Thanks.

On Jun 3, 4:37 am, Japmsn J. [email protected] wrote:

def word_to_links(word)
parts, o = [], “”
(word.size/3).times{ |i| parts << word[i*3,3] }

I wouldn’t rely on some or most kanjis being exactly 3 bytes in utf8 -
use the multibyte chars stuff in activesupport

Fred

Frederick C. wrote:

On Jun 3, 4:37�am, Japmsn J. [email protected] wrote:

� def word_to_links(word)
� � parts, o = [], “”
� � (word.size/3).times{ |i| parts << word[i*3,3] }

I wouldn’t rely on some or most kanjis being exactly 3 bytes in utf8 -
use the multibyte chars stuff in activesupport

Fred

Thanks I’ll look into that