[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Unicode strings in ruby code

Idan Miller

2/27/2008 8:32:00 AM

Hi everyone,

I'm trying to get ruby to run a script that has a hebrew string.
I'm using a string with just one letter - aleph, the first in the
hebrew alphabet.

This code:

a = "?"

that I write using SciTE and saved as UTF-8 gives these errors:
C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\357'
in expression
C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\273'
in expression
C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\277'
in expression

Now, if I save this as ANSI in SciTE it allows it, and then it runs.
However, if I use eclipse ruby plugin, it won't allow me to save aleph
as ANSI (rightfully).

Also, if I read aleph from a file everything works fine when the ruby
script is UTF-8.

What is wrong here?
Can ruby just not get strings in the code that aren't ANSI?

Thanks,
Idan Miller.
11 Answers

7stud --

2/27/2008 12:46:00 PM

0

Idan Miller wrote:
> Hi everyone,
>
> I'm trying to get ruby to run a script that has a hebrew string.
> I'm using a string with just one letter - aleph, the first in the
> hebrew alphabet.
>
> This code:
>
> a = "א"
>
> that I write using SciTE and saved as UTF-8 gives these errors:
> C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\357'
> in expression
> C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\273'
> in expression
> C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\277'
> in expression
>
> Now, if I save this as ANSI in SciTE it allows it, and then it runs.
> However, if I use eclipse ruby plugin, it won't allow me to save aleph
> as ANSI (rightfully).
>
> Also, if I read aleph from a file everything works fine when the ruby
> script is UTF-8.
>
> What is wrong here?
> Can ruby just not get strings in the code that aren't ANSI?
>
> Thanks,
> Idan Miller.

You can always do this:

a = "\xD7\x90"
puts a


If what you are trying to do is avoid having to type in utf chars in
hexidecimal notation, then try putting this at the top of your file:

$KCODE = "UTF-8"

and see if that allows you to enter the actual character with your
editor.
--
Posted via http://www.ruby-....

Idan Miller

2/27/2008 2:00:00 PM

0

Hi,

I am trying to avoid the hexidecimal notation since it is obviously
hard to do and not readable...
The KCODE statement doesn't help.

Idan.

On 27 ??????, 14:46, 7stud -- <bbxx789_0...@yahoo.com> wrote:
> Idan Miller wrote:
> > Hi everyone,
>
> > I'm trying to get ruby to run a script that has a hebrew string.
> > I'm using a string with just one letter - aleph, the first in the
> > hebrew alphabet.
>
> > This code:
>
> > a = "?"
>
> > that I write using SciTE and saved as UTF-8 gives these errors:
> > C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\357'
> > in expression
> > C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\273'
> > in expression
> > C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\277'
> > in expression
>
> > Now, if I save this as ANSI in SciTE it allows it, and then it runs.
> > However, if I use eclipse ruby plugin, it won't allow me to save aleph
> > as ANSI (rightfully).
>
> > Also, if I read aleph from a file everything works fine when the ruby
> > script is UTF-8.
>
> > What is wrong here?
> > Can ruby just not get strings in the code that aren't ANSI?
>
> > Thanks,
> > Idan Miller.
>
> You can always do this:
>
> a = "\xD7\x90"
> puts a
>
> If what you are trying to do is avoid having to type in utf chars in
> hexidecimal notation, then try putting this at the top of your file:
>
> $KCODE = "UTF-8"
>
> and see if that allows you to enter the actual character with your
> editor.
> --
> Posted viahttp://www.ruby-forum.... ???? ?????-
>
> -???? ???? ?????-

Arlen Cuss

2/27/2008 2:13:00 PM

0

SG0uIEl0IHdvcmtzIGZvciBtZToKCmNlbHRpY0Bzb2htYTp+JCBmaWxlIGhlYnJldy5yYgpoZWJy
ZXcucmI6IFVURi04IFVuaWNvZGUgdGV4dApjZWx0aWNAc29obWE6fiQgY2F0IGhlYnJldy5yYgph
ID0gIteQIgpwdXRzIGEKCmNlbHRpY0Bzb2htYTp+JCBydWJ5IGhlYnJldy5yYgrXkApjZWx0aWNA
c29obWE6fiQgcnVieSAtdgpydWJ5IDEuOC42ICgyMDA3LTA2LTA3IHBhdGNobGV2ZWwgMzYpIFtp
NDg2LWxpbnV4XQpjZWx0aWNAc29obWE6fiQKClNvIEkgY2FuJ3QgcmVjcmVhdGUgeW91ciBwcm9i
bGVtIC0gSSB3b25kZXIgaWYgaXQncyB0aGUgV2luZG93cwpkaXN0cmlidXRpb24uCgpBcmxlbgo=

Idan Miller

2/27/2008 2:28:00 PM

0

It must be a windows issue...
Which ruby do you run?
I'm running 1.8.6

On 27 ??????, 16:12, Arlen Cuss <cel...@sairyx..org> wrote:
> Hm. It works for me:
>
> celtic@sohma:~$ file hebrew.rb
> hebrew.rb: UTF-8 Unicode text
> celtic@sohma:~$ cat hebrew.rb
> a = "?"
> puts a
>
> celtic@sohma:~$ ruby hebrew.rb
> ?
> celtic@sohma:~$ ruby -v
> ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]
> celtic@sohma:~$
>
> So I can't recreate your problem - I wonder if it's the Windows
> distribution.
>
> Arlen

Idan Miller

2/27/2008 3:03:00 PM

0

*feels stupid*
I see you're running 1.8.6 as well
How can I move this issue forward?

On 27 ??????, 16:27, Idan Miller <Idan.Mil...@gmail.com> wrote:
> It must be a windows issue...
> Which ruby do you run?
> I'm running 1.8.6
>
> On 27 ??????, 16:12, Arlen Cuss <cel...@sairyx.org> wrote:
>
>
>
> > Hm. It works for me:
>
> > celtic@sohma:~$ file hebrew.rb
> > hebrew.rb: UTF-8 Unicode text
> > celtic@sohma:~$ cat hebrew.rb
> > a = "?"
> > puts a
>
> > celtic@sohma:~$ ruby hebrew.rb
> > ?
> > celtic@sohma:~$ ruby -v
> > ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]
> > celtic@sohma:~$
>
> > So I can't recreate your problem - I wonder if it's the Windows
> > distribution.
>
> > Arlen-???? ???? ?????-
>
> -???? ???? ?????-

Alex Fenton

2/27/2008 3:36:00 PM

0

Idan Miller wrote:

> This code:
>
> a = "א"
>
> that I write using SciTE and saved as UTF-8 gives these errors:
> C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\357'
> in expression
> C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\273'
> in expression
> C:/Documents and Settings/idan/Desktop/test.rb:1: Invalid char `\277'
> in expression
>
> Now, if I save this as ANSI in SciTE it allows it, and then it runs.

The problem is partly because you're using SCITE. It's saves the "Byte
Order Mark" or BOM at the beginning of UTF-8 files. Ruby fails to run
scripts with a BOM at the beginning.

The solution is to find a setting to save UTF8 without BOM. I don't know
how it's done in SCITE, but any decent code editor should be able to do
this. If, not find another.

As others have said, you should puts $KCODE='u' or run ruby with -Ku if
your script is encoded in UTF8.

alex

Tadashi Saito

2/27/2008 4:05:00 PM

0

Hi,

On Wed, 27 Feb 2008 06:00:22 -0800 (PST)
Idan Miller <Idan.Miller@gmail.com> wrote:

> Hi,
>
> I am trying to avoid the hexidecimal notation since it is obviously
> hard to do and not readable...
> The KCODE statement doesn't help.

-Ku option would.

--
?????

Idan Miller

2/28/2008 10:01:00 AM

0

When I run with -KU I'm expected to have a certian method missing in
Japanese:

Desktop/test.rb:1: undefined local variable or method `n++' for
main:Object (NameError)


On 27 ??????, 18:04, Tadashi Saito <sh...@mail2.accsnet.ne.jp> wrote:
> Hi,
>
> On Wed, 27 Feb 2008 06:00:22 -0800 (PST)
>
> Idan Miller <Idan.Mil...@gmail.com> wrote:
> > Hi,
>
> > I am trying to avoid the hexidecimal notation since it is obviously
> > hard to do and not readable...
> > The KCODE statement doesn't help.
>
> -Ku option would.
>
> --
> ?????

Justin Collins

2/28/2008 7:21:00 PM

0

Idan Miller wrote:
> When I run with -KU I'm expected to have a certian method missing in
> Japanese:
>
> Desktop/test.rb:1: undefined local variable or method `â?©â??â?' for
> main:Object (NameError)
>

I think you might still be having issues with how your editor is saving
the file. Try creating a fresh test file with something like Notepad and
see if you have the same problems.

-Justin

Wolfgang Nádasi-donner

2/29/2008 9:26:00 PM

0

Justin Collins wrote:
> Idan Miller wrote:
>> Desktop/test.rb:1: undefined local variable or method `â?©â??â?' for
>> main:Object (NameError)
> I think you might still be having issues with how your editor is saving
> the file. Try creating a fresh test file with something like Notepad and
> see if you have the same problems.

If you start the utf-8 encoded file with BOM with the following line...

=nil

..., Ruby 1.8 will have no problems.

Wolfgang Nádasi-Donner
--
Posted via http://www.ruby-....