# Iterate binary string and sqlite3 query

**URL:** <https://www.ruby-forum.com/t/iterate-binary-string-and-sqlite3-query/189560>\
**Category:** Rails\
**Created:** [June 3, 2010, 5:14am UTC](https://www.ruby-forum.com/t/iterate-binary-string-and-sqlite3-query/189560 "2010-06-03T05:14:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![japmsn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cdc98d/32.png) [@japmsn](https://www.ruby-forum.com/u/japmsn)\
**Post date:** [June 3, 2010, 5:14am UTC](https://www.ruby-forum.com/t/iterate-binary-string-and-sqlite3-query/189560/1 "2010-06-03T05:14:00Z")

</div>

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:  
[ä½•](https://www.ruby-forum.com/kanjis/12)[æ›œ](https://www.ruby-forum.com/kanjis/55)[æ—¥](https://www.ruby-forum.com/kanjis/39)

* * *

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.

---

<div class="post-metadata">

**Author:** ![japmsn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cdc98d/32.png) [@japmsn](https://www.ruby-forum.com/u/japmsn)\
**Post date:** [June 3, 2010, 5:37am UTC](https://www.ruby-forum.com/t/iterate-binary-string-and-sqlite3-query/189560/2 "2010-06-03T05:37:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![japmsn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cdc98d/32.png) [@japmsn](https://www.ruby-forum.com/u/japmsn)\
**Post date:** [June 3, 2010, 9:51am UTC](https://www.ruby-forum.com/t/iterate-binary-string-and-sqlite3-query/189560/3 "2010-06-03T09:51:22Z")

</div>

On Jun 3, 4:37 am, Japmsn J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![japmsn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cdc98d/32.png) [@japmsn](https://www.ruby-forum.com/u/japmsn)\
**Post date:** [June 3, 2010, 2:18pm UTC](https://www.ruby-forum.com/t/iterate-binary-string-and-sqlite3-query/189560/4 "2010-06-03T14:18:28Z")

</div>

Frederick C. wrote:

> On Jun 3, 4:37ï¿½am, Japmsn J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
